source: trunk/FACT++/src/Configuration.h@ 19435

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