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

Last change on this file since 11329 was 11329, checked in by tbretz, 13 years ago
Fixed a typo in HasPrint - print-unknown wasn't recognized correctly; removed the Has-clause from the Get-template; added GetMap and GetKeys
File size: 6.6 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 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
69public:
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 char *var) const { return fVariables[var].as<T>(); }
134 bool Has(const char *var) const { return fVariables.count(var)>0; }
135
136 template<class T>
137 std::map<std::string, T> GetMap(const std::string &var) const
138 {
139 const size_t len = var.length();
140
141 std::map<std::string, T> rc;
142 for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
143 it!=fVariables.end(); it++)
144 if (it->first.substr(0, len)==var)
145 rc[it->first] = it->second.as<T>();
146
147 return rc;
148 }
149
150 template<class T>
151 std::vector<std::string> GetKeys(const std::string &var) const
152 {
153 const size_t len = var.length();
154
155 std::vector<std::string> rc;
156 for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
157 it!=fVariables.end(); it++)
158 if (it->first.substr(0, len)==var)
159 rc.push_back(it->first);
160
161 return rc;
162 }
163
164 const std::string &GetName() const { return fName; }
165};
166
167template<class T>
168inline po::typed_value<T> *var(T *ptr=0)
169{ return po::value<T>(ptr); }
170
171template<class T>
172inline po::typed_value<T> *var(const T &val, T *ptr=0)
173{ return po::value<T>(ptr)->default_value(val); }
174
175template<class T>
176inline po::typed_value<std::vector<T>> *vars()
177{ return po::value<std::vector<T>>(); }
178
179inline po::typed_value<bool> *po_switch()
180{ return po::bool_switch(); }
181
182inline po::typed_value<bool> *po_bool()
183{ return po::value<bool>()->implicit_value(true)->default_value(false); }
184
185#endif
Note: See TracBrowser for help on using the repository browser.