diff options
Diffstat (limited to 'project2/common/environment.cpp')
-rw-r--r-- | project2/common/environment.cpp | 92 |
1 files changed, 43 insertions, 49 deletions
diff --git a/project2/common/environment.cpp b/project2/common/environment.cpp index 3106e9a..18d60a0 100644 --- a/project2/common/environment.cpp +++ b/project2/common/environment.cpp @@ -1,5 +1,6 @@ #include <pch.hpp> #include "environment.h" +#include "optionsSource.h" #include "loggers.h" #include "xmlObjectLoader.h" #include <stdio.h> @@ -8,73 +9,66 @@ #include <boost/foreach.hpp> #include <boost/bind.hpp> -namespace po = boost::program_options; - const Environment * Environment::currentEnv(NULL); int Environment::clLevel(-1); int Environment::slLevel(-1); -bool Environment::optionsBuilt(false); -po::options_description Environment::allOptions("Project2 options"); -po::positional_options_description Environment::posOptions; -Environment::Environment(int c, char ** v) : - argc(c), - argv(v) +Environment::Environment() : + commonOptions("Project2 Common options") { currentEnv = this; + commonOptions + ("common.syslogLevel", Options::value(&slLevel, -1), + "Log to syslog with level <arg> (default OFF)")("s") + ("common.consolelogLevel", Options::value(&clLevel, LOG_WARNING), + "Log to console with level <arg> (default WARNING)")("c") + ("common.datasourceRoot", Options::value(&datasourceRoot, "datasources"), + "The folder in which to find datasource definitions") + ("common.namespace", Options::value(&xmlNamespace, "http://project2.randomdan.homeip.net"), + "The namespace to use for Project2 components and responses") + ("common.namespacePrefix", Options::value(&xmlPrefix, "project2"), + "The namespace prefix to use for the Project2 namespace") + ("common.sessionTimeOut", Options::value(&sessionTimeOut, 3600), + "The time after which idle sessions are forgetten") + ; } +typedef std::vector<const Options *> AllOptions; + static -po::variables_map -injectSettingsHelper(int argc, char ** argv, const po::options_description * opts, po::positional_options_description * posOpts) +void +optionsHelper(AllOptions * ao, const Options * options) { - po::variables_map settings; - if (!opts) { - return settings; - } - if (argc > 0 && argv != NULL) { - if (posOpts) { - po::store(po::command_line_parser(argc, argv).options(*opts).positional(*posOpts).allow_unregistered().run(), settings); - } - else { - po::store(po::command_line_parser(argc, argv).options(*opts).allow_unregistered().run(), settings); - } + if (options) { + ao->push_back(options); } - po::store(po::parse_environment(*opts, "P2_"), settings); - if (boost::filesystem::exists(".p2config")) { - std::ifstream f(".p2config"); - po::store(po::parse_config_file(f, *opts, true), settings); +} + +static +void +consumeIntoAll(const AllOptions * ao, const Glib::ustring & n, const Glib::ustring & p, const Glib::ustring & v) +{ + BOOST_FOREACH(const Options * o, *ao) { + o->consume(n, p, v); } - po::notify(settings); - return settings; } void Environment::init() { - if (!optionsBuilt) { - po::options_description common("Project2 Common options"); - common.add_options() - ("sysloglevel,s", po::value(&slLevel)->default_value(-1), - "Log to syslog with level <arg> (default OFF)") - ("consoleloglevel,c", po::value(&clLevel)->default_value(LOG_WARNING), - "Log to console with level <arg> (default WARNING)") - ("datasourceroot", boost::program_options::value(&datasourceRoot)->default_value("datasources"), - "The folder in which to find datasource definitions") - ("xmlnamespace", boost::program_options::value(&xmlNamespace)->default_value("http://project2.randomdan.homeip.net"), - "The XML namespace to use for Project2 components and responses") - ("xmlprefix", boost::program_options::value(&xmlPrefix)->default_value("project2"), - "The XML namespace prefix to use for the Project2 XML namespace") - ("sessionTimeOut", boost::program_options::value(&sessionTimeOut)->default_value(3600), - "The time after which idle sessions are forgetten") - ; - allOptions.add(common).add(addOptions(posOptions)); - optionsBuilt = true; + AllOptions allOptions; + allOptions.push_back(&commonOptions); + allOptions.push_back(&engineOptions()); + LoaderBase::onAllComponents(boost::bind(optionsHelper, &allOptions, boost::bind(&ComponentLoader::options, _1))); + + BOOST_FOREACH(const AllOptions::value_type & v, allOptions) { + v->reset(); + } + + typedef std::map<std::string, boost::shared_ptr<OptionsSourceLoader> > ConfigParsersMap; + BOOST_FOREACH(const ConfigParsersMap::value_type & cp, *LoaderBase::objLoaders<OptionsSourceLoader>()) { + cp.second->create()->loadInto(boost::bind(&consumeIntoAll, &allOptions, _1, _2, _3)); } - po::variables_map settings(injectSettingsHelper(argc, argv, &allOptions, &posOptions)); - postinit(allOptions, settings); - LoaderBase::onAllComponents(boost::bind( - injectSettingsHelper, argc, argv, boost::bind(&ComponentLoader::options, _1), (po::positional_options_description*)NULL)); Logger()->clear(); if (clLevel >= 0) { |