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

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