From 0f2cf531f705d370321843e5ba9135b2ebdb5d19 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Sun, 17 May 2020 16:31:57 +0100 Subject: style(3p/nix): Reformat project in Google C++ style Reformatted with: fd . -e hh -e cc | xargs clang-format -i --- third_party/nix/src/libutil/config.cc | 466 ++++++++++++++++------------------ 1 file changed, 220 insertions(+), 246 deletions(-) (limited to 'third_party/nix/src/libutil/config.cc') diff --git a/third_party/nix/src/libutil/config.cc b/third_party/nix/src/libutil/config.cc index 9023cb1bb6de..d737945924e8 100644 --- a/third_party/nix/src/libutil/config.cc +++ b/third_party/nix/src/libutil/config.cc @@ -4,268 +4,251 @@ namespace nix { -bool Config::set(const std::string & name, const std::string & value) -{ - auto i = _settings.find(name); - if (i == _settings.end()) return false; - i->second.setting->set(value); - i->second.setting->overriden = true; - return true; -} - -void Config::addSetting(AbstractSetting * setting) -{ - _settings.emplace(setting->name, Config::SettingData(false, setting)); - for (auto & alias : setting->aliases) - _settings.emplace(alias, Config::SettingData(true, setting)); - - bool set = false; - - auto i = unknownSettings.find(setting->name); +bool Config::set(const std::string& name, const std::string& value) { + auto i = _settings.find(name); + if (i == _settings.end()) return false; + i->second.setting->set(value); + i->second.setting->overriden = true; + return true; +} + +void Config::addSetting(AbstractSetting* setting) { + _settings.emplace(setting->name, Config::SettingData(false, setting)); + for (auto& alias : setting->aliases) + _settings.emplace(alias, Config::SettingData(true, setting)); + + bool set = false; + + auto i = unknownSettings.find(setting->name); + if (i != unknownSettings.end()) { + setting->set(i->second); + setting->overriden = true; + unknownSettings.erase(i); + set = true; + } + + for (auto& alias : setting->aliases) { + auto i = unknownSettings.find(alias); if (i != unknownSettings.end()) { + if (set) + warn("setting '%s' is set, but it's an alias of '%s' which is also set", + alias, setting->name); + else { setting->set(i->second); setting->overriden = true; unknownSettings.erase(i); set = true; + } } - - for (auto & alias : setting->aliases) { - auto i = unknownSettings.find(alias); - if (i != unknownSettings.end()) { - if (set) - warn("setting '%s' is set, but it's an alias of '%s' which is also set", - alias, setting->name); - else { - setting->set(i->second); - setting->overriden = true; - unknownSettings.erase(i); - set = true; - } - } - } + } } -void AbstractConfig::warnUnknownSettings() -{ - for (auto & s : unknownSettings) - warn("unknown setting '%s'", s.first); +void AbstractConfig::warnUnknownSettings() { + for (auto& s : unknownSettings) warn("unknown setting '%s'", s.first); } -void AbstractConfig::reapplyUnknownSettings() -{ - auto unknownSettings2 = std::move(unknownSettings); - for (auto & s : unknownSettings2) - set(s.first, s.second); +void AbstractConfig::reapplyUnknownSettings() { + auto unknownSettings2 = std::move(unknownSettings); + for (auto& s : unknownSettings2) set(s.first, s.second); } -void Config::getSettings(std::map & res, bool overridenOnly) -{ - for (auto & opt : _settings) - if (!opt.second.isAlias && (!overridenOnly || opt.second.setting->overriden)) - res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), opt.second.setting->description}); +void Config::getSettings(std::map& res, + bool overridenOnly) { + for (auto& opt : _settings) + if (!opt.second.isAlias && + (!overridenOnly || opt.second.setting->overriden)) + res.emplace(opt.first, SettingInfo{opt.second.setting->to_string(), + opt.second.setting->description}); } -void AbstractConfig::applyConfigFile(const Path & path) -{ - try { - string contents = readFile(path); - - unsigned int pos = 0; - - while (pos < contents.size()) { - string line; - while (pos < contents.size() && contents[pos] != '\n') - line += contents[pos++]; - pos++; - - string::size_type hash = line.find('#'); - if (hash != string::npos) - line = string(line, 0, hash); - - vector tokens = tokenizeString >(line); - if (tokens.empty()) continue; - - if (tokens.size() < 2) - throw UsageError("illegal configuration line '%1%' in '%2%'", line, path); - - auto include = false; - auto ignoreMissing = false; - if (tokens[0] == "include") - include = true; - else if (tokens[0] == "!include") { - include = true; - ignoreMissing = true; - } - - if (include) { - if (tokens.size() != 2) - throw UsageError("illegal configuration line '%1%' in '%2%'", line, path); - auto p = absPath(tokens[1], dirOf(path)); - if (pathExists(p)) { - applyConfigFile(p); - } else if (!ignoreMissing) { - throw Error("file '%1%' included from '%2%' not found", p, path); - } - continue; - } - - if (tokens[1] != "=") - throw UsageError("illegal configuration line '%1%' in '%2%'", line, path); - - string name = tokens[0]; - - vector::iterator i = tokens.begin(); - advance(i, 2); - - set(name, concatStringsSep(" ", Strings(i, tokens.end()))); // FIXME: slow - }; - } catch (SysError &) { } -} +void AbstractConfig::applyConfigFile(const Path& path) { + try { + string contents = readFile(path); -void Config::resetOverriden() -{ - for (auto & s : _settings) - s.second.setting->overriden = false; -} + unsigned int pos = 0; + + while (pos < contents.size()) { + string line; + while (pos < contents.size() && contents[pos] != '\n') + line += contents[pos++]; + pos++; + + string::size_type hash = line.find('#'); + if (hash != string::npos) line = string(line, 0, hash); + + vector tokens = tokenizeString >(line); + if (tokens.empty()) continue; + + if (tokens.size() < 2) + throw UsageError("illegal configuration line '%1%' in '%2%'", line, + path); -void Config::toJSON(JSONObject & out) -{ - for (auto & s : _settings) - if (!s.second.isAlias) { - JSONObject out2(out.object(s.first)); - out2.attr("description", s.second.setting->description); - JSONPlaceholder out3(out2.placeholder("value")); - s.second.setting->toJSON(out3); + auto include = false; + auto ignoreMissing = false; + if (tokens[0] == "include") + include = true; + else if (tokens[0] == "!include") { + include = true; + ignoreMissing = true; + } + + if (include) { + if (tokens.size() != 2) + throw UsageError("illegal configuration line '%1%' in '%2%'", line, + path); + auto p = absPath(tokens[1], dirOf(path)); + if (pathExists(p)) { + applyConfigFile(p); + } else if (!ignoreMissing) { + throw Error("file '%1%' included from '%2%' not found", p, path); } -} + continue; + } + + if (tokens[1] != "=") + throw UsageError("illegal configuration line '%1%' in '%2%'", line, + path); -void Config::convertToArgs(Args & args, const std::string & category) -{ - for (auto & s : _settings) - if (!s.second.isAlias) - s.second.setting->convertToArg(args, category); + string name = tokens[0]; + + vector::iterator i = tokens.begin(); + advance(i, 2); + + set(name, + concatStringsSep(" ", Strings(i, tokens.end()))); // FIXME: slow + }; + } catch (SysError&) { + } } -AbstractSetting::AbstractSetting( - const std::string & name, - const std::string & description, - const std::set & aliases) - : name(name), description(description), aliases(aliases) -{ +void Config::resetOverriden() { + for (auto& s : _settings) s.second.setting->overriden = false; } -void AbstractSetting::toJSON(JSONPlaceholder & out) -{ - out.write(to_string()); +void Config::toJSON(JSONObject& out) { + for (auto& s : _settings) + if (!s.second.isAlias) { + JSONObject out2(out.object(s.first)); + out2.attr("description", s.second.setting->description); + JSONPlaceholder out3(out2.placeholder("value")); + s.second.setting->toJSON(out3); + } } -void AbstractSetting::convertToArg(Args & args, const std::string & category) -{ +void Config::convertToArgs(Args& args, const std::string& category) { + for (auto& s : _settings) + if (!s.second.isAlias) s.second.setting->convertToArg(args, category); } -template -void BaseSetting::toJSON(JSONPlaceholder & out) -{ - out.write(value); +AbstractSetting::AbstractSetting(const std::string& name, + const std::string& description, + const std::set& aliases) + : name(name), description(description), aliases(aliases) {} + +void AbstractSetting::toJSON(JSONPlaceholder& out) { out.write(to_string()); } + +void AbstractSetting::convertToArg(Args& args, const std::string& category) {} + +template +void BaseSetting::toJSON(JSONPlaceholder& out) { + out.write(value); } -template -void BaseSetting::convertToArg(Args & args, const std::string & category) -{ - args.mkFlag() - .longName(name) - .description(description) - .arity(1) - .handler([=](std::vector ss) { overriden = true; set(ss[0]); }) - .category(category); +template +void BaseSetting::convertToArg(Args& args, const std::string& category) { + args.mkFlag() + .longName(name) + .description(description) + .arity(1) + .handler([=](std::vector ss) { + overriden = true; + set(ss[0]); + }) + .category(category); } -template<> void BaseSetting::set(const std::string & str) -{ - value = str; +template <> +void BaseSetting::set(const std::string& str) { + value = str; } -template<> std::string BaseSetting::to_string() -{ - return value; +template <> +std::string BaseSetting::to_string() { + return value; } -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 +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 BaseSetting::to_string() -{ - static_assert(std::is_integral::value, "Integer required."); - return std::to_string(value); +template +std::string BaseSetting::to_string() { + static_assert(std::is_integral::value, "Integer required."); + return std::to_string(value); } -template<> void BaseSetting::set(const std::string & str) -{ - if (str == "true" || str == "yes" || str == "1") - value = true; - else if (str == "false" || str == "no" || str == "0") - value = false; - else - throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str); +template <> +void BaseSetting::set(const std::string& str) { + if (str == "true" || str == "yes" || str == "1") + value = true; + else if (str == "false" || str == "no" || str == "0") + value = false; + else + throw UsageError("Boolean setting '%s' has invalid value '%s'", name, str); } -template<> std::string BaseSetting::to_string() -{ - return value ? "true" : "false"; +template <> +std::string BaseSetting::to_string() { + return value ? "true" : "false"; } -template<> void BaseSetting::convertToArg(Args & args, const std::string & category) -{ - args.mkFlag() - .longName(name) - .description(description) - .handler([=](std::vector ss) { override(true); }) - .category(category); - args.mkFlag() - .longName("no-" + name) - .description(description) - .handler([=](std::vector ss) { override(false); }) - .category(category); +template <> +void BaseSetting::convertToArg(Args& args, const std::string& category) { + args.mkFlag() + .longName(name) + .description(description) + .handler([=](std::vector ss) { override(true); }) + .category(category); + args.mkFlag() + .longName("no-" + name) + .description(description) + .handler([=](std::vector ss) { override(false); }) + .category(category); } -template<> void BaseSetting::set(const std::string & str) -{ - value = tokenizeString(str); +template <> +void BaseSetting::set(const std::string& str) { + value = tokenizeString(str); } -template<> std::string BaseSetting::to_string() -{ - return concatStringsSep(" ", value); +template <> +std::string BaseSetting::to_string() { + return concatStringsSep(" ", value); } -template<> void BaseSetting::toJSON(JSONPlaceholder & out) -{ - JSONList list(out.list()); - for (auto & s : value) - list.elem(s); +template <> +void BaseSetting::toJSON(JSONPlaceholder& out) { + JSONList list(out.list()); + for (auto& s : value) list.elem(s); } -template<> void BaseSetting::set(const std::string & str) -{ - value = tokenizeString(str); +template <> +void BaseSetting::set(const std::string& str) { + value = tokenizeString(str); } -template<> std::string BaseSetting::to_string() -{ - return concatStringsSep(" ", value); +template <> +std::string BaseSetting::to_string() { + return concatStringsSep(" ", value); } -template<> void BaseSetting::toJSON(JSONPlaceholder & out) -{ - JSONList list(out.list()); - for (auto & s : value) - list.elem(s); +template <> +void BaseSetting::toJSON(JSONPlaceholder& out) { + JSONList list(out.list()); + for (auto& s : value) list.elem(s); } template class BaseSetting; @@ -279,60 +262,51 @@ template class BaseSetting; template class BaseSetting; template class BaseSetting; -void PathSetting::set(const std::string & str) -{ - if (str == "") { - if (allowEmpty) - value = ""; - else - throw UsageError("setting '%s' cannot be empty", name); - } else - value = canonPath(str); +void PathSetting::set(const std::string& str) { + if (str == "") { + if (allowEmpty) + value = ""; + else + throw UsageError("setting '%s' cannot be empty", name); + } else + value = canonPath(str); } -bool GlobalConfig::set(const std::string & name, const std::string & value) -{ - for (auto & config : *configRegistrations) - if (config->set(name, value)) return true; +bool GlobalConfig::set(const std::string& name, const std::string& value) { + for (auto& config : *configRegistrations) + if (config->set(name, value)) return true; - unknownSettings.emplace(name, value); + unknownSettings.emplace(name, value); - return false; + return false; } -void GlobalConfig::getSettings(std::map & res, bool overridenOnly) -{ - for (auto & config : *configRegistrations) - config->getSettings(res, overridenOnly); +void GlobalConfig::getSettings(std::map& res, + bool overridenOnly) { + for (auto& config : *configRegistrations) + config->getSettings(res, overridenOnly); } -void GlobalConfig::resetOverriden() -{ - for (auto & config : *configRegistrations) - config->resetOverriden(); +void GlobalConfig::resetOverriden() { + for (auto& config : *configRegistrations) config->resetOverriden(); } -void GlobalConfig::toJSON(JSONObject & out) -{ - for (auto & config : *configRegistrations) - config->toJSON(out); +void GlobalConfig::toJSON(JSONObject& out) { + for (auto& config : *configRegistrations) config->toJSON(out); } -void GlobalConfig::convertToArgs(Args & args, const std::string & category) -{ - for (auto & config : *configRegistrations) - config->convertToArgs(args, category); +void GlobalConfig::convertToArgs(Args& args, const std::string& category) { + for (auto& config : *configRegistrations) + config->convertToArgs(args, category); } GlobalConfig globalConfig; -GlobalConfig::ConfigRegistrations * GlobalConfig::configRegistrations; +GlobalConfig::ConfigRegistrations* GlobalConfig::configRegistrations; -GlobalConfig::Register::Register(Config * config) -{ - if (!configRegistrations) - configRegistrations = new ConfigRegistrations; - configRegistrations->emplace_back(config); +GlobalConfig::Register::Register(Config* config) { + if (!configRegistrations) configRegistrations = new ConfigRegistrations; + configRegistrations->emplace_back(config); } -} +} // namespace nix -- cgit 1.4.1