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

Last change on this file since 11217 was 11039, checked in by tbretz, 13 years ago
'UnLibToolize' the program name.
File size: 5.7 KB
Line 
1#ifndef FACT_Configuration
2#define FACT_Configuration
3
4#include <boost/program_options.hpp>
5
6namespace po = boost::program_options;
7
8class Configuration
9{
10private:
11 /// Convienience enum to access the fOption* data memebers more verbosely.
12 enum
13 {
14 kHidden = 0, ///< Index for hidden options (not shown in PrintParsed)
15 kVisible = 1 ///< Index for options visible in PrintParsed
16 };
17
18 const std::string fName; /// argv[0]
19
20 std::map<std::string, std::string> fEnvMap;
21
22 po::options_description fOptionsCommandline[2]; /// Description of the command-line options
23 po::options_description fOptionsConfigfile[2]; /// Description of the options in the configuration file
24 po::options_description fOptionsDatabase[2]; /// Description of options from the database
25 po::options_description fOptionsEnvironment[2]; /// Description of options from the environment
26
27 po::positional_options_description fArgumentPositions; /// Description of positional command-line options (arguments)
28
29 std::vector<std::string> fUnknownCommandline; /// Storage container for unrecognized commandline options
30 std::vector<std::string> fUnknownConfigfile; /// Storage container for unrecognized options from configuration files
31 std::vector<std::string> fUnknownEnvironment; /// Storage container for unrecognized options from the environment
32 std::vector<std::string> fUnknownDatabase; /// Storage container for unrecognized options retrieved from the database
33
34 std::string fPriorityFile; /// File name of the priority configuration file (overwrites option from the databse)
35 std::string fDefaultFile; /// File name of the default configuration file (usually {program}.rc)
36 std::string fDatabase; /// URL for database connection (see Configuration::parse_database)
37
38 po::variables_map fVariables; /// Variables as compiled by the Parse-function, which will be passed to the program
39
40 /// A default mapper for environment variables skipping all of them
41 std::string DefaultMapper(const std::string env)
42 {
43 return fEnvMap[env];
44 }
45
46 /// Pointer to the mapper function for environment variables
47 boost::function<std::string(std::string)> fNameMapper;
48 boost::function<void()> fPrintUsage;
49 boost::function<void(const std::string&)> fPrintVersion;
50
51 /// Helper function which return the max of the two arguments in the first argument
52 static void Max(int &val, const int &comp)
53 {
54 if (comp>val)
55 val=comp;
56 }
57
58 /// Print all options from a list of already parsed options
59 void PrintParsed(const po::parsed_options &parsed) const;
60 /// Print a list of all unkown options within the given vector
61 void PrintUnknown(std::vector<std::string> &vec, int steps=1);
62
63 virtual void PrintUsage() const { }
64 virtual void PrintVersion() const;
65
66 std::string UnLibToolize(const std::string &src) const;
67
68public:
69 Configuration(const std::string &prgname="");
70
71 /// Retrieve data from a database and return them as options
72 static po::basic_parsed_options<char>
73 parse_database(const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
74
75 // Setup
76 void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
77 void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
78 void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
79 void AddOptionsDatabase(const po::options_description &db, bool visible=true);
80 void AddOptions(const po::options_description &opt, bool visible=true)
81 {
82 AddOptionsCommandline(opt, visible);
83 AddOptionsConfigfile(opt, visible);
84 AddOptionsEnvironment(opt, visible);
85 AddOptionsDatabase(opt, visible);
86 }
87
88 void SetArgumentPositions(const po::positional_options_description &desc);
89
90 void SetNameMapper(const boost::function<std::string(std::string)> &func);
91 void SetNameMapper();
92
93 void SetPrintUsage(const boost::function<void(void)> &func);
94 void SetPrintUsage();
95
96 void SetPrintVersion(const boost::function<void(const std::string &)> &func);
97 void SetPrintVersion();
98
99 void AddEnv(const std::string &conf, const std::string &env)
100 {
101 fEnvMap[env] = conf;
102 }
103
104 // Output
105 void PrintOptions();
106 void PrintUnknown();
107
108 // Process command line arguments
109 const po::variables_map &Parse(int argc, const char **argv);
110
111 bool HasVersion() const
112 {
113 return Has("version");
114 }
115
116 bool HasHelp() const
117 {
118 return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
119 }
120
121 bool HasPrint() const
122 {
123 return Has("print-all") || Has("print") || Has("print-default") ||
124 Has("print-database") || Has("print-config") ||
125 Has("print-environment") || Has("print-unkown") ||
126 Has("print-options");
127 }
128
129 // Simplified access to the parsed options
130 template<class T>
131 T Get(const char *var) const { return Has(var) ? fVariables[var].as<T>() : T(); }
132 bool Has(const char *var) const { return fVariables.count(var)>0; }
133
134 const std::string &GetName() const { return fName; }
135};
136
137template<class T>
138inline po::typed_value<T> *var(T *ptr=0)
139{ return po::value<T>(ptr); }
140
141template<class T>
142inline po::typed_value<T> *var(const T &val, T *ptr=0)
143{ return po::value<T>(ptr)->default_value(val); }
144
145template<class T>
146inline po::typed_value<std::vector<T>> *vars()
147{ return po::value<std::vector<T>>(); }
148
149inline po::typed_value<bool> *po_switch()
150{ return po::bool_switch(); }
151
152inline po::typed_value<bool> *po_bool()
153{ return po::value<bool>()->implicit_value(true)->default_value(false); }
154
155#endif
Note: See TracBrowser for help on using the repository browser.