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

Last change on this file since 10954 was 10796, checked in by tbretz, 13 years ago
Added PrintVersion directly to avoid clashes with exceptions.
File size: 5.6 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
66public:
67 Configuration(const std::string &prgname="");
68
69 /// Retrieve data from a database and return them as options
70 static po::basic_parsed_options<char>
71 parse_database(const std::string &database, const po::options_description& desc, bool allow_unregistered=false);
72
73 // Setup
74 void AddOptionsCommandline(const po::options_description &cl, bool visible=true);
75 void AddOptionsConfigfile(const po::options_description &cf, bool visible=true);
76 void AddOptionsEnvironment(const po::options_description &env, bool visible=true);
77 void AddOptionsDatabase(const po::options_description &db, bool visible=true);
78 void AddOptions(const po::options_description &opt, bool visible=true)
79 {
80 AddOptionsCommandline(opt, visible);
81 AddOptionsConfigfile(opt, visible);
82 AddOptionsEnvironment(opt, visible);
83 AddOptionsDatabase(opt, visible);
84 }
85
86 void SetArgumentPositions(const po::positional_options_description &desc);
87
88 void SetNameMapper(const boost::function<std::string(std::string)> &func);
89 void SetNameMapper();
90
91 void SetPrintUsage(const boost::function<void(void)> &func);
92 void SetPrintUsage();
93
94 void SetPrintVersion(const boost::function<void(const std::string &)> &func);
95 void SetPrintVersion();
96
97 void AddEnv(const std::string &conf, const std::string &env)
98 {
99 fEnvMap[env] = conf;
100 }
101
102 // Output
103 void PrintOptions();
104 void PrintUnknown();
105
106 // Process command line arguments
107 const po::variables_map &Parse(int argc, const char **argv);
108
109 bool HasVersion() const
110 {
111 return Has("version");
112 }
113
114 bool HasHelp() const
115 {
116 return Has("help") || Has("help-config") || Has("help-env") || Has("help-database");
117 }
118
119 bool HasPrint() const
120 {
121 return Has("print-all") || Has("print") || Has("print-default") ||
122 Has("print-database") || Has("print-config") ||
123 Has("print-environment") || Has("print-unkown") ||
124 Has("print-options");
125 }
126
127 // Simplified access to the parsed options
128 template<class T>
129 T Get(const char *var) const { return Has(var) ? fVariables[var].as<T>() : T(); }
130 bool Has(const char *var) const { return fVariables.count(var)>0; }
131
132 const std::string &GetName() const { return fName; }
133};
134
135template<class T>
136inline po::typed_value<T> *var(T *ptr=0)
137{ return po::value<T>(ptr); }
138
139template<class T>
140inline po::typed_value<T> *var(const T &val, T *ptr=0)
141{ return po::value<T>(ptr)->default_value(val); }
142
143template<class T>
144inline po::typed_value<std::vector<T>> *vars()
145{ return po::value<std::vector<T>>(); }
146
147inline po::typed_value<bool> *po_switch()
148{ return po::bool_switch(); }
149
150inline po::typed_value<bool> *po_bool()
151{ return po::value<bool>()->implicit_value(true)->default_value(false); }
152
153#endif
Note: See TracBrowser for help on using the repository browser.