| 1 | #ifndef FACT_Configuration
|
|---|
| 2 | #define FACT_Configuration
|
|---|
| 3 |
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <boost/program_options.hpp>
|
|---|
| 6 |
|
|---|
| 7 | namespace po = boost::program_options;
|
|---|
| 8 |
|
|---|
| 9 | class Configuration
|
|---|
| 10 | {
|
|---|
| 11 | private:
|
|---|
| 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 |
|
|---|
| 69 | public:
|
|---|
| 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 std::string &var) const { return fVariables[var].as<T>(); }
|
|---|
| 134 | bool Has(const std::string &var) const { return fVariables.count(var)>0; }
|
|---|
| 135 |
|
|---|
| 136 | template<class T, class S>
|
|---|
| 137 | T Get(const std::string &var, const S &val) const
|
|---|
| 138 | {
|
|---|
| 139 | std::ostringstream str;
|
|---|
| 140 | str << var << val;
|
|---|
| 141 | return Get<T>(str.str());
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | template<class T>
|
|---|
| 145 | bool Has(const std::string &var, const T &val) const
|
|---|
| 146 | {
|
|---|
| 147 | std::ostringstream str;
|
|---|
| 148 | str << var << val;
|
|---|
| 149 | return Has(str.str());
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | template<class T, class S>
|
|---|
| 153 | T GetDef(const std::string &var, const S &val) const
|
|---|
| 154 | {
|
|---|
| 155 | return Has(var, val) ? Get<T>(var, val) : Get<T>(var+"default");
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | template<class T>
|
|---|
| 159 | bool HasDef(const std::string &var, const T &val) const
|
|---|
| 160 | {
|
|---|
| 161 | return Has(var, val) ? true : Has(var+"default");
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | template<class T>
|
|---|
| 165 | std::map<std::string, T> GetMap(const std::string &var) const
|
|---|
| 166 | {
|
|---|
| 167 | const size_t len = var.length();
|
|---|
| 168 |
|
|---|
| 169 | std::map<std::string, T> rc;
|
|---|
| 170 | for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
|
|---|
| 171 | it!=fVariables.end(); it++)
|
|---|
| 172 | if (it->first.substr(0, len)==var)
|
|---|
| 173 | rc[it->first] = it->second.as<T>();
|
|---|
| 174 |
|
|---|
| 175 | return rc;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | template<class T>
|
|---|
| 179 | std::vector<std::string> GetKeys(const std::string &var) const
|
|---|
| 180 | {
|
|---|
| 181 | const size_t len = var.length();
|
|---|
| 182 |
|
|---|
| 183 | std::vector<std::string> rc;
|
|---|
| 184 | for (std::map<std::string, boost::program_options::variable_value>::const_iterator it=fVariables.begin();
|
|---|
| 185 | it!=fVariables.end(); it++)
|
|---|
| 186 | if (it->first.substr(0, len)==var)
|
|---|
| 187 | rc.push_back(it->first);
|
|---|
| 188 |
|
|---|
| 189 | return rc;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | const std::string &GetName() const { return fName; }
|
|---|
| 193 | };
|
|---|
| 194 |
|
|---|
| 195 | template<typename T>
|
|---|
| 196 | struct Hex
|
|---|
| 197 | {
|
|---|
| 198 | T val;
|
|---|
| 199 | Hex() { }
|
|---|
| 200 | Hex(const T &v) : val(v) { }
|
|---|
| 201 | operator T() const { return val; }
|
|---|
| 202 | };
|
|---|
| 203 | template<typename T>
|
|---|
| 204 | std::istream &operator>>(std::istream &in, Hex<T> &rc)
|
|---|
| 205 | {
|
|---|
| 206 | T val;
|
|---|
| 207 | in >> std::hex >> val;
|
|---|
| 208 | rc.val = val;
|
|---|
| 209 | return in;
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | template<class T>
|
|---|
| 213 | inline po::typed_value<T> *var(T *ptr=0)
|
|---|
| 214 | { return po::value<T>(ptr); }
|
|---|
| 215 |
|
|---|
| 216 | template<class T>
|
|---|
| 217 | inline po::typed_value<T> *var(const T &val, T *ptr=0)
|
|---|
| 218 | { return po::value<T>(ptr)->default_value(val); }
|
|---|
| 219 |
|
|---|
| 220 | template<class T>
|
|---|
| 221 | inline po::typed_value<std::vector<T>> *vars()
|
|---|
| 222 | { return po::value<std::vector<T>>(); }
|
|---|
| 223 |
|
|---|
| 224 | inline po::typed_value<bool> *po_switch()
|
|---|
| 225 | { return po::bool_switch(); }
|
|---|
| 226 |
|
|---|
| 227 | inline po::typed_value<bool> *po_bool()
|
|---|
| 228 | { return po::value<bool>()->implicit_value(true)->default_value(false); }
|
|---|
| 229 |
|
|---|
| 230 | #endif
|
|---|