diff options
author | eldritch horrors <pennae@lix.systems> | 2024-03-04 04:47:18 +0100 |
---|---|---|
committer | eldritch horrors <pennae@lix.systems> | 2024-03-04 04:47:18 +0100 |
commit | 4d9dde15efbc05af471acb3efc5b04c087ceeef0 (patch) | |
tree | e01d0cc3902973a2900973d999b3c076a2d78cc6 /src/libutil/config.cc | |
parent | ab40b2c5d0f90d6a119bf4b368f933f5331b0c15 (diff) |
Merge pull request #9191 from tfc/libutil-implementation
libutil: Small improvements
(cherry picked from commit edc07588ecd5053da57c2ab6c2543abf49730ce3)
Change-Id: I01a481d872d859b372a4bade9bebd3dab2fb4efb
Diffstat (limited to 'src/libutil/config.cc')
-rw-r--r-- | src/libutil/config.cc | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 8672edaa8..8e06273ee 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -9,6 +9,10 @@ namespace nix { +Config::Config(StringMap initials) + : AbstractConfig(std::move(initials)) +{ } + bool Config::set(const std::string & name, const std::string & value) { bool append = false; @@ -29,9 +33,9 @@ bool Config::set(const std::string & name, const std::string & value) 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) - _settings.emplace(alias, Config::SettingData(true, setting)); + _settings.emplace(alias, Config::SettingData{true, setting}); bool set = false; @@ -59,6 +63,10 @@ void Config::addSetting(AbstractSetting * setting) } } +AbstractConfig::AbstractConfig(StringMap initials) + : unknownSettings(std::move(initials)) +{ } + void AbstractConfig::warnUnknownSettings() { 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() { 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 { return str; @@ -385,11 +403,33 @@ static Path parsePath(const AbstractSetting & s, const std::string & 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 { 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 { if (str == "") @@ -398,6 +438,11 @@ std::optional<Path> OptionalPathSetting::parse(const std::string & str) const 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) { for (auto & config : *configRegistrations) |