about summary refs log tree commit diff
path: root/third_party/nix/src/libutil/config.hh
blob: 81b1c80e0e9741d2b68cc7079ff7bba3259f284d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <map>
#include <set>

#include "libutil/types.hh"

#pragma once

namespace nix {

class Args;
class AbstractSetting;
class JSONPlaceholder;
class JSONObject;

class AbstractConfig {
 protected:
  StringMap unknownSettings;

  explicit AbstractConfig(const StringMap& initials = {})
      : unknownSettings(initials) {}

 public:
  virtual bool set(const std::string& name, const std::string& value) = 0;

  struct SettingInfo {
    std::string value;
    std::string description;
  };

  virtual void getSettings(std::map<std::string, SettingInfo>& res,
                           bool overridenOnly = false) = 0;

  void applyConfigFile(const Path& path);

  virtual void resetOverriden() = 0;

  virtual void toJSON(JSONObject& out) = 0;

  virtual void convertToArgs(Args& args, const std::string& category) = 0;

  void warnUnknownSettings();

  void reapplyUnknownSettings();
};

/* 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 : public AbstractConfig {
  friend class AbstractSetting;

 public:
  struct SettingData {
    bool isAlias;
    AbstractSetting* setting;
    SettingData(bool isAlias, AbstractSetting* setting)
        : isAlias(isAlias), setting(setting) {}
  };

  typedef std::map<std::string, SettingData> Settings;

 private:
  Settings _settings;

 public:
  explicit Config(const StringMap& initials = {}) : AbstractConfig(initials) {}

  bool set(const std::string& name, const std::string& value) override;

  void addSetting(AbstractSetting* setting);

  void getSettings(std::map<std::string, SettingInfo>& res,
                   bool overridenOnly = false) override;

  void resetOverriden() override;

  void toJSON(JSONObject& out) override;

  void convertToArgs(Args& args, const std::string& category) override;
};

class AbstractSetting {
  friend class Config;

 public:
  const std::string name;
  const std::string description;
  const std::set<std::string> aliases;

  int created = 123;

  bool overriden = false;

 protected:
  AbstractSetting(std::string name, std::string description,
                  std::set<std::string> aliases);

  virtual ~AbstractSetting() {
    // Check against a gcc miscompilation causing our constructor
    // not to run (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431).
    assert(created == 123);
  }

  virtual void set(const std::string& value) = 0;

  virtual std::string to_string() = 0;

  virtual void toJSON(JSONPlaceholder& out);

  virtual void convertToArg(Args& args, const std::string& category);

  bool isOverriden() { return overriden; }
};

/* A setting of type T. */
template <typename T>
class BaseSetting : public AbstractSetting {
 protected:
  T value;

 public:
  BaseSetting(const T& def, const std::string& name,
              const std::string& description,
              const std::set<std::string>& aliases = {})
      : AbstractSetting(name, description, aliases), value(def) {}

  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) { assign(v); }
  virtual void assign(const T& v) { value = v; }

  void set(const std::string& str) override;

  virtual void override(const T& v) {
    overriden = true;
    value = v;
  }

  std::string to_string() override;

  void convertToArg(Args& args, const std::string& category) override;

  void toJSON(JSONPlaceholder& out) override;
};

template <typename T>
std::ostream& operator<<(std::ostream& str, const BaseSetting<T>& opt) {
  str << (const T&)opt;
  return str;
}

template <typename T>
bool operator==(const T& v1, const BaseSetting<T>& v2) {
  return v1 == (const T&)v2;
}

template <typename T>
class Setting : public BaseSetting<T> {
 public:
  Setting(Config* options, const T& def, const std::string& name,
          const std::string& description,
          const std::set<std::string>& aliases = {})
      : BaseSetting<T>(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 BaseSetting<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 = {})
      : BaseSetting<Path>(def, name, description, aliases),
        allowEmpty(allowEmpty) {
    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); }
};

struct GlobalConfig : public AbstractConfig {
  using ConfigRegistrations = std::vector<Config*>;
  static ConfigRegistrations* configRegistrations;

  bool set(const std::string& name, const std::string& value) override;

  void getSettings(std::map<std::string, SettingInfo>& res,
                   bool overridenOnly = false) override;

  void resetOverriden() override;

  void toJSON(JSONObject& out) override;

  void convertToArgs(Args& args, const std::string& category) override;

  struct Register {
    explicit Register(Config* config);
  };
};

extern GlobalConfig globalConfig;

}  // namespace nix