mirror of
https://github.com/NixOS/nix.git
synced 2025-11-14 14:32:42 +01:00
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.
This commit is contained in:
parent
568a099c88
commit
2040240e23
16 changed files with 334 additions and 40 deletions
112
src/libutil/config.cc
Normal file
112
src/libutil/config.cc
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
#include "config.hh"
|
||||
#include "args.hh"
|
||||
|
||||
namespace nix {
|
||||
|
||||
void Config::set(const std::string & name, const std::string & value)
|
||||
{
|
||||
auto i = _settings.find(name);
|
||||
if (i == _settings.end())
|
||||
throw UsageError("unknown setting '%s'", name);
|
||||
i->second.setting->set(value);
|
||||
}
|
||||
|
||||
void Config::add(AbstractSetting * setting)
|
||||
{
|
||||
_settings.emplace(setting->name, Config::SettingData{false, setting});
|
||||
for (auto & alias : setting->aliases)
|
||||
_settings.emplace(alias, Config::SettingData{true, setting});
|
||||
|
||||
bool set = false;
|
||||
|
||||
auto i = initials.find(setting->name);
|
||||
if (i != initials.end()) {
|
||||
setting->set(i->second);
|
||||
initials.erase(i);
|
||||
set = true;
|
||||
}
|
||||
|
||||
for (auto & alias : setting->aliases) {
|
||||
auto i = initials.find(alias);
|
||||
if (i != initials.end()) {
|
||||
if (set)
|
||||
warn("setting '%s' is set, but it's an alias of '%s' which is also set",
|
||||
alias, setting->name);
|
||||
else {
|
||||
setting->set(i->second);
|
||||
initials.erase(i);
|
||||
set = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Config::warnUnused()
|
||||
{
|
||||
for (auto & i : initials)
|
||||
warn("unknown setting '%s'", i.first);
|
||||
}
|
||||
|
||||
std::string Config::dump()
|
||||
{
|
||||
std::string res;
|
||||
for (auto & opt : _settings)
|
||||
if (!opt.second.isAlias)
|
||||
res += opt.first + " = " + opt.second.setting->to_string() + "\n";
|
||||
return res;
|
||||
}
|
||||
|
||||
AbstractSetting::AbstractSetting(
|
||||
const std::string & name,
|
||||
const std::string & description,
|
||||
const std::set<std::string> & aliases)
|
||||
: name(name), description(description), aliases(aliases)
|
||||
{
|
||||
}
|
||||
|
||||
template<> void Setting<std::string>::set(const std::string & str)
|
||||
{
|
||||
value = str;
|
||||
}
|
||||
|
||||
template<> std::string Setting<std::string>::to_string()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
template<> void Setting<int>::set(const std::string & str)
|
||||
{
|
||||
try {
|
||||
value = std::stoi(str);
|
||||
} catch (...) {
|
||||
throw UsageError("setting '%s' has invalid value '%s'", name, str);
|
||||
}
|
||||
}
|
||||
|
||||
template<> std::string Setting<int>::to_string()
|
||||
{
|
||||
return std::to_string(value);
|
||||
}
|
||||
|
||||
template<> void Setting<bool>::set(const std::string & str)
|
||||
{
|
||||
value = str == "true" || str == "1";
|
||||
}
|
||||
|
||||
template<> std::string Setting<bool>::to_string()
|
||||
{
|
||||
return value ? "true" : "false";
|
||||
}
|
||||
|
||||
void PathSetting::set(const std::string & str)
|
||||
{
|
||||
if (str == "") {
|
||||
if (allowEmpty)
|
||||
value = "";
|
||||
else
|
||||
throw UsageError("setting '%s' cannot be empty", name);
|
||||
} else
|
||||
value = canonPath(str);
|
||||
}
|
||||
|
||||
}
|
||||
151
src/libutil/config.hh
Normal file
151
src/libutil/config.hh
Normal file
|
|
@ -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; }
|
||||
};
|
||||
|
||||
}
|
||||
|
|
@ -7,6 +7,7 @@
|
|||
#include <list>
|
||||
#include <set>
|
||||
#include <memory>
|
||||
#include <map>
|
||||
|
||||
#include <boost/format.hpp>
|
||||
|
||||
|
|
@ -141,6 +142,7 @@ private:
|
|||
|
||||
typedef list<string> Strings;
|
||||
typedef set<string> StringSet;
|
||||
typedef std::map<std::string, std::string> StringMap;
|
||||
|
||||
|
||||
/* Paths are just strings. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue