From 4410e9d995bcd53a7a4cff0bbee3917375adcba3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 20 Apr 2017 16:52:53 +0200 Subject: [PATCH] Setting: Remove "Tag" template argument --- src/libstore/globals.cc | 22 ++------------- src/libstore/globals.hh | 39 +++++++++++++++++++++++--- src/libutil/config.cc | 53 ++++++++++++++---------------------- src/libutil/config.hh | 48 ++++++++++++++++++++------------ src/nix-daemon/nix-daemon.cc | 2 +- 5 files changed, 90 insertions(+), 74 deletions(-) diff --git a/src/libstore/globals.cc b/src/libstore/globals.cc index 6b9d07746..3242ef9d6 100644 --- a/src/libstore/globals.cc +++ b/src/libstore/globals.cc @@ -73,7 +73,7 @@ unsigned int Settings::getDefaultCores() const string nixVersion = PACKAGE_VERSION; -template<> void Setting::set(const std::string & str) +template<> void BaseSetting::set(const std::string & str) { if (str == "true") value = smEnabled; else if (str == "relaxed") value = smRelaxed; @@ -81,7 +81,7 @@ template<> void Setting::set(const std::string & str) else throw UsageError("option '%s' has invalid value '%s'", name, str); } -template<> std::string Setting::to_string() +template<> std::string BaseSetting::to_string() { if (value == smEnabled) return "true"; else if (value == smRelaxed) return "relaxed"; @@ -89,27 +89,11 @@ template<> std::string Setting::to_string() else abort(); } -template<> void Setting::set(const std::string & str) +void MaxBuildJobsSetting::set(const std::string & str) { if (str == "auto") value = std::max(1U, std::thread::hardware_concurrency()); else if (!string2Int(str, value)) throw UsageError("configuration setting ‘%s’ should be ‘auto’ or an integer", name); } -template<> std::string Setting::to_string() -{ - return std::to_string(value); -} - -template<> void Setting::set(const std::string & str) -{ - value = parseBool(str); - nix::useCaseHack = true; -} - -template<> std::string Setting::to_string() -{ - return printBool(value); -} - } diff --git a/src/libstore/globals.hh b/src/libstore/globals.hh index d3ecaadb6..b4f44de2e 100644 --- a/src/libstore/globals.hh +++ b/src/libstore/globals.hh @@ -13,6 +13,39 @@ typedef enum { smEnabled, smRelaxed, smDisabled } SandboxMode; extern bool useCaseHack; // FIXME +struct CaseHackSetting : public BaseSetting +{ + CaseHackSetting(Config * options, + const std::string & name, + const std::string & description, + const std::set & aliases = {}) + : BaseSetting(useCaseHack, name, description, aliases) + { + options->addSetting(this); + } + + void set(const std::string & str) override + { + BaseSetting::set(str); + nix::useCaseHack = true; + } +}; + +struct MaxBuildJobsSetting : public BaseSetting +{ + MaxBuildJobsSetting(Config * options, + unsigned int def, + const std::string & name, + const std::string & description, + const std::set & aliases = {}) + : BaseSetting(def, name, description, aliases) + { + options->addSetting(this); + } + + void set(const std::string & str) override; +}; + class Settings : public Config { unsigned int getDefaultCores(); @@ -66,8 +99,7 @@ public: the log to show if a build fails. */ size_t logLines = 10; - struct MaxBuildJobsTag { }; - Setting maxBuildJobs{this, 1, "build-max-jobs", + MaxBuildJobsSetting maxBuildJobs{this, 1, "build-max-jobs", "Maximum number of parallel build jobs. \"auto\" means use number of cores."}; Setting buildCores{this, getDefaultCores(), "build-cores", @@ -268,8 +300,7 @@ public: Setting enableImportFromDerivation{this, true, "allow-import-from-derivation", "Whether the evaluator allows importing the result of a derivation."}; - struct CaseHackTag { }; - Setting useCaseHack{this, nix::useCaseHack, "use-case-hack", + CaseHackSetting useCaseHack{this, "use-case-hack", "Whether to enable a Darwin-specific hack for dealing with file name collisions."}; Setting connectTimeout{this, 0, "connect-timeout", diff --git a/src/libutil/config.cc b/src/libutil/config.cc index bf1372997..72b6cf806 100644 --- a/src/libutil/config.cc +++ b/src/libutil/config.cc @@ -111,83 +111,72 @@ AbstractSetting::AbstractSetting( { } -template<> void Setting::set(const std::string & str) +template<> void BaseSetting::set(const std::string & str) { value = str; } -template<> std::string Setting::to_string() +template<> std::string BaseSetting::to_string() { return value; } -template -void Setting::set(const std::string & str) +template +void BaseSetting::set(const std::string & str) { static_assert(std::is_integral::value, "Integer required."); if (!string2Int(str, value)) throw UsageError("setting '%s' has invalid value '%s'", name, str); } -template -std::string Setting::to_string() +template +std::string BaseSetting::to_string() { static_assert(std::is_integral::value, "Integer required."); return std::to_string(value); } -bool AbstractSetting::parseBool(const std::string & str) +template<> void BaseSetting::set(const std::string & str) { if (str == "true" || str == "yes" || str == "1") - return true; + value = true; else if (str == "false" || str == "no" || str == "0") - return false; + value = false; else throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str); } -template<> void Setting::set(const std::string & str) +template<> std::string BaseSetting::to_string() { - value = parseBool(str); + return value ? "true" : "false"; } -std::string AbstractSetting::printBool(bool b) -{ - return b ? "true" : "false"; -} - - -template<> std::string Setting::to_string() -{ - return printBool(value); -} - -template<> void Setting::set(const std::string & str) +template<> void BaseSetting::set(const std::string & str) { value = tokenizeString(str); } -template<> std::string Setting::to_string() +template<> std::string BaseSetting::to_string() { return concatStringsSep(" ", value); } -template<> void Setting::set(const std::string & str) +template<> void BaseSetting::set(const std::string & str) { value = tokenizeString(str); } -template<> std::string Setting::to_string() +template<> std::string BaseSetting::to_string() { return concatStringsSep(" ", value); } -template class Setting; -template class Setting; -template class Setting; -template class Setting; -template class Setting; -template class Setting; +template class BaseSetting; +template class BaseSetting; +template class BaseSetting; +template class BaseSetting; +template class BaseSetting; +template class BaseSetting; void PathSetting::set(const std::string & str) { diff --git a/src/libutil/config.hh b/src/libutil/config.hh index 952bf04b8..130f59e2b 100644 --- a/src/libutil/config.hh +++ b/src/libutil/config.hh @@ -90,17 +90,12 @@ protected: virtual std::string to_string() = 0; - bool parseBool(const std::string & str); - std::string printBool(bool b); - bool isOverriden() { return overriden; } }; -struct DefaultSettingTag { }; - /* A setting of type T. */ -template -class Setting : public AbstractSetting +template +class BaseSetting : public AbstractSetting { protected: @@ -108,23 +103,21 @@ protected: public: - Setting(Config * options, - const T & def, + BaseSetting(const T & def, const std::string & name, const std::string & description, const std::set & aliases = {}) : AbstractSetting(name, description, aliases) , value(def) - { - options->addSetting(this); - } + { } operator const T &() const { return value; } operator T &() { return value; } const T & get() 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 operator =(const T & v) { assign(v); } + virtual void assign(const T & v) { value = v; } void set(const std::string & str) override; @@ -132,18 +125,35 @@ public: }; template -std::ostream & operator <<(std::ostream & str, const Setting & opt) +std::ostream & operator <<(std::ostream & str, const BaseSetting & opt) { str << (const T &) opt; return str; } template -bool operator ==(const T & v1, const Setting & v2) { return v1 == (const T &) v2; } +bool operator ==(const T & v1, const BaseSetting & v2) { return v1 == (const T &) v2; } + +template +class Setting : public BaseSetting +{ +public: + Setting(Config * options, + const T & def, + const std::string & name, + const std::string & description, + const std::set & aliases = {}) + : BaseSetting(def, name, description, aliases) + { + options->addSetting(this); + } + + void operator =(const T & v) { this->assign(v); } +}; /* A special setting for Paths. These are automatically canonicalised (e.g. "/foo//bar/" becomes "/foo/bar"). */ -class PathSetting : public Setting +class PathSetting : public BaseSetting { bool allowEmpty; @@ -155,15 +165,17 @@ public: const std::string & name, const std::string & description, const std::set & aliases = {}) - : Setting(options, def, name, description, aliases) + : BaseSetting(def, name, description, aliases) , allowEmpty(allowEmpty) { - set(value); + options->addSetting(this); } void set(const std::string & str) override; Path operator +(const char * p) const { return value + p; } + + void operator =(const Path & v) { this->assign(v); } }; } diff --git a/src/nix-daemon/nix-daemon.cc b/src/nix-daemon/nix-daemon.cc index 5c2641eac..07ad0b45b 100644 --- a/src/nix-daemon/nix-daemon.cc +++ b/src/nix-daemon/nix-daemon.cc @@ -440,7 +440,7 @@ static void performOp(ref store, bool trusted, unsigned int clientVe settings.keepGoing = readInt(from); settings.tryFallback = readInt(from); verbosity = (Verbosity) readInt(from); - settings.maxBuildJobs = readInt(from); + settings.maxBuildJobs.assign(readInt(from)); settings.maxSilentTime = readInt(from); settings.useBuildHook = readInt(from) != 0; settings.verboseBuild = lvlError == (Verbosity) readInt(from);