diff options
author | rebecca “wiggles” turner <rbt@sent.as> | 2024-08-23 22:09:11 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@localhost> | 2024-08-23 22:09:11 +0000 |
commit | c5949bfe313a92aab0e4cf38ab2407b0ac922ce8 (patch) | |
tree | ed9880d9cc4f25b240d83c8d8fdd04e533ca8afc /src/libutil | |
parent | 984563735934f63c972d0539b4ea57e87389d2a0 (diff) | |
parent | e3c289dbe945415367dc1f54f85ad2452f5a97e0 (diff) |
Merge "libutil/config: unify path setting types" into main
Diffstat (limited to 'src/libutil')
-rw-r--r-- | src/libutil/config.cc | 52 | ||||
-rw-r--r-- | src/libutil/config.hh | 69 |
2 files changed, 23 insertions, 98 deletions
diff --git a/src/libutil/config.cc b/src/libutil/config.cc index 3c4f4798b..8e20f1321 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -434,34 +434,12 @@ 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 +template<> Path PathsSetting<Path>::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 +template<> std::optional<Path> PathsSetting<std::optional<Path>>::parse(const std::string & str) const { if (str == "") return std::nullopt; @@ -469,23 +447,7 @@ 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); -} - -PathsSetting::PathsSetting(Config * options, - const Paths & def, - const std::string & name, - const std::string & description, - const std::set<std::string> & aliases) - : BaseSetting<Paths>(def, true, name, description, aliases) -{ - options->addSetting(this); -} - - -Paths PathsSetting::parse(const std::string & str) const +template<> Paths PathsSetting<Paths>::parse(const std::string & str) const { auto strings = tokenizeString<Strings>(str); Paths parsed; @@ -497,10 +459,10 @@ Paths PathsSetting::parse(const std::string & str) const return parsed; } -PathsSetting::operator bool() const noexcept -{ - return !get().empty(); -} +template class PathsSetting<Path>; +template class PathsSetting<std::optional<Path>>; +template class PathsSetting<Paths>; + bool GlobalConfig::set(const std::string & name, const std::string & value) { diff --git a/src/libutil/config.hh b/src/libutil/config.hh index d1812f47e..dbca4b406 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -353,69 +353,32 @@ public: }; /** - * A special setting for Paths. These are automatically canonicalised - * (e.g. "/foo//bar/" becomes "/foo/bar"). - * - * It is mandatory to specify a path; i.e. the empty string is not - * permitted. + * A special setting for Paths. + * These are automatically canonicalised (e.g. "/foo//bar/" becomes "/foo/bar"). + * The empty string is not permitted when a path is required. */ -class PathSetting : public BaseSetting<Path> -{ -public: - - PathSetting(Config * options, - const Path & def, - const std::string & name, - const std::string & description, - const std::set<std::string> & aliases = {}); - - Path parse(const std::string & str) const override; - - Path operator +(const char * p) const { return value + p; } - - void operator =(const Path & v) { this->assign(v); } -}; - -/** - * Like `PathSetting`, but the absence of a path is also allowed. - * - * `std::optional` is used instead of the empty string for clarity. - */ -class OptionalPathSetting : public BaseSetting<std::optional<Path>> -{ -public: - - OptionalPathSetting(Config * options, - const std::optional<Path> & def, - const std::string & name, - const std::string & description, - const std::set<std::string> & aliases = {}); - - std::optional<Path> parse(const std::string & str) const override; - - void operator =(const std::optional<Path> & v); -}; - -/** - * Like `OptionalPathSetting`, but allows multiple paths. - */ -class PathsSetting : public BaseSetting<Paths> +template<typename T> +class PathsSetting : public BaseSetting<T> { public: - PathsSetting(Config * options, - const Paths & def, + const T & def, const std::string & name, const std::string & description, - const std::set<std::string> & aliases = {}); - - Paths parse(const std::string & str) const override; + const std::set<std::string> & aliases = {}, + const bool documentDefault = true, + std::optional<ExperimentalFeature> experimentalFeature = std::nullopt) + : BaseSetting<T>(def, documentDefault, name, description, aliases, std::move(experimentalFeature)) + { + options->addSetting(this); + } - void operator =(const Paths & v); + T parse(const std::string & str) const override; - operator bool() const noexcept; + void operator =(const T & v) { this->assign(v); } }; + struct GlobalConfig : public AbstractConfig { typedef std::vector<Config*> ConfigRegistrations; |