|
|
|
@ -20,16 +20,20 @@ namespace llarp
|
|
|
|
|
bool required_,
|
|
|
|
|
bool multiValued_);
|
|
|
|
|
|
|
|
|
|
virtual ~ConfigDefinitionBase() {}
|
|
|
|
|
virtual
|
|
|
|
|
~ConfigDefinitionBase() {}
|
|
|
|
|
|
|
|
|
|
/// subclasses should provide their default value as a string
|
|
|
|
|
virtual std::string defaultValueAsString() = 0;
|
|
|
|
|
virtual std::string
|
|
|
|
|
defaultValueAsString() = 0;
|
|
|
|
|
|
|
|
|
|
/// subclasses should parse and store the provided input
|
|
|
|
|
virtual void parseValue(const std::string& input) = 0;
|
|
|
|
|
virtual void
|
|
|
|
|
parseValue(const std::string& input) = 0;
|
|
|
|
|
|
|
|
|
|
/// subclasess should write their parsed value (not default value) as a string
|
|
|
|
|
virtual std::string writeValue(bool useDefault) = 0;
|
|
|
|
|
virtual std::string
|
|
|
|
|
writeValue(bool useDefault) = 0;
|
|
|
|
|
|
|
|
|
|
std::string section;
|
|
|
|
|
std::string name;
|
|
|
|
@ -51,7 +55,8 @@ namespace llarp
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nonstd::optional<T> getValue() const
|
|
|
|
|
nonstd::optional<T>
|
|
|
|
|
getValue() const
|
|
|
|
|
{
|
|
|
|
|
if (parsedValue)
|
|
|
|
|
return parsedValue.value();
|
|
|
|
@ -61,7 +66,8 @@ namespace llarp
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string defaultValueAsString()
|
|
|
|
|
std::string
|
|
|
|
|
defaultValueAsString()
|
|
|
|
|
{
|
|
|
|
|
if (defaultValue)
|
|
|
|
|
return std::to_string(defaultValue.value());
|
|
|
|
@ -69,7 +75,8 @@ namespace llarp
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void parseValue(const std::string& input)
|
|
|
|
|
void
|
|
|
|
|
parseValue(const std::string& input)
|
|
|
|
|
{
|
|
|
|
|
if (not multiValued and parsedValue)
|
|
|
|
|
{
|
|
|
|
@ -86,7 +93,8 @@ namespace llarp
|
|
|
|
|
parsedValue = t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string writeValue(bool useDefault)
|
|
|
|
|
std::string
|
|
|
|
|
writeValue(bool useDefault)
|
|
|
|
|
{
|
|
|
|
|
if (parsedValue)
|
|
|
|
|
return std::to_string(parsedValue.value());
|
|
|
|
@ -110,17 +118,13 @@ namespace llarp
|
|
|
|
|
// the first std::string template parameter is the section
|
|
|
|
|
std::unordered_map<std::string, std::unordered_map<std::string, ConfigDefinition_ptr>> m_definitions;
|
|
|
|
|
|
|
|
|
|
Configuration& addDefinition(ConfigDefinition_ptr def);
|
|
|
|
|
Configuration&
|
|
|
|
|
addDefinition(ConfigDefinition_ptr def);
|
|
|
|
|
|
|
|
|
|
Configuration& addConfigValue(string_view section,
|
|
|
|
|
configuration&
|
|
|
|
|
addconfigvalue(string_view section,
|
|
|
|
|
string_view name,
|
|
|
|
|
string_view value)
|
|
|
|
|
{
|
|
|
|
|
ConfigDefinition_ptr& definition = lookupDefinitionOrThrow(section, name);
|
|
|
|
|
definition->parseValue(std::string(value));
|
|
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
string_view value);
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
nonstd::optional<T> getConfigValue(string_view section, string_view name)
|
|
|
|
@ -143,7 +147,8 @@ namespace llarp
|
|
|
|
|
/// duplicates, etc. are handled during parsing.
|
|
|
|
|
///
|
|
|
|
|
/// @throws std::invalid_argument if configuration constraints are not met
|
|
|
|
|
void validate();
|
|
|
|
|
void
|
|
|
|
|
validate();
|
|
|
|
|
|
|
|
|
|
std::string
|
|
|
|
|
generateDefaultConfig();
|
|
|
|
@ -153,25 +158,8 @@ namespace llarp
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
|
|
const ConfigDefinition_ptr& lookupDefinitionOrThrow(string_view section, string_view name) const
|
|
|
|
|
{
|
|
|
|
|
const auto sectionItr = m_definitions.find(std::string(section));
|
|
|
|
|
if (sectionItr == m_definitions.end())
|
|
|
|
|
throw std::invalid_argument(stringify("No config section ", section));
|
|
|
|
|
|
|
|
|
|
auto& sectionDefinitions = sectionItr->second;
|
|
|
|
|
const auto definitionItr = sectionDefinitions.find(std::string(name));
|
|
|
|
|
if (definitionItr == sectionDefinitions.end())
|
|
|
|
|
throw std::invalid_argument(stringify("No config item ", name, " within section ", section));
|
|
|
|
|
|
|
|
|
|
return definitionItr->second;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ConfigDefinition_ptr& lookupDefinitionOrThrow(string_view section, string_view name)
|
|
|
|
|
{
|
|
|
|
|
return const_cast<ConfigDefinition_ptr&>(
|
|
|
|
|
const_cast<const Configuration*>(this)->lookupDefinitionOrThrow(section, name));
|
|
|
|
|
}
|
|
|
|
|
ConfigDefinition_ptr& lookupDefinitionOrThrow(string_view section, string_view name);
|
|
|
|
|
const ConfigDefinition_ptr& lookupDefinitionOrThrow(string_view section, string_view name) const;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace llarp
|
|
|
|
|