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

Last change on this file since 20084 was 19805, checked in by tbretz, 5 years ago
Added a 'Map' which is a resource of the kind 'something/somethingelse'
File size: 9.9 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 struct Map : std::pair<std::string, std::string>
83 {
84 Map() { }
85 };
86
87 Configuration(const std::string &prgname="");
88 virtual ~Configuration() { }
89
90 /// Retrieve data from a database and return them as options
91 static po::basic_parsed_options<char>
92 parse_database(const std::string &prgname, const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
93
94 // Setup
95 void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
96 void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
97 void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
98 void AddOptionsDatabase(const po::options_description &db, bool visible=true);
99 void AddOptions(const po::options_description &opt, bool visible=true)
100 {
101 AddOptionsCommandline(opt, visible);
102 AddOptionsConfigfile(opt, visible);
103 AddOptionsEnvironment(opt, visible);
104 AddOptionsDatabase(opt, visible);
105 }
106
107 void SetArgumentPositions(const po::positional_options_description &desc);
108
109 void SetNameMapper(const std::function<std::string(std::string)> &func);
110 void SetNameMapper();
111
112 void SetPrintUsage(const std::function<void(void)> &func);
113 void SetPrintUsage();
114
115 void SetPrintVersion(const std::function<void(const std::string &)> &func);
116 void SetPrintVersion();
117
118 void AddEnv(const std::string &conf, const std::string &env)
119 {
120 fEnvMap[env] = conf;
121 }
122
123 // Output
124 void PrintOptions() const;
125 void PrintUnknown() const;
126 void PrintWildcardOptions() const;
127
128 const std::map<std::string,std::string> &GetWildcardOptions() const { return fWildcardOptions; }
129 const std::vector<std::string> GetWildcardOptions(const std::string &opt) const;
130
131 template<class T>
132 const std::map<std::string,T> GetOptions(const std::string &opt)
133 {
134 const std::vector<std::string> rc = GetWildcardOptions(opt+'*');
135
136 std::map<std::string,T> map;
137 for (auto it=rc.begin(); it!=rc.end(); it++)
138 map[it->substr(opt.length())] = Get<T>(*it);
139
140 return map;
141 }
142
143 std::multimap<std::string, std::string> GetOptions() const;
144
145 // Process command line arguments
146 const po::variables_map &Parse(int argc, const char **argv, const std::function<void()> &func=std::function<void()>());
147 const po::variables_map &ParseFile(const std::string &fname, const bool &checkf);
148 bool DoParse(int argc, const char **argv, const std::function<void()> &func=std::function<void()>());
149 bool ReadFile(const std::string &fname, const bool &checkf=false);
150
151 bool HasVersion()
152 {
153 return Has("version");
154 }
155
156 bool HasHelp()
157 {
158 return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
159 }
160
161 bool HasPrint()
162 {
163 return Has("print-all") || Has("print") || Has("print-default") ||
164 Has("print-database") || Has("print-config") ||
165 Has("print-environment") || Has("print-unknown") ||
166 Has("print-options") || Has("print-wildcards");
167 }
168
169 // Simplified access to the parsed options
170 template<class T>
171 T Get(const std::string &var) { fWildcardOptions.erase(var); return fVariables[var].as<T>(); }
172 bool Has(const std::string &var) { fWildcardOptions.erase(var); return fVariables.count(var)>0; }
173
174 template<class T>
175 std::vector<T> Vec(const std::string &var) { return Has(var) ? fVariables[var].as<std::vector<T>>() : std::vector<T>(); }
176
177 template<class T, class S>
178 T Get(const std::string &var, const S &val)
179 {
180 std::ostringstream str;
181 str << var << val;
182 return Get<T>(str.str());
183 }
184
185 template<class T>
186 bool Has(const std::string &var, const T &val)
187 {
188 std::ostringstream str;
189 str << var << val;
190 return Has(str.str());
191 }
192
193 template<class T, class S>
194 T GetDef(const std::string &var, const S &val)
195 {
196 return Has(var, val) ? Get<T>(var, val) : Get<T>(var+"default");
197 }
198
199 template<class T>
200 bool HasDef(const std::string &var, const T &val)
201 {
202 // Make sure the .default option is touched
203 const bool rc = Has(var+"default");
204
205 return Has(var, val) ? true : rc;
206 }
207
208 void Remove(const std::string &var)
209 {
210 fVariables.erase(var);
211 }
212
213 const std::string GetPrefixedString(const std::string &var)
214 {
215 const boost::filesystem::path ff(Get<std::string>(var));
216 const boost::filesystem::path pp(fPrefixPath);
217 return (ff.has_parent_path() ? ff : pp/ff).string();
218 }
219
220/*
221 template<class T>
222 std::map<std::string, T> GetMap(const std::string &var)
223 {
224 const size_t len = var.length();
225
226 std::map<std::string, T> rc;
227 for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
228 it!=fVariables.end(); it++)
229 if (it->first.substr(0, len)==var)
230 rc[it->first] = it->second.as<T>();
231
232 return rc;
233 }
234
235 template<class T>
236 std::vector<std::string> GetKeys(const std::string &var)
237 {
238 const size_t len = var.length();
239
240 std::vector<std::string> rc;
241 for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
242 it!=fVariables.end(); it++)
243 if (it->first.substr(0, len)==var)
244 rc.push_back(it->first);
245
246 return rc;
247 }
248*/
249 const std::string &GetName() const { return fName; }
250 const boost::filesystem::path GetPrefixPath() const { return fPrefixPath; }
251};
252
253template<typename T>
254struct Hex
255{
256 T val;
257 Hex() { }
258 Hex(const T &v) : val(v) { }
259 operator T() const { return val; }
260};
261template<typename T>
262std::istream &operator>>(std::istream &in, Hex<T> &rc)
263{
264 T val;
265 in >> std::hex >> val;
266 rc.val = val;
267 return in;
268}
269
270template<class T>
271inline po::typed_value<T> *var(T *ptr=0)
272{ return po::value<T>(ptr); }
273
274template<class T>
275inline po::typed_value<T> *var(const T &val, T *ptr=0)
276{ return po::value<T>(ptr)->default_value(val); }
277
278template<class T>
279inline po::typed_value<std::vector<T>> *vars()
280{ return po::value<std::vector<T>>(); }
281
282inline po::typed_value<bool> *po_switch()
283{ return po::bool_switch(); }
284
285inline po::typed_value<bool> *po_bool(bool def=false)
286{ return po::value<bool>()->implicit_value(true)->default_value(def); }
287
288
289std::istream &operator>>(std::istream &in, Configuration::Map &m);
290
291#endif
Note: See TracBrowser for help on using the repository browser.