Merge pull request #9191 from tfc/libutil-implementation

libutil: Small improvements
(cherry picked from commit edc07588ecd5053da57c2ab6c2543abf49730ce3)
Change-Id: I01a481d872d859b372a4bade9bebd3dab2fb4efb
This commit is contained in:
eldritch horrors 2024-03-04 04:47:18 +01:00
parent ab40b2c5d0
commit 4d9dde15ef
2 changed files with 55 additions and 31 deletions

View file

@ -9,6 +9,10 @@
namespace nix { namespace nix {
Config::Config(StringMap initials)
: AbstractConfig(std::move(initials))
{ }
bool Config::set(const std::string & name, const std::string & value) bool Config::set(const std::string & name, const std::string & value)
{ {
bool append = false; bool append = false;
@ -29,9 +33,9 @@ bool Config::set(const std::string & name, const std::string & value)
void Config::addSetting(AbstractSetting * setting) void Config::addSetting(AbstractSetting * setting)
{ {
_settings.emplace(setting->name, Config::SettingData(false, setting)); _settings.emplace(setting->name, Config::SettingData{false, setting});
for (auto & alias : setting->aliases) for (auto & alias : setting->aliases)
_settings.emplace(alias, Config::SettingData(true, setting)); _settings.emplace(alias, Config::SettingData{true, setting});
bool set = false; bool set = false;
@ -59,6 +63,10 @@ void Config::addSetting(AbstractSetting * setting)
} }
} }
AbstractConfig::AbstractConfig(StringMap initials)
: unknownSettings(std::move(initials))
{ }
void AbstractConfig::warnUnknownSettings() void AbstractConfig::warnUnknownSettings()
{ {
for (auto & s : unknownSettings) for (auto & s : unknownSettings)
@ -199,6 +207,13 @@ AbstractSetting::AbstractSetting(
{ {
} }
AbstractSetting::~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);
}
nlohmann::json AbstractSetting::toJSON() nlohmann::json AbstractSetting::toJSON()
{ {
return nlohmann::json(toJSONObject()); return nlohmann::json(toJSONObject());
@ -220,6 +235,9 @@ void AbstractSetting::convertToArg(Args & args, const std::string & category)
{ {
} }
bool AbstractSetting::isOverridden() const { return overridden; }
template<> std::string BaseSetting<std::string>::parse(const std::string & str) const template<> std::string BaseSetting<std::string>::parse(const std::string & str) const
{ {
return str; return str;
@ -385,11 +403,33 @@ static Path parsePath(const AbstractSetting & s, const std::string & str)
return canonPath(str); return canonPath(str);
} }
PathSetting::PathSetting(Config * options,
const Path & def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases)
: BaseSetting<Path>(def, true, name, description, aliases)
{
options->addSetting(this);
}
Path PathSetting::parse(const std::string & str) const Path PathSetting::parse(const std::string & str) const
{ {
return parsePath(*this, str); return parsePath(*this, str);
} }
OptionalPathSetting::OptionalPathSetting(Config * options,
const std::optional<Path> & def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases)
: BaseSetting<std::optional<Path>>(def, true, name, description, aliases)
{
options->addSetting(this);
}
std::optional<Path> OptionalPathSetting::parse(const std::string & str) const std::optional<Path> OptionalPathSetting::parse(const std::string & str) const
{ {
if (str == "") if (str == "")
@ -398,6 +438,11 @@ std::optional<Path> OptionalPathSetting::parse(const std::string & str) const
return parsePath(*this, str); return parsePath(*this, str);
} }
void OptionalPathSetting::operator =(const std::optional<Path> & v)
{
this->assign(v);
}
bool GlobalConfig::set(const std::string & name, const std::string & value) bool GlobalConfig::set(const std::string & name, const std::string & value)
{ {
for (auto & config : *configRegistrations) for (auto & config : *configRegistrations)

View file

@ -52,9 +52,7 @@ class AbstractConfig
protected: protected:
StringMap unknownSettings; StringMap unknownSettings;
AbstractConfig(const StringMap & initials = {}) AbstractConfig(StringMap initials = {});
: unknownSettings(initials)
{ }
public: public:
@ -150,9 +148,6 @@ public:
{ {
bool isAlias; bool isAlias;
AbstractSetting * setting; AbstractSetting * setting;
SettingData(bool isAlias, AbstractSetting * setting)
: isAlias(isAlias), setting(setting)
{ }
}; };
typedef std::map<std::string, SettingData> Settings; typedef std::map<std::string, SettingData> Settings;
@ -163,9 +158,7 @@ private:
public: public:
Config(const StringMap & initials = {}) Config(StringMap initials = {});
: AbstractConfig(initials)
{ }
bool set(const std::string & name, const std::string & value) override; bool set(const std::string & name, const std::string & value) override;
@ -206,12 +199,7 @@ protected:
const std::set<std::string> & aliases, const std::set<std::string> & aliases,
std::optional<ExperimentalFeature> experimentalFeature = std::nullopt); std::optional<ExperimentalFeature> experimentalFeature = std::nullopt);
virtual ~AbstractSetting() 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, bool append = false) = 0; virtual void set(const std::string & value, bool append = false) = 0;
@ -229,7 +217,7 @@ protected:
virtual void convertToArg(Args & args, const std::string & category); virtual void convertToArg(Args & args, const std::string & category);
bool isOverridden() const { return overridden; } bool isOverridden() const;
}; };
/** /**
@ -324,8 +312,7 @@ public:
template<typename T> template<typename T>
std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt) std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
{ {
str << (const T &) opt; return str << static_cast<const T &>(opt);
return str;
} }
template<typename T> template<typename T>
@ -365,11 +352,7 @@ public:
const Path & def, const Path & def,
const std::string & name, const std::string & name,
const std::string & description, const std::string & description,
const std::set<std::string> & aliases = {}) const std::set<std::string> & aliases = {});
: BaseSetting<Path>(def, true, name, description, aliases)
{
options->addSetting(this);
}
Path parse(const std::string & str) const override; Path parse(const std::string & str) const override;
@ -391,15 +374,11 @@ public:
const std::optional<Path> & def, const std::optional<Path> & def,
const std::string & name, const std::string & name,
const std::string & description, const std::string & description,
const std::set<std::string> & aliases = {}) const std::set<std::string> & aliases = {});
: BaseSetting<std::optional<Path>>(def, true, name, description, aliases)
{
options->addSetting(this);
}
std::optional<Path> parse(const std::string & str) const override; std::optional<Path> parse(const std::string & str) const override;
void operator =(const std::optional<Path> & v) { this->assign(v); } void operator =(const std::optional<Path> & v);
}; };
struct GlobalConfig : public AbstractConfig struct GlobalConfig : public AbstractConfig