Browse Source

fixed some documentation and changed position of const in Settings class.

main
gereon 13 years ago
parent
commit
a8517c7246
  1. 36
      src/utility/Settings.cpp
  2. 26
      src/utility/Settings.h

36
src/utility/Settings.cpp

@ -21,7 +21,7 @@ namespace settings {
namespace bpo = boost::program_options; namespace bpo = boost::program_options;
/* /*
* static initializers * Static initializers.
*/ */
std::unique_ptr<bpo::options_description> storm::settings::Settings::desc = nullptr; std::unique_ptr<bpo::options_description> storm::settings::Settings::desc = nullptr;
std::string storm::settings::Settings::binaryName = ""; std::string storm::settings::Settings::binaryName = "";
@ -42,17 +42,17 @@ std::map< std::pair<std::string, std::string>, std::shared_ptr<bpo::options_desc
* @param argv should be argv passed to main function * @param argv should be argv passed to main function
* @param filename either nullptr or name of config file * @param filename either nullptr or name of config file
*/ */
Settings::Settings(const int argc, const char* argv[], const char* filename) { Settings::Settings(int const argc, char const * const argv[], char const * const filename) {
Settings::binaryName = std::string(argv[0]); Settings::binaryName = std::string(argv[0]);
try { try {
// Initially fill description objects // Initially fill description objects.
this->initDescriptions(); this->initDescriptions();
// Take care of positional arguments // Take care of positional arguments.
Settings::positional.add("trafile", 1); Settings::positional.add("trafile", 1);
Settings::positional.add("labfile", 1); Settings::positional.add("labfile", 1);
// Check module triggers, add corresponding options // Check module triggers, add corresponding options.
std::map< std::string, std::list< std::string > > options; std::map< std::string, std::list< std::string > > options;
for (auto it : Settings::modules) { for (auto it : Settings::modules) {
@ -67,12 +67,12 @@ Settings::Settings(const int argc, const char* argv[], const char* filename) {
; ;
} }
// Perform first parse run // Perform first parse run.
this->firstRun(argc, argv, filename); this->firstRun(argc, argv, filename);
// Buffer for items to be deleted // Buffer for items to be deleted.
std::list< std::pair< std::string, std::string > > deleteQueue; std::list< std::pair< std::string, std::string > > deleteQueue;
// Check module triggers // Check module triggers.
for (auto it : Settings::modules) { for (auto it : Settings::modules) {
std::pair< std::string, std::string > trigger = it.first; std::pair< std::string, std::string > trigger = it.first;
if (this->vm.count(trigger.first)) { if (this->vm.count(trigger.first)) {
@ -84,16 +84,15 @@ Settings::Settings(const int argc, const char* argv[], const char* filename) {
} }
for (auto it : deleteQueue) Settings::modules.erase(it); for (auto it : deleteQueue) Settings::modules.erase(it);
// Stop if help is set.
// Stop if help is set
if (this->vm.count("help") > 0) { if (this->vm.count("help") > 0) {
return; return;
} }
// Perform second run // Perform second run.
this->secondRun(argc, argv, filename); this->secondRun(argc, argv, filename);
// Finalize parsed options, check for specified requirements // Finalize parsed options, check for specified requirements.
bpo::notify(this->vm); bpo::notify(this->vm);
LOG4CPLUS_DEBUG(logger, "Finished loading config."); LOG4CPLUS_DEBUG(logger, "Finished loading config.");
} }
@ -117,7 +116,6 @@ Settings::Settings(const int argc, const char* argv[], const char* filename) {
/*! /*!
* Initially fill options_description objects. * Initially fill options_description objects.
* First puts some generic options, then calls all register Callbacks.
*/ */
void Settings::initDescriptions() { void Settings::initDescriptions() {
LOG4CPLUS_DEBUG(logger, "Initializing descriptions."); LOG4CPLUS_DEBUG(logger, "Initializing descriptions.");
@ -141,13 +139,13 @@ void Settings::initDescriptions() {
* given), but allow for unregistered options, do not check requirements * given), but allow for unregistered options, do not check requirements
* from options_description objects, do not check positional arguments. * from options_description objects, do not check positional arguments.
*/ */
void Settings::firstRun(const int argc, const char* argv[], const char* filename) { void Settings::firstRun(int const argc, char const * const argv[], char const * const filename) {
LOG4CPLUS_DEBUG(logger, "Performing first run."); LOG4CPLUS_DEBUG(logger, "Performing first run.");
// parse command line // Parse command line.
bpo::store(bpo::command_line_parser(argc, argv).options(*(Settings::desc)).allow_unregistered().run(), this->vm); bpo::store(bpo::command_line_parser(argc, argv).options(*(Settings::desc)).allow_unregistered().run(), this->vm);
/* /*
* load config file if specified * Load config file if specified.
*/ */
if (this->vm.count("configfile")) { if (this->vm.count("configfile")) {
bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), *(Settings::desc)), this->vm, true); bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), *(Settings::desc)), this->vm, true);
@ -161,12 +159,12 @@ void Settings::firstRun(const int argc, const char* argv[], const char* filename
* given) and check for unregistered options, requirements from * given) and check for unregistered options, requirements from
* options_description objects and positional arguments. * options_description objects and positional arguments.
*/ */
void Settings::secondRun(const int argc, const char* argv[], const char* filename) { void Settings::secondRun(int const argc, char const * const argv[], char const * const filename) {
LOG4CPLUS_DEBUG(logger, "Performing second run."); LOG4CPLUS_DEBUG(logger, "Performing second run.");
// Parse command line // Parse command line.
bpo::store(bpo::command_line_parser(argc, argv).options(*(Settings::desc)).positional(this->positional).run(), this->vm); bpo::store(bpo::command_line_parser(argc, argv).options(*(Settings::desc)).positional(this->positional).run(), this->vm);
/* /*
* load config file if specified * Load config file if specified.
*/ */
if (this->vm.count("configfile")) { if (this->vm.count("configfile")) {
bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), *(Settings::desc)), this->vm, true); bpo::store(bpo::parse_config_file<char>(this->vm["configfile"].as<std::string>().c_str(), *(Settings::desc)), this->vm, true);

26
src/utility/Settings.h

@ -51,7 +51,7 @@ namespace settings {
* @brief Get value of a generic option. * @brief Get value of a generic option.
*/ */
template <typename T> template <typename T>
const T& get(const std::string &name) const { inline const T& get( std::string const & name) const {
if (this->vm.count(name) == 0) throw storm::exceptions::InvalidSettingsException() << "Could not read option " << name << "."; if (this->vm.count(name) == 0) throw storm::exceptions::InvalidSettingsException() << "Could not read option " << name << ".";
return this->vm[name].as<T>(); return this->vm[name].as<T>();
} }
@ -59,14 +59,14 @@ namespace settings {
/*! /*!
* @brief Get value of string option * @brief Get value of string option
*/ */
const std::string& getString(const std::string &name) const { inline const std::string& getString(std::string const & name) const {
return this->get<std::string>(name); return this->get<std::string>(name);
} }
/*! /*!
* @brief Check if an option is set * @brief Check if an option is set
*/ */
const bool isSet(const std::string &name) const { inline const bool isSet(std::string const & name) const {
return this->vm.count(name) > 0; return this->vm.count(name) > 0;
} }
@ -107,28 +107,28 @@ namespace settings {
*/ */
template <typename T> template <typename T>
static void registerModule() { static void registerModule() {
// get trigger // Get trigger values.
std::pair< std::string, std::string > trigger = T::getOptionTrigger(); std::pair< std::string, std::string > trigger = T::getOptionTrigger();
// build description name // Build description name.
std::stringstream str; std::stringstream str;
str << "Options for " << T::getModuleName() << " (" << trigger.first << " = " << trigger.second << ")"; str << "Options for " << T::getModuleName() << " (" << trigger.first << " = " << trigger.second << ")";
std::shared_ptr<bpo::options_description> desc = std::shared_ptr<bpo::options_description>(new bpo::options_description(str.str())); std::shared_ptr<bpo::options_description> desc = std::shared_ptr<bpo::options_description>(new bpo::options_description(str.str()));
// but options // Put options into description.
T::putOptions(desc.get()); T::putOptions(desc.get());
// store // Store module.
Settings::modules[ trigger ] = desc; Settings::modules[ trigger ] = desc;
} }
friend std::ostream& help(std::ostream& os); friend std::ostream& help(std::ostream& os);
friend std::ostream& helpConfigfile(std::ostream& os); friend std::ostream& helpConfigfile(std::ostream& os);
friend Settings* instance(); friend Settings* instance();
friend Settings* newInstance(const int argc, const char* argv[], const char* filename); friend Settings* newInstance(int const argc, char const * const argv[], char const * const filename);
private: private:
/*! /*!
* @brief Constructor. * @brief Constructor.
*/ */
Settings(const int argc, const char* argv[], const char* filename); Settings(int const argc, char const * const argv[], char const * const filename);
/*! /*!
* @brief Initialize options_description object. * @brief Initialize options_description object.
@ -138,12 +138,12 @@ namespace settings {
/*! /*!
* @brief Perform first parser run * @brief Perform first parser run
*/ */
void firstRun(const int argc, const char* argv[], const char* filename); void firstRun(int const argc, char const * const argv[], char const * const filename);
/*! /*!
* @brief Perform second parser run. * @brief Perform second parser run.
*/ */
void secondRun(const int argc, const char* argv[], const char* filename); void secondRun(int const argc, char const * const argv[], char const * const filename);
/*! /*!
* @brief Option description for positional arguments on command line. * @brief Option description for positional arguments on command line.
@ -197,10 +197,10 @@ namespace settings {
* *
* @param argc should be argc passed to main function * @param argc should be argc passed to main function
* @param argv should be argv passed to main function * @param argv should be argv passed to main function
* @param filename either NULL or name of config file * @param filename either NULL or name of config file
* @return The new instance of Settings. * @return The new instance of Settings.
*/ */
inline Settings* newInstance(const int argc, const char* argv[], const char* filename) { inline Settings* newInstance(int const argc, char const * const argv[], char const * const filename) {
if (Settings::inst != nullptr) delete Settings::inst; if (Settings::inst != nullptr) delete Settings::inst;
Settings::inst = new Settings(argc, argv, filename); Settings::inst = new Settings(argc, argv, filename);
return Settings::inst; return Settings::inst;

|||||||
100:0
Loading…
Cancel
Save