2017-04-13 13:55:38 +00:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "types.hh"
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace nix {
|
|
|
|
|
|
|
|
class Args;
|
|
|
|
class AbstractSetting;
|
2017-04-20 15:34:47 +00:00
|
|
|
class JSONPlaceholder;
|
|
|
|
class JSONObject;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
public:
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
struct SettingData
|
|
|
|
{
|
2017-05-03 14:08:48 +00:00
|
|
|
bool isAlias;
|
2017-04-13 13:55:38 +00:00
|
|
|
AbstractSetting * setting;
|
2017-05-03 14:08:48 +00:00
|
|
|
SettingData(bool isAlias, AbstractSetting * setting)
|
|
|
|
: isAlias(isAlias), setting(setting)
|
|
|
|
{ }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
typedef std::map<std::string, SettingData> Settings;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
Settings _settings;
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
StringMap initials;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Config(const StringMap & initials)
|
|
|
|
: initials(initials)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
void set(const std::string & name, const std::string & value);
|
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
void addSetting(AbstractSetting * setting);
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
void warnUnknownSettings();
|
2017-04-13 13:55:38 +00:00
|
|
|
|
2017-04-20 12:58:16 +00:00
|
|
|
StringMap getSettings(bool overridenOnly = false);
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2017-06-07 14:49:54 +00:00
|
|
|
const Settings & _getSettings() { return _settings; }
|
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
void applyConfigFile(const Path & path, bool fatal = false);
|
2017-04-20 12:58:16 +00:00
|
|
|
|
|
|
|
void resetOverriden();
|
2017-04-20 15:34:47 +00:00
|
|
|
|
|
|
|
void toJSON(JSONObject & out);
|
2017-06-07 14:17:17 +00:00
|
|
|
|
|
|
|
void convertToArgs(Args & args);
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class AbstractSetting
|
|
|
|
{
|
|
|
|
friend class Config;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
const std::string name;
|
|
|
|
const std::string description;
|
|
|
|
const std::set<std::string> aliases;
|
|
|
|
|
|
|
|
int created = 123;
|
|
|
|
|
2017-04-20 12:58:16 +00:00
|
|
|
bool overriden = false;
|
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
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
|
2017-04-20 12:58:16 +00:00
|
|
|
// not to run (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80431).
|
2017-04-13 13:55:38 +00:00
|
|
|
assert(created == 123);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void set(const std::string & value) = 0;
|
|
|
|
|
|
|
|
virtual std::string to_string() = 0;
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2017-04-20 15:34:47 +00:00
|
|
|
virtual void toJSON(JSONPlaceholder & out);
|
|
|
|
|
2017-06-07 14:17:17 +00:00
|
|
|
virtual void convertToArg(Args & args);
|
|
|
|
|
2017-04-20 12:58:16 +00:00
|
|
|
bool isOverriden() { return overriden; }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* A setting of type T. */
|
2017-04-20 14:52:53 +00:00
|
|
|
template<typename T>
|
|
|
|
class BaseSetting : public AbstractSetting
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
protected:
|
|
|
|
|
|
|
|
T value;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-04-20 14:52:53 +00:00
|
|
|
BaseSetting(const T & def,
|
2017-04-13 13:55:38 +00:00
|
|
|
const std::string & name,
|
|
|
|
const std::string & description,
|
|
|
|
const std::set<std::string> & aliases = {})
|
|
|
|
: AbstractSetting(name, description, aliases)
|
|
|
|
, value(def)
|
2017-04-20 14:52:53 +00:00
|
|
|
{ }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
operator const T &() const { return value; }
|
2017-04-13 18:53:23 +00:00
|
|
|
operator T &() { return value; }
|
|
|
|
const T & get() const { return value; }
|
2017-04-13 13:55:38 +00:00
|
|
|
bool operator ==(const T & v2) const { return value == v2; }
|
|
|
|
bool operator !=(const T & v2) const { return value != v2; }
|
2017-04-20 14:52:53 +00:00
|
|
|
void operator =(const T & v) { assign(v); }
|
|
|
|
virtual void assign(const T & v) { value = v; }
|
2017-04-13 13:55:38 +00:00
|
|
|
|
|
|
|
void set(const std::string & str) override;
|
|
|
|
|
|
|
|
std::string to_string() override;
|
2017-04-20 15:34:47 +00:00
|
|
|
|
2017-06-07 14:17:17 +00:00
|
|
|
void convertToArg(Args & args) override;
|
|
|
|
|
2017-04-20 15:34:47 +00:00
|
|
|
void toJSON(JSONPlaceholder & out) override;
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2017-04-20 14:52:53 +00:00
|
|
|
std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
str << (const T &) opt;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2017-04-13 18:53:23 +00:00
|
|
|
template<typename T>
|
2017-04-20 14:52:53 +00:00
|
|
|
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); }
|
|
|
|
};
|
2017-04-13 18:53:23 +00:00
|
|
|
|
2017-04-13 13:55:38 +00:00
|
|
|
/* A special setting for Paths. These are automatically canonicalised
|
|
|
|
(e.g. "/foo//bar/" becomes "/foo/bar"). */
|
2017-04-20 14:52:53 +00:00
|
|
|
class PathSetting : public BaseSetting<Path>
|
2017-04-13 13:55:38 +00:00
|
|
|
{
|
|
|
|
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 = {})
|
2017-04-20 14:52:53 +00:00
|
|
|
: BaseSetting<Path>(def, name, description, aliases)
|
2017-04-13 13:55:38 +00:00
|
|
|
, allowEmpty(allowEmpty)
|
|
|
|
{
|
2017-04-20 14:52:53 +00:00
|
|
|
options->addSetting(this);
|
2017-04-13 13:55:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void set(const std::string & str) override;
|
|
|
|
|
|
|
|
Path operator +(const char * p) const { return value + p; }
|
2017-04-20 14:52:53 +00:00
|
|
|
|
|
|
|
void operator =(const Path & v) { this->assign(v); }
|
2017-04-13 13:55:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|