Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting handlers (side effects when settings are changed) #9922

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/libstore/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -262,28 +262,28 @@ NLOHMANN_JSON_SERIALIZE_ENUM(SandboxMode, {
{SandboxMode::smDisabled, false},
});

template<> SandboxMode BaseSetting<SandboxMode>::parse(const std::string & str) const
template<> SandboxMode Setting<SandboxMode>::parse(const std::string & str) const
{
if (str == "true") return smEnabled;
else if (str == "relaxed") return smRelaxed;
else if (str == "false") return smDisabled;
else throw UsageError("option '%s' has invalid value '%s'", name, str);
}

template<> struct BaseSetting<SandboxMode>::trait
template<> struct Setting<SandboxMode>::trait
{
static constexpr bool appendable = false;
};

template<> std::string BaseSetting<SandboxMode>::to_string() const
template<> std::string Setting<SandboxMode>::to_string() const
{
if (value == smEnabled) return "true";
else if (value == smRelaxed) return "relaxed";
else if (value == smDisabled) return "false";
else abort();
}

template<> void BaseSetting<SandboxMode>::convertToArg(Args & args, const std::string & category)
template<> void Setting<SandboxMode>::convertToArg(Args & args, const std::string & category)
{
args.addFlag({
.longName = name,
Expand Down Expand Up @@ -321,7 +321,7 @@ Paths PluginFilesSetting::parse(const std::string & str) const
{
if (pluginsLoaded)
throw UsageError("plugin-files set after plugins were loaded, you may need to move the flag before the subcommand");
return BaseSetting<Paths>::parse(str);
return Setting<Paths>::parse(str);
}


Expand Down
12 changes: 5 additions & 7 deletions src/libstore/globals.hh
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ namespace nix {

typedef enum { smEnabled, smRelaxed, smDisabled } SandboxMode;

struct MaxBuildJobsSetting : public BaseSetting<unsigned int>
struct MaxBuildJobsSetting : public Setting<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, true, name, description, aliases)
{
options->addSetting(this);
}
: Setting<unsigned int>(options, def, name, description, aliases)
{ }

unsigned int parse(const std::string & str) const override;
};

struct PluginFilesSetting : public BaseSetting<Paths>
struct PluginFilesSetting : public Setting<Paths>
{
bool pluginsLoaded = false;

Expand All @@ -39,7 +37,7 @@ struct PluginFilesSetting : public BaseSetting<Paths>
const std::string & name,
const std::string & description,
const std::set<std::string> & aliases = {})
: BaseSetting<Paths>(def, true, name, description, aliases)
: Setting<Paths>(options, def, name, description, aliases)
{
options->addSetting(this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/libutil/abstract-setting-to-json.hh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace nix {
template<typename T>
std::map<std::string, nlohmann::json> BaseSetting<T>::toJSONObject() const
std::map<std::string, nlohmann::json> Setting<T>::toJSONObject() const
{
auto obj = AbstractSetting::toJSONObject();
obj.emplace("value", value);
Expand Down
40 changes: 21 additions & 19 deletions src/libutil/config-impl.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,57 +12,59 @@
* instantiation.
*/

#include "logging.hh"
#include "args.hh"
#include "config.hh"

namespace nix {

template<> struct BaseSetting<Strings>::trait
template<> struct Setting<Strings>::trait
{
static constexpr bool appendable = true;
};
template<> struct BaseSetting<StringSet>::trait
template<> struct Setting<StringSet>::trait
{
static constexpr bool appendable = true;
};
template<> struct BaseSetting<StringMap>::trait
template<> struct Setting<StringMap>::trait
{
static constexpr bool appendable = true;
};
template<> struct BaseSetting<std::set<ExperimentalFeature>>::trait
template<> struct Setting<std::set<ExperimentalFeature>>::trait
{
static constexpr bool appendable = true;
};

template<typename T>
struct BaseSetting<T>::trait
struct Setting<T>::trait
{
static constexpr bool appendable = false;
};

template<typename T>
bool BaseSetting<T>::isAppendable()
bool Setting<T>::isAppendable()
{
return trait::appendable;
}

template<> void BaseSetting<Strings>::appendOrSet(Strings newValue, bool append);
template<> void BaseSetting<StringSet>::appendOrSet(StringSet newValue, bool append);
template<> void BaseSetting<StringMap>::appendOrSet(StringMap newValue, bool append);
template<> void BaseSetting<std::set<ExperimentalFeature>>::appendOrSet(std::set<ExperimentalFeature> newValue, bool append);
template<> void Setting<Strings>::appendOrSet(Strings newValue, bool append);
template<> void Setting<StringSet>::appendOrSet(StringSet newValue, bool append);
template<> void Setting<StringMap>::appendOrSet(StringMap newValue, bool append);
template<> void Setting<std::set<ExperimentalFeature>>::appendOrSet(std::set<ExperimentalFeature> newValue, bool append);

template<typename T>
void BaseSetting<T>::appendOrSet(T newValue, bool append)
void Setting<T>::appendOrSet(T newValue, bool append)
{
static_assert(
!trait::appendable,
"using default `appendOrSet` implementation with an appendable type");
assert(!append);

value = std::move(newValue);
assign(std::move(newValue));
}

template<typename T>
void BaseSetting<T>::set(const std::string & str, bool append)
void Setting<T>::set(const std::string & str, bool append)
{
if (experimentalFeatureSettings.isEnabled(experimentalFeature))
appendOrSet(parse(str), append);
Expand All @@ -74,10 +76,10 @@ void BaseSetting<T>::set(const std::string & str, bool append)
}
}

template<> void BaseSetting<bool>::convertToArg(Args & args, const std::string & category);
template<> void Setting<bool>::convertToArg(Args & args, const std::string & category);

template<typename T>
void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
void Setting<T>::convertToArg(Args & args, const std::string & category)
{
args.addFlag({
.longName = name,
Expand All @@ -100,8 +102,8 @@ void BaseSetting<T>::convertToArg(Args & args, const std::string & category)
}

#define DECLARE_CONFIG_SERIALISER(TY) \
template<> TY BaseSetting< TY >::parse(const std::string & str) const; \
template<> std::string BaseSetting< TY >::to_string() const;
template<> TY Setting< TY >::parse(const std::string & str) const; \
template<> std::string Setting< TY >::to_string() const;

DECLARE_CONFIG_SERIALISER(std::string)
DECLARE_CONFIG_SERIALISER(std::optional<std::string>)
Expand All @@ -112,7 +114,7 @@ DECLARE_CONFIG_SERIALISER(StringMap)
DECLARE_CONFIG_SERIALISER(std::set<ExperimentalFeature>)

template<typename T>
T BaseSetting<T>::parse(const std::string & str) const
T Setting<T>::parse(const std::string & str) const
{
static_assert(std::is_integral<T>::value, "Integer required.");

Expand All @@ -123,7 +125,7 @@ T BaseSetting<T>::parse(const std::string & str) const
}

template<typename T>
std::string BaseSetting<T>::to_string() const
std::string Setting<T>::to_string() const
{
static_assert(std::is_integral<T>::value, "Integer required.");

Expand Down
Loading
Loading