aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/config.hh
diff options
context:
space:
mode:
authorEelco Dolstra <edolstra@gmail.com>2017-04-13 15:55:38 +0200
committerEelco Dolstra <edolstra@gmail.com>2017-04-13 16:03:31 +0200
commit2040240e238a41c2eb799bf4dbf394fec297ac16 (patch)
tree73661117f25db0f21cf7497ad43c6cb2766a7a05 /src/libutil/config.hh
parent568a099c889e7ccc5a49b15575078e99acf8bc2f (diff)
Add a Config class to simplify adding configuration settings
The typical use is to inherit Config and add Setting<T> members: class MyClass : private Config { Setting<int> foo{this, 123, "foo", "the number of foos to use"}; Setting<std::string> bar{this, "blabla", "bar", "the name of the bar"}; MyClass() : Config(readConfigFile("/etc/my-app.conf")) { std::cout << foo << "\n"; // will print 123 unless overriden } }; Currently, this is used by Store and its subclasses for store parameters. You now get a warning if you specify a non-existant store parameter in a store URI.
Diffstat (limited to 'src/libutil/config.hh')
-rw-r--r--src/libutil/config.hh151
1 files changed, 151 insertions, 0 deletions
diff --git a/src/libutil/config.hh b/src/libutil/config.hh
new file mode 100644
index 000000000..fb2d48e9c
--- /dev/null
+++ b/src/libutil/config.hh
@@ -0,0 +1,151 @@
+#include <map>
+#include <set>
+
+#include "types.hh"
+
+#pragma once
+
+namespace nix {
+
+class Args;
+class AbstractSetting;
+
+/* A class to simplify providing configuration settings. The typical
+ use is to inherit Config and add Setting<T> members:
+
+ class MyClass : private Config
+ {
+ Setting<int> foo{this, 123, "foo", "the number of foos to use"};
+ Setting<std::string> bar{this, "blabla", "bar", "the name of the bar"};
+
+ MyClass() : Config(readConfigFile("/etc/my-app.conf"))
+ {
+ std::cout << foo << "\n"; // will print 123 unless overriden
+ }
+ };
+*/
+
+class Config
+{
+ friend class AbstractSetting;
+
+ struct SettingData
+ {
+ bool isAlias = false;
+ AbstractSetting * setting;
+ };
+
+ std::map<std::string, SettingData> _settings;
+
+ StringMap initials;
+
+public:
+
+ Config(const StringMap & initials)
+ : initials(initials)
+ { }
+
+ void set(const std::string & name, const std::string & value);
+
+ void add(AbstractSetting * setting);
+
+ void warnUnused();
+
+ std::string dump();
+};
+
+class AbstractSetting
+{
+ friend class Config;
+
+public:
+
+ const std::string name;
+ const std::string description;
+ const std::set<std::string> aliases;
+
+ int created = 123;
+
+protected:
+
+ AbstractSetting(
+ const std::string & name,
+ const std::string & description,
+ const std::set<std::string> & aliases);
+
+ virtual ~AbstractSetting()
+ {
+ // Check against a gcc miscompilation causing our constructor
+ // not to run.
+ assert(created == 123);
+ }
+
+ virtual void set(const std::string & value) = 0;
+
+ virtual std::string to_string() = 0;
+};
+
+/* A setting of type T. */
+template<typename T>
+class Setting : public AbstractSetting
+{
+protected:
+
+ T value;
+
+public:
+
+ Setting(Config * options,
+ const T & def,
+ const std::string & name,
+ const std::string & description,
+ const std::set<std::string> & aliases = {})
+ : AbstractSetting(name, description, aliases)
+ , value(def)
+ {
+ options->add(this);
+ }
+
+ operator const T &() 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 set(const std::string & str) override;
+
+ std::string to_string() override;
+};
+
+template<typename T>
+std::ostream & operator <<(std::ostream & str, const Setting<T> & opt)
+{
+ str << (const T &) opt;
+ return str;
+}
+
+/* A special setting for Paths. These are automatically canonicalised
+ (e.g. "/foo//bar/" becomes "/foo/bar"). */
+class PathSetting : public Setting<Path>
+{
+ bool allowEmpty;
+
+public:
+
+ PathSetting(Config * options,
+ bool allowEmpty,
+ const Path & def,
+ const std::string & name,
+ const std::string & description,
+ const std::set<std::string> & aliases = {})
+ : Setting<Path>(options, def, name, description, aliases)
+ , allowEmpty(allowEmpty)
+ {
+ set(value);
+ }
+
+ void set(const std::string & str) override;
+
+ Path operator +(const char * p) const { return value + p; }
+};
+
+}