From 153372211c242e539754680b5fe979e3be61ebfe Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 25 Sep 2013 14:54:46 +0300 Subject: [PATCH] Base config options directly on features There are no standalone options anymore, so might as well streamline things a bit. --- source/config.cpp | 24 +++++++++++++----------- source/config.h | 12 +++++------- source/feature.cpp | 4 ++-- source/feature.h | 4 ++-- source/sourcepackage.cpp | 12 ++++++------ 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/source/config.cpp b/source/config.cpp index 75784e3..69abe9d 100644 --- a/source/config.cpp +++ b/source/config.cpp @@ -16,12 +16,14 @@ Config::Config(SourcePackage &p): changed(false) { } -void Config::add_option(const string &n, const string &v, const string &d) +const Config::Option &Config::add_option(const Feature &f) { - Option opt(n, v, d); - if(pending_options.count(n)) - opt.value = pending_options[n]; - options.insert(OptionMap::value_type(n, opt)); + Option opt(f); + InputOptions::const_iterator i = pending_options.find(opt.name); + if(i!=pending_options.end()) + opt.value = i->second; + + return options.insert(OptionMap::value_type(opt.name, opt)).first->second; } bool Config::set_option(const string &opt, const string &val) @@ -85,12 +87,12 @@ void Config::save() const } -Config::Option::Option(const string &n, const string &v, const string &d): - name(n), - default_value(v), - description(d), - value(v) -{ } +Config::Option::Option(const Feature &f): + Feature(f), + value(default_value) +{ + name = "with_"+name; +} Config::Loader::Loader(Config &c): diff --git a/source/config.h b/source/config.h index 9499775..1f7fa71 100644 --- a/source/config.h +++ b/source/config.h @@ -6,6 +6,7 @@ #include #include #include +#include "feature.h" class SourcePackage; @@ -17,14 +18,11 @@ class Config { public: /** A single configuration option. */ - struct Option + struct Option: public Feature { - std::string name; - std::string default_value; - std::string description; std::string value; - Option(const std::string &, const std::string &, const std::string &); + Option(const Feature &); }; typedef std::map OptionMap; @@ -48,8 +46,8 @@ private: public: Config(SourcePackage &); - /** Adds a configuration option with name, default value and description. */ - void add_option(const std::string &, const std::string &, const std::string &); + /** Adds a configuration option based on a feature. */ + const Option &add_option(const Feature &); bool set_option(const std::string &, const std::string &); diff --git a/source/feature.cpp b/source/feature.cpp index 5f0ea5f..a483482 100644 --- a/source/feature.cpp +++ b/source/feature.cpp @@ -3,6 +3,6 @@ Feature::Loader::Loader(Feature &f): Msp::DataFile::ObjectLoader(f) { - add("description", &Feature::descr); - add("default", &Feature::def_value); + add("description", &Feature::description); + add("default", &Feature::default_value); } diff --git a/source/feature.h b/source/feature.h index 8d496b3..3c13a5a 100644 --- a/source/feature.h +++ b/source/feature.h @@ -12,8 +12,8 @@ struct Feature }; std::string name; - std::string descr; - std::string def_value; + std::string description; + std::string default_value; Feature(const std::string &n): name(n) { } }; diff --git a/source/sourcepackage.cpp b/source/sourcepackage.cpp index e5a2fa7..515841d 100644 --- a/source/sourcepackage.cpp +++ b/source/sourcepackage.cpp @@ -188,17 +188,17 @@ void SourcePackage::Loader::finish() void SourcePackage::Loader::feature(const string &n, const string &d) { Feature feat(n); - feat.descr = d; - feat.def_value = "no"; + feat.description = d; + feat.default_value = "no"; load_sub(feat); obj.features.push_back(feat); - string config_key = "with_"+feat.name; - obj.config.add_option(config_key, feat.def_value, feat.descr); + + const Config::Option &opt = obj.config.add_option(feat); if(options) { - Config::InputOptions::const_iterator i = options->find(config_key); + Config::InputOptions::const_iterator i = options->find(opt.name); if(i!=options->end()) - obj.config.set_option(config_key, i->second); + obj.config.set_option(opt.name, i->second); } } -- 2.43.0