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

Last change on this file since 10793 was 10707, checked in by tbretz, 14 years ago
Use the functionality of boost program_options better; implemented po_bool() as a special po_switch() case; added some comments.
File size: 5.4 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
50 /// Helper function which return the max of the two arguments in the first argument
51 static void Max(int &val, const int &comp)
52 {
53 if (comp>val)
54 val=comp;
55 }
56
57 /// Print all options from a list of already parsed options
58 void PrintParsed(const po::parsed_options &parsed) const;
59 /// Print a list of all unkown options within the given vector
60 void PrintUnknown(std::vector<std::string> &vec, int steps=1);
61
62 virtual void PrintUsage() const { }
63
64public:
65 Configuration(const std::string &prgname="");
66
67 /// Retrieve data from a database and return them as options
68 static po::basic_parsed_options<char>
69 parse_database(const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
70
71 // Setup
72 void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
73 void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
74 void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
75 void AddOptionsDatabase(const po::options_description &db, bool visible=true);
76 void AddOptions(const po::options_description &opt, bool visible=true)
77 {
78 AddOptionsCommandline(opt, visible);
79 AddOptionsConfigfile(opt, visible);
80 AddOptionsEnvironment(opt, visible);
81 AddOptionsDatabase(opt, visible);
82 }
83
84 void SetArgumentPositions(const po::positional_options_description &desc);
85
86 void SetNameMapper(const boost::function<std::string(std::string)> &func);
87 void SetNameMapper();
88
89 void SetPrintUsage(const boost::function<void(void)> &func);
90 void SetPrintUsage();
91
92 void AddEnv(const std::string &conf, const std::string &env)
93 {
94 fEnvMap[env] = conf;
95 }
96
97 // Output
98 void PrintOptions();
99 void PrintUnknown();
100
101 // Process command line arguments
102 const po::variables_map &Parse(int argc, const char **argv);
103
104 bool HasVersion() const
105 {
106 return Has("version");
107 }
108
109 bool HasHelp() const
110 {
111 return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
112 }
113
114 bool HasPrint() const
115 {
116 return Has("print-all") || Has("print") || Has("print-default") ||
117 Has("print-database") || Has("print-config") ||
118 Has("print-environment") || Has("print-unkown") ||
119 Has("print-options");
120 }
121
122 // Simplified access to the parsed options
123 template<class T>
124 T Get(const char *var) const { return Has(var) ? fVariables[var].as<T>() : T(); }
125 bool Has(const char *var) const { return fVariables.count(var)>0; }
126
127 const std::string &GetName() const { return fName; }
128};
129
130template<class T>
131inline po::typed_value<T> *var(T *ptr=0)
132{ return po::value<T>(ptr); }
133
134template<class T>
135inline po::typed_value<T> *var(const T &val, T *ptr=0)
136{ return po::value<T>(ptr)->default_value(val); }
137
138template<class T>
139inline po::typed_value<std::vector<T>> *vars()
140{ return po::value<std::vector<T>>(); }
141
142inline po::typed_value<bool> *po_switch()
143{ return po::bool_switch(); }
144
145inline po::typed_value<bool> *po_bool()
146{ return po::value<bool>()->implicit_value(true)->default_value(false); }
147
148#endif
Note: See TracBrowser for help on using the repository browser.