From 4410e9d995bcd53a7a4cff0bbee3917375adcba3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 20 Apr 2017 16:52:53 +0200 Subject: Setting: Remove "Tag" template argument --- src/libutil/config.hh | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) (limited to 'src/libutil/config.hh') diff --git a/src/libutil/config.hh b/src/libutil/config.hh index 952bf04b8..130f59e2b 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -90,17 +90,12 @@ protected: virtual std::string to_string() = 0; - bool parseBool(const std::string & str); - std::string printBool(bool b); - bool isOverriden() { return overriden; } }; -struct DefaultSettingTag { }; - /* A setting of type T. */ -template -class Setting : public AbstractSetting +template +class BaseSetting : public AbstractSetting { protected: @@ -108,23 +103,21 @@ protected: public: - Setting(Config * options, - const T & def, + BaseSetting(const T & def, const std::string & name, const std::string & description, const std::set & aliases = {}) : AbstractSetting(name, description, aliases) , value(def) - { - options->addSetting(this); - } + { } operator const T &() const { return value; } operator T &() { return value; } const T & get() const { return value; } bool operator ==(const T & v2) const { return value == v2; } bool operator !=(const T & v2) const { return value != v2; } - void operator =(const T & v) { value = v; } + void operator =(const T & v) { assign(v); } + virtual void assign(const T & v) { value = v; } void set(const std::string & str) override; @@ -132,18 +125,35 @@ public: }; template -std::ostream & operator <<(std::ostream & str, const Setting & opt) +std::ostream & operator <<(std::ostream & str, const BaseSetting & opt) { str << (const T &) opt; return str; } template -bool operator ==(const T & v1, const Setting & v2) { return v1 == (const T &) v2; } +bool operator ==(const T & v1, const BaseSetting & v2) { return v1 == (const T &) v2; } + +template +class Setting : public BaseSetting +{ +public: + Setting(Config * options, + const T & def, + const std::string & name, + const std::string & description, + const std::set & aliases = {}) + : BaseSetting(def, name, description, aliases) + { + options->addSetting(this); + } + + void operator =(const T & v) { this->assign(v); } +}; /* A special setting for Paths. These are automatically canonicalised (e.g. "/foo//bar/" becomes "/foo/bar"). */ -class PathSetting : public Setting +class PathSetting : public BaseSetting { bool allowEmpty; @@ -155,15 +165,17 @@ public: const std::string & name, const std::string & description, const std::set & aliases = {}) - : Setting(options, def, name, description, aliases) + : BaseSetting(def, name, description, aliases) , allowEmpty(allowEmpty) { - set(value); + options->addSetting(this); } void set(const std::string & str) override; Path operator +(const char * p) const { return value + p; } + + void operator =(const Path & v) { this->assign(v); } }; } -- cgit v1.2.3