about summary refs log tree commit diff
path: root/src/libutil/config.hh
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-04-13T18·53+0200
committerEelco Dolstra <edolstra@gmail.com>2017-04-13T18·53+0200
commitba9ad29fdbfda3836bb06b35817f08fd10beaa22 (patch)
tree565646143793af4e91ee88630e667bb7976e8686 /src/libutil/config.hh
parent6bd9576aeb55927cb551736a47b4e8e3fd1063bb (diff)
Convert Settings to the new config system
This makes all config options self-documenting.

Unknown or unparseable config settings and --option flags now cause a
warning.
Diffstat (limited to 'src/libutil/config.hh')
-rw-r--r--src/libutil/config.hh22
1 files changed, 17 insertions, 5 deletions
diff --git a/src/libutil/config.hh b/src/libutil/config.hh
index fb2d48e9c8..6c8612f675 100644
--- a/src/libutil/config.hh
+++ b/src/libutil/config.hh
@@ -47,11 +47,13 @@ public:
 
     void set(const std::string & name, const std::string & value);
 
-    void add(AbstractSetting * setting);
+    void addSetting(AbstractSetting * setting);
 
-    void warnUnused();
+    void warnUnknownSettings();
 
-    std::string dump();
+    StringMap getSettings();
+
+    void applyConfigFile(const Path & path, bool fatal = false);
 };
 
 class AbstractSetting
@@ -83,10 +85,15 @@ protected:
     virtual void set(const std::string & value) = 0;
 
     virtual std::string to_string() = 0;
+
+    bool parseBool(const std::string & str);
+    std::string printBool(bool b);
 };
 
+struct DefaultSettingTag { };
+
 /* A setting of type T. */
-template<typename T>
+template<typename T, typename Tag = DefaultSettingTag>
 class Setting : public AbstractSetting
 {
 protected:
@@ -103,10 +110,12 @@ public:
         : AbstractSetting(name, description, aliases)
         , value(def)
     {
-        options->add(this);
+        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; }
@@ -123,6 +132,9 @@ std::ostream & operator <<(std::ostream & str, const Setting<T> & opt)
     return str;
 }
 
+template<typename T>
+bool operator ==(const T & v1, const Setting<T> & v2) { return v1 == (const T &) v2; }
+
 /* A special setting for Paths. These are automatically canonicalised
    (e.g. "/foo//bar/" becomes "/foo/bar"). */
 class PathSetting : public Setting<Path>