FACT++  1.0
Configuration.h
Go to the documentation of this file.
1 #ifndef FACT_Configuration
2 #define FACT_Configuration
3 
4 #include <iostream>
5 #include <boost/program_options.hpp>
6 
7 namespace po = boost::program_options;
8 
10 {
11 private:
13  enum
14  {
15  kHidden = 0,
16  kVisible = 1
17  };
18 
19  const std::string fName;
20 
21  std::map<std::string, std::string> fEnvMap;
22 
23  po::options_description fOptionsCommandline[2];
24  po::options_description fOptionsConfigfile[2];
25  po::options_description fOptionsDatabase[2];
26  po::options_description fOptionsEnvironment[2];
27 
28  po::positional_options_description fArgumentPositions;
29 
30  std::vector<std::string> fUnknownCommandline;
31  std::vector<std::string> fUnknownConfigfile;
32  std::vector<std::string> fUnknownEnvironment;
33  std::vector<std::string> fUnknownDatabase;
34 
35  std::map<std::string, std::string> fWildcardOptions;
36 
37  std::string fPriorityFile;
38  std::string fDefaultFile;
39  std::string fDatabase;
40 
41  po::variables_map fVariables;
42 
44  std::string DefaultMapper(const std::string env)
45  {
46  return fEnvMap[env];
47  }
48 
50  std::function<std::string(std::string)> fNameMapper;
51  std::function<void()> fPrintUsage;
52  std::function<void(const std::string&)> fPrintVersion;
53 
55  static void Max(int &val, const int &comp)
56  {
57  if (comp>val)
58  val=comp;
59  }
60 
62  void CreateWildcardOptions();
63 
64  // Helper functions for PrintOptions and GetOptions
65  template<class T>
66  std::string VecAsStr(const po::variable_value &v) const;
67  std::string VarAsStr(const po::variable_value &v) const;
68 
70  void PrintParsed(const po::parsed_options &parsed) const;
72  void PrintUnknown(const std::vector<std::string> &vec, int steps=1) const;
73 
74  virtual void PrintUsage() const { }
75  virtual void PrintVersion() const;
76 
77  std::string UnLibToolize(const std::string &src) const;
78 
79 public:
80  Configuration(const std::string &prgname="");
81  virtual ~Configuration() { }
82 
84  static po::basic_parsed_options<char>
85  parse_database(const std::string &prgname, const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
86 
87  // Setup
88  void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
89  void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
90  void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
91  void AddOptionsDatabase(const po::options_description &db, bool visible=true);
92  void AddOptions(const po::options_description &opt, bool visible=true)
93  {
94  AddOptionsCommandline(opt, visible);
95  AddOptionsConfigfile(opt, visible);
96  AddOptionsEnvironment(opt, visible);
97  AddOptionsDatabase(opt, visible);
98  }
99 
100  void SetArgumentPositions(const po::positional_options_description &desc);
101 
102  void SetNameMapper(const std::function<std::string(std::string)> &func);
103  void SetNameMapper();
104 
105  void SetPrintUsage(const std::function<void(void)> &func);
106  void SetPrintUsage();
107 
108  void SetPrintVersion(const std::function<void(const std::string &)> &func);
109  void SetPrintVersion();
110 
111  void AddEnv(const std::string &conf, const std::string &env)
112  {
113  fEnvMap[env] = conf;
114  }
115 
116  // Output
117  void PrintOptions() const;
118  void PrintUnknown() const;
119  void PrintWildcardOptions() const;
120 
121  const std::map<std::string,std::string> &GetWildcardOptions() const { return fWildcardOptions; }
122  const std::vector<std::string> GetWildcardOptions(const std::string &opt) const;
123 
124  template<class T>
125  const std::map<std::string,T> GetOptions(const std::string &opt)
126  {
127  const std::vector<std::string> rc = GetWildcardOptions(opt+'*');
128 
129  std::map<std::string,T> map;
130  for (auto it=rc.begin(); it!=rc.end(); it++)
131  map[it->substr(opt.length())] = Get<T>(*it);
132 
133  return map;
134  }
135 
136  std::multimap<std::string, std::string> GetOptions() const;
137 
138  // Process command line arguments
139  const po::variables_map &Parse(int argc, const char **argv, const std::function<void()> &func=std::function<void()>());
140  bool DoParse(int argc, const char **argv, const std::function<void()> &func=std::function<void()>());
141 
142  bool HasVersion()
143  {
144  return Has("version");
145  }
146 
147  bool HasHelp()
148  {
149  return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
150  }
151 
152  bool HasPrint()
153  {
154  return Has("print-all") || Has("print") || Has("print-default") ||
155  Has("print-database") || Has("print-config") ||
156  Has("print-environment") || Has("print-unknown") ||
157  Has("print-options") || Has("print-wildcards");
158  }
159 
160  // Simplified access to the parsed options
161  template<class T>
162  T Get(const std::string &var) { fWildcardOptions.erase(var); return fVariables[var].as<T>(); }
163  bool Has(const std::string &var) { fWildcardOptions.erase(var); return fVariables.count(var)>0; }
164 
165  template<class T>
166  std::vector<T> Vec(const std::string &var) { return Has(var) ? fVariables[var].as<std::vector<T>>() : std::vector<T>(); }
167 
168  template<class T, class S>
169  T Get(const std::string &var, const S &val)
170  {
171  std::ostringstream str;
172  str << var << val;
173  return Get<T>(str.str());
174  }
175 
176  template<class T>
177  bool Has(const std::string &var, const T &val)
178  {
179  std::ostringstream str;
180  str << var << val;
181  return Has(str.str());
182  }
183 
184  template<class T, class S>
185  T GetDef(const std::string &var, const S &val)
186  {
187  return Has(var, val) ? Get<T>(var, val) : Get<T>(var+"default");
188  }
189 
190  template<class T>
191  bool HasDef(const std::string &var, const T &val)
192  {
193  // Make sure the .default option is touched
194  const bool rc = Has(var+"default");
195 
196  return Has(var, val) ? true : rc;
197  }
198 
199  void Remove(const std::string &var)
200  {
201  fVariables.erase(var);
202  }
203 
204 /*
205  template<class T>
206  std::map<std::string, T> GetMap(const std::string &var)
207  {
208  const size_t len = var.length();
209 
210  std::map<std::string, T> rc;
211  for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
212  it!=fVariables.end(); it++)
213  if (it->first.substr(0, len)==var)
214  rc[it->first] = it->second.as<T>();
215 
216  return rc;
217  }
218 
219  template<class T>
220  std::vector<std::string> GetKeys(const std::string &var)
221  {
222  const size_t len = var.length();
223 
224  std::vector<std::string> rc;
225  for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
226  it!=fVariables.end(); it++)
227  if (it->first.substr(0, len)==var)
228  rc.push_back(it->first);
229 
230  return rc;
231  }
232 */
233  const std::string &GetName() const { return fName; }
234 };
235 
236 template<typename T>
237 struct Hex
238 {
239  T val;
240  Hex() { }
241  Hex(const T &v) : val(v) { }
242  operator T() const { return val; }
243 };
244 template<typename T>
245 std::istream &operator>>(std::istream &in, Hex<T> &rc)
246 {
247  T val;
248  in >> std::hex >> val;
249  rc.val = val;
250  return in;
251 }
252 
253 template<class T>
254 inline po::typed_value<T> *var(T *ptr=0)
255 { return po::value<T>(ptr); }
256 
257 template<class T>
258 inline po::typed_value<T> *var(const T &val, T *ptr=0)
259 { return po::value<T>(ptr)->default_value(val); }
260 
261 template<class T>
262 inline po::typed_value<std::vector<T>> *vars()
263 { return po::value<std::vector<T>>(); }
264 
265 inline po::typed_value<bool> *po_switch()
266 { return po::bool_switch(); }
267 
268 inline po::typed_value<bool> *po_bool(bool def=false)
269 { return po::value<bool>()->implicit_value(true)->default_value(def); }
270 
271 #endif
Hex(const T &v)
T Get(const std::string &var, const S &val)
void PrintWildcardOptions() const
void AddOptionsEnvironment(const po::options_description &env, bool visible=true)
std::string fDatabase
File name of the default configuration file (usually {program}.rc)
Definition: Configuration.h:39
std::string fPriorityFile
Options which were registered using wildcards.
Definition: Configuration.h:37
Configuration(const std::string &prgname="")
bool Has(const std::string &var, const T &val)
std::string VecAsStr(const po::variable_value &v) const
char str[80]
Definition: test_client.c:7
std::vector< std::string > fUnknownDatabase
Storage container for unrecognized options from the environment.
Definition: Configuration.h:33
void AddOptionsConfigfile(const po::options_description &cf, bool visible=true)
T Get(const std::string &var)
po::options_description fOptionsEnvironment[2]
Description of options from the database.
Definition: Configuration.h:26
po::typed_value< T > * var(T *ptr=0)
po::typed_value< bool > * po_switch()
void CreateWildcardOptions()
Helper for Parse to create list of used wildcard options.
const std::map< std::string, T > GetOptions(const std::string &opt)
void SetNameMapper()
void AddEnv(const std::string &conf, const std::string &env)
void Remove(const std::string &var)
std::vector< T > Vec(const std::string &var)
Index for hidden options (not shown in PrintParsed)
Definition: Configuration.h:15
void SetArgumentPositions(const po::positional_options_description &desc)
std::function< std::string(std::string)> fNameMapper
Pointer to the mapper function for environment variables.
Definition: Configuration.h:50
Index for options visible in PrintParsed.
Definition: Configuration.h:16
virtual ~Configuration()
Definition: Configuration.h:81
po::positional_options_description fArgumentPositions
Description of options from the environment.
Definition: Configuration.h:28
std::multimap< std::string, std::string > GetOptions() const
po::options_description fOptionsDatabase[2]
Description of the options in the configuration file.
Definition: Configuration.h:25
void PrintParsed(const po::parsed_options &parsed) const
Print all options from a list of already parsed options.
void SetPrintUsage()
void AddOptionsDatabase(const po::options_description &db, bool visible=true)
std::function< void(const std::string &)> fPrintVersion
Definition: Configuration.h:52
void PrintUnknown() const
bool Has(const std::string &var)
T GetDef(const std::string &var, const S &val)
void PrintOptions() const
po::typed_value< std::vector< T > > * vars()
std::istream & operator>>(std::istream &in, Hex< T > &rc)
std::string fDefaultFile
File name of the priority configuration file (overwrites option from the databse) ...
Definition: Configuration.h:38
void AddOptions(const po::options_description &opt, bool visible=true)
Definition: Configuration.h:92
void AddOptionsCommandline(const po::options_description &cl, bool visible=true)
po::options_description fOptionsCommandline[2]
Definition: Configuration.h:23
Commandline parsing, resource file parsing and database access.
Definition: Configuration.h:9
std::map< std::string, std::string > fWildcardOptions
Storage container for unrecognized options retrieved from the database.
Definition: Configuration.h:35
const std::string fName
Definition: Configuration.h:19
po::options_description fOptionsConfigfile[2]
Description of the command-line options.
Definition: Configuration.h:24
std::vector< std::string > fUnknownCommandline
Description of positional command-line options (arguments)
Definition: Configuration.h:30
std::function< void()> fPrintUsage
Definition: Configuration.h:51
std::string VarAsStr(const po::variable_value &v) const
static po::basic_parsed_options< char > parse_database(const std::string &prgname, const std::string &database, const po::options_description &desc, bool allow_unregistered=false)
Retrieve data from a database and return them as options.
static void Max(int &val, const int &comp)
Helper function which return the max of the two arguments in the first argument.
Definition: Configuration.h:55
const std::map< std::string, std::string > & GetWildcardOptions() const
po::variables_map fVariables
URL for database connection (see Configuration::parse_database)
Definition: Configuration.h:41
std::string DefaultMapper(const std::string env)
Variables as compiled by the Parse-function, which will be passed to the program. ...
Definition: Configuration.h:44
virtual void PrintVersion() const
po::typed_value< bool > * po_bool(bool def=false)
std::string UnLibToolize(const std::string &src) const
const po::variables_map & Parse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
virtual void PrintUsage() const
Definition: Configuration.h:74
std::vector< std::string > fUnknownConfigfile
Storage container for unrecognized commandline options.
Definition: Configuration.h:31
bool HasDef(const std::string &var, const T &val)
std::vector< std::string > fUnknownEnvironment
Storage container for unrecognized options from configuration files.
Definition: Configuration.h:32
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
const std::string & GetName() const
void SetPrintVersion()
std::map< std::string, std::string > fEnvMap
argv[0]
Definition: Configuration.h:21