1#ifndef XRPL_BASICS_BASICCONFIG_H_INCLUDED
2#define XRPL_BASICS_BASICCONFIG_H_INCLUDED
4#include <xrpl/basics/contract.h>
6#include <boost/beast/core/string.hpp>
7#include <boost/lexical_cast.hpp>
74 lines_[0] = std::move(value);
89 Throw<std::runtime_error>(
90 "A legacy value must have exactly one line. Section: " +
name_);
119 template <
class T = std::
string>
126 return boost::lexical_cast<T>(iter->second);
134 auto const v = get<T>(
name);
135 return v.has_value() ? *v : other;
278 return s.second.had_trailing_comments();
298 bool found_and_valid =
false;
301 auto const val = section.
get<T>(name);
302 if ((found_and_valid = val.has_value()))
305 catch (boost::bad_lexical_cast&)
308 return found_and_valid;
319 T
const& defaultValue,
323 bool found_and_valid = set<T>(target, name, section);
324 if (!found_and_valid)
325 target = defaultValue;
326 return found_and_valid;
334template <
class T = std::
string>
338 T
const& defaultValue = T{})
342 return section.
value_or<T>(name, defaultValue);
344 catch (boost::bad_lexical_cast&)
355 auto const val = section.
get(name);
359 catch (boost::bad_lexical_cast&)
369 return set<T>(v, name, section);
Holds unparsed configuration information.
friend std::ostream & operator<<(std::ostream &ss, BasicConfig const &c)
bool exists(std::string const &name) const
Returns true if a section with the given name exists.
bool had_trailing_comments() const
Section const & operator[](std::string const &name) const
void deprecatedClearSection(std::string const §ion)
Remove all the key/value pairs from the section.
Section & section(std::string const &name)
Returns the section with the given name.
void build(IniFileSections const &ifs)
std::unordered_map< std::string, Section > map_
void overwrite(std::string const §ion, std::string const &key, std::string const &value)
Overwrite a key/value pair with a command line argument If the section does not exist it is created.
Section & operator[](std::string const &name)
void legacy(std::string const §ion, std::string value)
Set a value that is not a key/value pair.
Holds a collection of configuration values.
std::string const & name() const
Returns the name of this section.
std::string legacy() const
Get the legacy value for this section.
std::vector< std::string > lines_
friend std::ostream & operator<<(std::ostream &, Section const §ion)
const_iterator begin() const
decltype(lookup_)::const_iterator const_iterator
const_iterator cend() const
bool had_trailing_comments() const
T value_or(std::string const &name, T const &other) const
Returns a value if present, else another value.
std::vector< std::string > const & lines() const
Returns all the lines in the section.
void legacy(std::string value)
Set the legacy value for this section.
std::optional< T > get(std::string const &name) const
bool exists(std::string const &name) const
Returns true if a key with the given name exists.
void append(std::string const &line)
Append a line to this section.
const_iterator cbegin() const
bool had_trailing_comments_
void append(std::vector< std::string > const &lines)
Append a set of lines to this section.
std::vector< std::string > const & values() const
Returns all the values in the section.
std::unordered_map< std::string, std::string > lookup_
std::vector< std::string > values_
const_iterator end() const
T emplace_back(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
bool set(T &target, std::string const &name, Section const §ion)
Set a value from a configuration Section If the named value is not found or doesn't parse as a T,...
bool get_if_exists(Section const §ion, std::string const &name, T &v)
bool get_if_exists< bool >(Section const §ion, std::string const &name, bool &v)
T get(Section const §ion, std::string const &name, T const &defaultValue=T{})
Retrieve a key/value pair from a section.