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

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