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 |
|
---|
9 | class Configuration
|
---|
10 | {
|
---|
11 | private:
|
---|
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 | boost::function<std::string(std::string)> fNameMapper;
|
---|
49 | boost::function<void()> fPrintUsage;
|
---|
50 | boost::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 | /// Print all options from a list of already parsed options
|
---|
60 | void PrintParsed(const po::parsed_options &parsed) const;
|
---|
61 | /// Print a list of all unkown options within the given vector
|
---|
62 | void PrintUnknown(std::vector<std::string> &vec, int steps=1);
|
---|
63 |
|
---|
64 | virtual void PrintUsage() const { }
|
---|
65 | virtual void PrintVersion() const;
|
---|
66 |
|
---|
67 | std::string UnLibToolize(const std::string &src) const;
|
---|
68 |
|
---|
69 | public:
|
---|
70 | Configuration(const std::string &prgname="");
|
---|
71 | virtual ~Configuration() { }
|
---|
72 |
|
---|
73 | /// Retrieve data from a database and return them as options
|
---|
74 | static po::basic_parsed_options<char>
|
---|
75 | parse_database(const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
|
---|
76 |
|
---|
77 | // Setup
|
---|
78 | void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
|
---|
79 | void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
|
---|
80 | void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
|
---|
81 | void AddOptionsDatabase(const po::options_description &db, bool visible=true);
|
---|
82 | void AddOptions(const po::options_description &opt, bool visible=true)
|
---|
83 | {
|
---|
84 | AddOptionsCommandline(opt, visible);
|
---|
85 | AddOptionsConfigfile(opt, visible);
|
---|
86 | AddOptionsEnvironment(opt, visible);
|
---|
87 | AddOptionsDatabase(opt, visible);
|
---|
88 | }
|
---|
89 |
|
---|
90 | void SetArgumentPositions(const po::positional_options_description &desc);
|
---|
91 |
|
---|
92 | void SetNameMapper(const boost::function<std::string(std::string)> &func);
|
---|
93 | void SetNameMapper();
|
---|
94 |
|
---|
95 | void SetPrintUsage(const boost::function<void(void)> &func);
|
---|
96 | void SetPrintUsage();
|
---|
97 |
|
---|
98 | void SetPrintVersion(const boost::function<void(const std::string &)> &func);
|
---|
99 | void SetPrintVersion();
|
---|
100 |
|
---|
101 | void AddEnv(const std::string &conf, const std::string &env)
|
---|
102 | {
|
---|
103 | fEnvMap[env] = conf;
|
---|
104 | }
|
---|
105 |
|
---|
106 | // Output
|
---|
107 | void PrintOptions();
|
---|
108 | void PrintUnknown();
|
---|
109 |
|
---|
110 | // Process command line arguments
|
---|
111 | const po::variables_map &Parse(int argc, const char **argv);
|
---|
112 |
|
---|
113 | bool HasVersion() const
|
---|
114 | {
|
---|
115 | return Has("version");
|
---|
116 | }
|
---|
117 |
|
---|
118 | bool HasHelp() const
|
---|
119 | {
|
---|
120 | return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
|
---|
121 | }
|
---|
122 |
|
---|
123 | bool HasPrint() const
|
---|
124 | {
|
---|
125 | return Has("print-all") || Has("print") || Has("print-default") ||
|
---|
126 | Has("print-database") || Has("print-config") ||
|
---|
127 | Has("print-environment") || Has("print-unknown") ||
|
---|
128 | Has("print-options");
|
---|
129 | }
|
---|
130 |
|
---|
131 | // Simplified access to the parsed options
|
---|
132 | template<class T>
|
---|
133 | T Get(const std::string &var) const { return fVariables[var].as<T>(); }
|
---|
134 | template<class T>
|
---|
135 | std::vector<T> Vec(const std::string &var) const { return Has(var) ? fVariables[var].as<std::vector<T>>() : std::vector<T>(); }
|
---|
136 | bool Has(const std::string &var) const { return fVariables.count(var)>0; }
|
---|
137 |
|
---|
138 | template<class T, class S>
|
---|
139 | T Get(const std::string &var, const S &val) const
|
---|
140 | {
|
---|
141 | std::ostringstream str;
|
---|
142 | str << var << val;
|
---|
143 | return Get<T>(str.str());
|
---|
144 | }
|
---|
145 |
|
---|
146 | template<class T>
|
---|
147 | bool Has(const std::string &var, const T &val) const
|
---|
148 | {
|
---|
149 | std::ostringstream str;
|
---|
150 | str << var << val;
|
---|
151 | return Has(str.str());
|
---|
152 | }
|
---|
153 |
|
---|
154 | template<class T, class S>
|
---|
155 | T GetDef(const std::string &var, const S &val) const
|
---|
156 | {
|
---|
157 | return Has(var, val) ? Get<T>(var, val) : Get<T>(var+"default");
|
---|
158 | }
|
---|
159 |
|
---|
160 | template<class T>
|
---|
161 | bool HasDef(const std::string &var, const T &val) const
|
---|
162 | {
|
---|
163 | return Has(var, val) ? true : Has(var+"default");
|
---|
164 | }
|
---|
165 |
|
---|
166 | template<class T>
|
---|
167 | std::map<std::string, T> GetMap(const std::string &var) const
|
---|
168 | {
|
---|
169 | const size_t len = var.length();
|
---|
170 |
|
---|
171 | std::map<std::string, T> rc;
|
---|
172 | for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
|
---|
173 | it!=fVariables.end(); it++)
|
---|
174 | if (it->first.substr(0, len)==var)
|
---|
175 | rc[it->first] = it->second.as<T>();
|
---|
176 |
|
---|
177 | return rc;
|
---|
178 | }
|
---|
179 |
|
---|
180 | template<class T>
|
---|
181 | std::vector<std::string> GetKeys(const std::string &var) const
|
---|
182 | {
|
---|
183 | const size_t len = var.length();
|
---|
184 |
|
---|
185 | std::vector<std::string> rc;
|
---|
186 | for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
|
---|
187 | it!=fVariables.end(); it++)
|
---|
188 | if (it->first.substr(0, len)==var)
|
---|
189 | rc.push_back(it->first);
|
---|
190 |
|
---|
191 | return rc;
|
---|
192 | }
|
---|
193 |
|
---|
194 | const std::string &GetName() const { return fName; }
|
---|
195 | };
|
---|
196 |
|
---|
197 | template<typename T>
|
---|
198 | struct Hex
|
---|
199 | {
|
---|
200 | T val;
|
---|
201 | Hex() { }
|
---|
202 | Hex(const T &v) : val(v) { }
|
---|
203 | operator T() const { return val; }
|
---|
204 | };
|
---|
205 | template<typename T>
|
---|
206 | std::istream &operator>>(std::istream &in, Hex<T> &rc)
|
---|
207 | {
|
---|
208 | T val;
|
---|
209 | in >> std::hex >> val;
|
---|
210 | rc.val = val;
|
---|
211 | return in;
|
---|
212 | }
|
---|
213 |
|
---|
214 | template<class T>
|
---|
215 | inline po::typed_value<T> *var(T *ptr=0)
|
---|
216 | { return po::value<T>(ptr); }
|
---|
217 |
|
---|
218 | template<class T>
|
---|
219 | inline po::typed_value<T> *var(const T &val, T *ptr=0)
|
---|
220 | { return po::value<T>(ptr)->default_value(val); }
|
---|
221 |
|
---|
222 | template<class T>
|
---|
223 | inline po::typed_value<std::vector<T>> *vars()
|
---|
224 | { return po::value<std::vector<T>>(); }
|
---|
225 |
|
---|
226 | inline po::typed_value<bool> *po_switch()
|
---|
227 | { return po::bool_switch(); }
|
---|
228 |
|
---|
229 | inline po::typed_value<bool> *po_bool()
|
---|
230 | { return po::value<bool>()->implicit_value(true)->default_value(false); }
|
---|
231 |
|
---|
232 | #endif
|
---|