Setting: Remove "Tag" template argument

This commit is contained in:
Eelco Dolstra 2017-04-20 16:52:53 +02:00
parent f05d5f89ff
commit 4410e9d995
No known key found for this signature in database
GPG key ID: 8170B4726D7198DE
5 changed files with 90 additions and 74 deletions

View file

@ -73,7 +73,7 @@ unsigned int Settings::getDefaultCores()
const string nixVersion = PACKAGE_VERSION; const string nixVersion = PACKAGE_VERSION;
template<> void Setting<SandboxMode>::set(const std::string & str) template<> void BaseSetting<SandboxMode>::set(const std::string & str)
{ {
if (str == "true") value = smEnabled; if (str == "true") value = smEnabled;
else if (str == "relaxed") value = smRelaxed; else if (str == "relaxed") value = smRelaxed;
@ -81,7 +81,7 @@ template<> void Setting<SandboxMode>::set(const std::string & str)
else throw UsageError("option '%s' has invalid value '%s'", name, str); else throw UsageError("option '%s' has invalid value '%s'", name, str);
} }
template<> std::string Setting<SandboxMode>::to_string() template<> std::string BaseSetting<SandboxMode>::to_string()
{ {
if (value == smEnabled) return "true"; if (value == smEnabled) return "true";
else if (value == smRelaxed) return "relaxed"; else if (value == smRelaxed) return "relaxed";
@ -89,27 +89,11 @@ template<> std::string Setting<SandboxMode>::to_string()
else abort(); else abort();
} }
template<> void Setting<unsigned int, Settings::MaxBuildJobsTag>::set(const std::string & str) void MaxBuildJobsSetting::set(const std::string & str)
{ {
if (str == "auto") value = std::max(1U, std::thread::hardware_concurrency()); if (str == "auto") value = std::max(1U, std::thread::hardware_concurrency());
else if (!string2Int(str, value)) else if (!string2Int(str, value))
throw UsageError("configuration setting %s should be auto or an integer", name); throw UsageError("configuration setting %s should be auto or an integer", name);
} }
template<> std::string Setting<unsigned int, Settings::MaxBuildJobsTag>::to_string()
{
return std::to_string(value);
}
template<> void Setting<bool, Settings::CaseHackTag>::set(const std::string & str)
{
value = parseBool(str);
nix::useCaseHack = true;
}
template<> std::string Setting<bool, Settings::CaseHackTag>::to_string()
{
return printBool(value);
}
} }

View file

@ -13,6 +13,39 @@ typedef enum { smEnabled, smRelaxed, smDisabled } SandboxMode;
extern bool useCaseHack; // FIXME extern bool useCaseHack; // FIXME
struct CaseHackSetting : public BaseSetting<bool>
{
CaseHackSetting(Config * options,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<bool>(useCaseHack, name, description, aliases)
{
options->addSetting(this);
}
void set(const std::string & str) override
{
BaseSetting<bool>::set(str);
nix::useCaseHack = true;
}
};
struct MaxBuildJobsSetting : public BaseSetting<unsigned int>
{
MaxBuildJobsSetting(Config * options,
unsigned int def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<unsigned int>(def, name, description, aliases)
{
options->addSetting(this);
}
void set(const std::string & str) override;
};
class Settings : public Config { class Settings : public Config {
unsigned int getDefaultCores(); unsigned int getDefaultCores();
@ -66,8 +99,7 @@ public:
the log to show if a build fails. */ the log to show if a build fails. */
size_t logLines = 10; size_t logLines = 10;
struct MaxBuildJobsTag { }; MaxBuildJobsSetting maxBuildJobs{this, 1, "build-max-jobs",
Setting<unsigned int, MaxBuildJobsTag> maxBuildJobs{this, 1, "build-max-jobs",
"Maximum number of parallel build jobs. \"auto\" means use number of cores."}; "Maximum number of parallel build jobs. \"auto\" means use number of cores."};
Setting<unsigned int> buildCores{this, getDefaultCores(), "build-cores", Setting<unsigned int> buildCores{this, getDefaultCores(), "build-cores",
@ -268,8 +300,7 @@ public:
Setting<bool> enableImportFromDerivation{this, true, "allow-import-from-derivation", Setting<bool> enableImportFromDerivation{this, true, "allow-import-from-derivation",
"Whether the evaluator allows importing the result of a derivation."}; "Whether the evaluator allows importing the result of a derivation."};
struct CaseHackTag { }; CaseHackSetting useCaseHack{this, "use-case-hack",
Setting<bool, CaseHackTag> useCaseHack{this, nix::useCaseHack, "use-case-hack",
"Whether to enable a Darwin-specific hack for dealing with file name collisions."}; "Whether to enable a Darwin-specific hack for dealing with file name collisions."};
Setting<unsigned long> connectTimeout{this, 0, "connect-timeout", Setting<unsigned long> connectTimeout{this, 0, "connect-timeout",

View file

@ -111,83 +111,72 @@ AbstractSetting::AbstractSetting(
{ {
} }
template<> void Setting<std::string>::set(const std::string & str) template<> void BaseSetting<std::string>::set(const std::string & str)
{ {
value = str; value = str;
} }
template<> std::string Setting<std::string>::to_string() template<> std::string BaseSetting<std::string>::to_string()
{ {
return value; return value;
} }
template<typename T, typename Tag> template<typename T>
void Setting<T, Tag>::set(const std::string & str) void BaseSetting<T>::set(const std::string & str)
{ {
static_assert(std::is_integral<T>::value, "Integer required."); static_assert(std::is_integral<T>::value, "Integer required.");
if (!string2Int(str, value)) if (!string2Int(str, value))
throw UsageError("setting '%s' has invalid value '%s'", name, str); throw UsageError("setting '%s' has invalid value '%s'", name, str);
} }
template<typename T, typename Tag> template<typename T>
std::string Setting<T, Tag>::to_string() std::string BaseSetting<T>::to_string()
{ {
static_assert(std::is_integral<T>::value, "Integer required."); static_assert(std::is_integral<T>::value, "Integer required.");
return std::to_string(value); return std::to_string(value);
} }
bool AbstractSetting::parseBool(const std::string & str) template<> void BaseSetting<bool>::set(const std::string & str)
{ {
if (str == "true" || str == "yes" || str == "1") if (str == "true" || str == "yes" || str == "1")
return true; value = true;
else if (str == "false" || str == "no" || str == "0") else if (str == "false" || str == "no" || str == "0")
return false; value = false;
else else
throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str); throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str);
} }
template<> void Setting<bool>::set(const std::string & str) template<> std::string BaseSetting<bool>::to_string()
{ {
value = parseBool(str); return value ? "true" : "false";
} }
std::string AbstractSetting::printBool(bool b) template<> void BaseSetting<Strings>::set(const std::string & str)
{
return b ? "true" : "false";
}
template<> std::string Setting<bool>::to_string()
{
return printBool(value);
}
template<> void Setting<Strings>::set(const std::string & str)
{ {
value = tokenizeString<Strings>(str); value = tokenizeString<Strings>(str);
} }
template<> std::string Setting<Strings>::to_string() template<> std::string BaseSetting<Strings>::to_string()
{ {
return concatStringsSep(" ", value); return concatStringsSep(" ", value);
} }
template<> void Setting<StringSet>::set(const std::string & str) template<> void BaseSetting<StringSet>::set(const std::string & str)
{ {
value = tokenizeString<StringSet>(str); value = tokenizeString<StringSet>(str);
} }
template<> std::string Setting<StringSet>::to_string() template<> std::string BaseSetting<StringSet>::to_string()
{ {
return concatStringsSep(" ", value); return concatStringsSep(" ", value);
} }
template class Setting<int>; template class BaseSetting<int>;
template class Setting<unsigned int>; template class BaseSetting<unsigned int>;
template class Setting<long>; template class BaseSetting<long>;
template class Setting<unsigned long>; template class BaseSetting<unsigned long>;
template class Setting<long long>; template class BaseSetting<long long>;
template class Setting<unsigned long long>; template class BaseSetting<unsigned long long>;
void PathSetting::set(const std::string & str) void PathSetting::set(const std::string & str)
{ {

View file

@ -90,17 +90,12 @@ protected:
virtual std::string to_string() = 0; virtual std::string to_string() = 0;
bool parseBool(const std::string & str);
std::string printBool(bool b);
bool isOverriden() { return overriden; } bool isOverriden() { return overriden; }
}; };
struct DefaultSettingTag { };
/* A setting of type T. */ /* A setting of type T. */
template<typename T, typename Tag = DefaultSettingTag> template<typename T>
class Setting : public AbstractSetting class BaseSetting : public AbstractSetting
{ {
protected: protected:
@ -108,23 +103,21 @@ protected:
public: public:
Setting(Config * options, BaseSetting(const T & def,
const T & 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 = {})
: AbstractSetting(name, description, aliases) : AbstractSetting(name, description, aliases)
, value(def) , value(def)
{ { }
options->addSetting(this);
}
operator const T &() const { return value; } operator const T &() const { return value; }
operator T &() { return value; } operator T &() { return value; }
const T & get() const { 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; }
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; void set(const std::string & str) override;
@ -132,18 +125,35 @@ public:
}; };
template<typename T> template<typename T>
std::ostream & operator <<(std::ostream & str, const Setting<T> & opt) std::ostream & operator <<(std::ostream & str, const BaseSetting<T> & opt)
{ {
str << (const T &) opt; str << (const T &) opt;
return str; return str;
} }
template<typename T> template<typename T>
bool operator ==(const T & v1, const Setting<T> & v2) { return v1 == (const T &) v2; } bool operator ==(const T & v1, const BaseSetting<T> & v2) { return v1 == (const T &) v2; }
template<typename T>
class Setting : public BaseSetting<T>
{
public:
Setting(Config * options,
const T & def,
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<T>(def, name, description, aliases)
{
options->addSetting(this);
}
void operator =(const T & v) { this->assign(v); }
};
/* A special setting for Paths. These are automatically canonicalised /* A special setting for Paths. These are automatically canonicalised
(e.g. "/foo//bar/" becomes "/foo/bar"). */ (e.g. "/foo//bar/" becomes "/foo/bar"). */
class PathSetting : public Setting<Path> class PathSetting : public BaseSetting<Path>
{ {
bool allowEmpty; bool allowEmpty;
@ -155,15 +165,17 @@ public:
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 = {})
: Setting<Path>(options, def, name, description, aliases) : BaseSetting<Path>(def, name, description, aliases)
, allowEmpty(allowEmpty) , allowEmpty(allowEmpty)
{ {
set(value); options->addSetting(this);
} }
void set(const std::string & str) override; void set(const std::string & str) override;
Path operator +(const char * p) const { return value + p; } Path operator +(const char * p) const { return value + p; }
void operator =(const Path & v) { this->assign(v); }
}; };
} }

View file

@ -440,7 +440,7 @@ static void performOp(ref<LocalStore> store, bool trusted, unsigned int clientVe
settings.keepGoing = readInt(from); settings.keepGoing = readInt(from);
settings.tryFallback = readInt(from); settings.tryFallback = readInt(from);
verbosity = (Verbosity) readInt(from); verbosity = (Verbosity) readInt(from);
settings.maxBuildJobs = readInt(from); settings.maxBuildJobs.assign(readInt(from));
settings.maxSilentTime = readInt(from); settings.maxSilentTime = readInt(from);
settings.useBuildHook = readInt(from) != 0; settings.useBuildHook = readInt(from) != 0;
settings.verboseBuild = lvlError == (Verbosity) readInt(from); settings.verboseBuild = lvlError == (Verbosity) readInt(from);