MySQL 8.0.39
Source Code Documentation
mysql_harness::Config Class Reference

Configuration. More...

#include <config_parser.h>

Inheritance diagram for mysql_harness::Config:
[legend]

Public Types

using SectionKey = std::pair< std::string, std::string >
 
using OptionMap = ConfigSection::OptionMap
 
using SectionList = std::list< ConfigSection * >
 
using ConstSectionList = std::list< const ConfigSection * >
 
using ConfigOverwrites = std::map< std::pair< std::string, std::string >, std::map< std::string, std::string > >
 

Public Member Functions

 Config (unsigned int flags=0U, const ConfigOverwrites &config_overwrites={})
 Construct a configuration. More...
 
template<class AssocT >
 Config (const AssocT &parameters, unsigned int flags=0U, const ConfigOverwrites &config_overwrites={})
 Construct a configuration. More...
 
template<class AssocT , class SeqT >
 Config (const AssocT &parameters, const SeqT &reserved, unsigned int flags=0U, const ConfigOverwrites &config_overwrites={})
 Construct a configuration. More...
 
 Config (const Config &)=default
 
Configoperator= (const Config &)=default
 
virtual ~Config ()=default
 
template<class SeqT >
void set_reserved (const SeqT &reserved)
 
void read (std::istream &input)
 Read configuration file from file, directory, or input stream. More...
 
void read (const Path &path)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void read (const Path &path, const std::string &pattern)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool empty () const
 Check if the configuration is empty. More...
 
void clear ()
 Clear the configuration. More...
 
bool remove (const SectionKey &section_key) noexcept
 Remove section from configuration. More...
 
bool remove (const std::string &section, const std::string &key=std::string()) noexcept
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void update (const Config &other)
 Update configuration using another configuration. More...
 
ConstSectionList get (const std::string &section) const
 Get a list of sections having a name. More...
 
SectionList get (const std::string &section)
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
ConfigSectionget_default_section () const
 
ConfigSectionget (const std::string &section, const std::string &key)
 Get a section by name and key. More...
 
const ConfigSectionget (const std::string &section, const std::string &key) const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
ConfigSectionadd (const std::string &section, const std::string &key=std::string())
 Add a new section to the configuration. More...
 
bool has (const std::string &section, const std::string &key=std::string()) const
 
bool has_any (const std::string &section) const
 
std::string get_default (const std::string &option) const
 
bool has_default (const std::string &option) const
 
void set_default (const std::string &option, const std::string &value)
 
bool is_reserved (const std::string &word) const
 
std::list< Config::SectionKeysection_names () const
 
ConstSectionList sections () const
 Get a list of all sections in the configuration. More...
 

Public Attributes

bool error_on_unsupported_option {false}
 

Static Public Attributes

static constexpr const char * DEFAULT_PATTERN = "*.cfg"
 Default pattern to used to identify configuration files. More...
 
static constexpr unsigned int allow_keys = 1U
 Flags for construction of configurations. More...
 

Protected Types

using SectionMap = std::map< SectionKey, ConfigSection >
 
using ReservedList = std::vector< std::string >
 

Protected Member Functions

void copy_guts (const Config &source) noexcept
 Copy the guts of another configuration. More...
 
virtual void do_read_file (const Path &path)
 Function to read single file. More...
 
virtual void do_read_stream (std::istream &input)
 Function to read the configuration from a stream. More...
 
void apply_overwrites ()
 

Protected Attributes

SectionMap sections_
 
ReservedList reserved_
 
std::shared_ptr< ConfigSectiondefaults_
 
unsigned int flags_
 
ConfigOverwrites config_overwrites_
 

Detailed Description

Configuration.

A configuration consisting of named configuration sections.

There are three different constructors that are available with different kind of parameters.

Member Typedef Documentation

◆ ConfigOverwrites

using mysql_harness::Config::ConfigOverwrites = std::map<std::pair<std::string, std::string>, std::map<std::string, std::string> >

◆ ConstSectionList

◆ OptionMap

◆ ReservedList

using mysql_harness::Config::ReservedList = std::vector<std::string>
protected

◆ SectionKey

using mysql_harness::Config::SectionKey = std::pair<std::string, std::string>

◆ SectionList

◆ SectionMap

Constructor & Destructor Documentation

◆ Config() [1/4]

mysql_harness::Config::Config ( unsigned int  flags = 0U,
const ConfigOverwrites config_overwrites = {} 
)
explicit

Construct a configuration.

Parameters
flagsflags.
config_overwritesoverwrites for selected configuration options.

◆ Config() [2/4]

template<class AssocT >
mysql_harness::Config::Config ( const AssocT &  parameters,
unsigned int  flags = 0U,
const ConfigOverwrites config_overwrites = {} 
)
inlineexplicit

Construct a configuration.

Template Parameters
AssocTAssociate container type
  • parameters Associative container with parameters.
  • flags flags.
  • config_overwrites overwrites for selected configuration options.
Exceptions
bad_optionon bad options

◆ Config() [3/4]

template<class AssocT , class SeqT >
mysql_harness::Config::Config ( const AssocT &  parameters,
const SeqT &  reserved,
unsigned int  flags = 0U,
const ConfigOverwrites config_overwrites = {} 
)
inlineexplicit

Construct a configuration.

Construct a configuration instance by reading a configuration file and overriding the values read from a list of supplied parameters.

Template Parameters
AssocTAssociate container type
SeqTSequence container type
  • parameters Associative container with parameters.
  • reserved Sequence container of reserved words.
  • flags flags.
  • config_overwrites overwrites for selected configuration options.
Exceptions
bad_optionon bad options

◆ Config() [4/4]

mysql_harness::Config::Config ( const Config )
default

◆ ~Config()

virtual mysql_harness::Config::~Config ( )
virtualdefault

Member Function Documentation

◆ add()

ConfigSection & mysql_harness::Config::add ( const std::string &  section,
const std::string &  key = std::string() 
)

Add a new section to the configuration.

Parameters
sectionName of section to add.
keyOptional key of section to add.
Returns
Reference to newly constructed configuration section instance.

◆ apply_overwrites()

void mysql_harness::Config::apply_overwrites ( )
protected

◆ clear()

void mysql_harness::Config::clear ( )

Clear the configuration.

This will remove all configuration information from the configuration, including the default section, but not the reserved words nor the flags set.

◆ copy_guts()

void mysql_harness::Config::copy_guts ( const Config source)
protectednoexcept

Copy the guts of another configuration.

This member function is used to copy configuration state (the "guts") but not the sections and options, including not copying the default section.

◆ do_read_file()

void mysql_harness::Config::do_read_file ( const Path path)
protectedvirtual

Function to read single file.

Exceptions
std::runtime_error,syntax_error

◆ do_read_stream()

void mysql_harness::Config::do_read_stream ( std::istream &  input)
protectedvirtual

Function to read the configuration from a stream.

Exceptions
syntax_error,maybebad_section (not sure if it can happen)
Note
This function is guaranteeed to be called for reading all configurations so it can be overridden to handle post- or pre-parsing actions.

◆ empty()

bool mysql_harness::Config::empty ( ) const

Check if the configuration is empty.

Returns
true if there are any sections in the configuration (not counting the default section), false otherwise.

◆ get() [1/4]

Config::SectionList mysql_harness::Config::get ( const std::string &  section)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ get() [2/4]

Config::ConstSectionList mysql_harness::Config::get ( const std::string &  section) const

Get a list of sections having a name.

There can be several sections under the same name, but they will have different keys.

Note
The empty string is used to denote the keyless section.
Parameters
sectionSection name of sections to fetch.

◆ get() [3/4]

ConfigSection & mysql_harness::Config::get ( const std::string &  section,
const std::string &  key 
)

Get a section by name and key.

Get a section given a name and a key. Since there can be several sections with the same name (but different keys) this will always return a unique section.

Note
The empty string is used to denote the keyless section.
Parameters
sectionName of section to fetch.
keyKey for section to fetch.
Returns
Reference to section instance.
Exceptions
bad_sectionThrown if the section do not exist or if a key were used but is not allowed.

◆ get() [4/4]

const ConfigSection & mysql_harness::Config::get ( const std::string &  section,
const std::string &  key 
) const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ get_default()

std::string mysql_harness::Config::get_default ( const std::string &  option) const

◆ get_default_section()

ConfigSection & mysql_harness::Config::get_default_section ( ) const

◆ has()

bool mysql_harness::Config::has ( const std::string &  section,
const std::string &  key = std::string() 
) const

◆ has_any()

bool mysql_harness::Config::has_any ( const std::string &  section) const
Returns
True if a section exists, no matter what the key is.

◆ has_default()

bool mysql_harness::Config::has_default ( const std::string &  option) const

◆ is_reserved()

bool mysql_harness::Config::is_reserved ( const std::string &  word) const

◆ operator=()

Config & mysql_harness::Config::operator= ( const Config )
default

◆ read() [1/3]

void mysql_harness::Config::read ( const Path path)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ read() [2/3]

void mysql_harness::Config::read ( const Path path,
const std::string &  pattern 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ read() [3/3]

void mysql_harness::Config::read ( std::istream &  input)

Read configuration file from file, directory, or input stream.

If there are conflicting sections (several instance sections with identical name and key) or conflicting options (several instances of an option in the same section) in the input, an exception will be thrown.

If the input is a stream, the contents of the stream will be added to the configuration, but if the input is either a file or a directory, the exisisting contents of the configuration will be removed.

If a pattern is given, the path is assumed to be a directory and all files matching the pattern will be read into the the configuration object. The files together will be treated as if it is a single file, that is, no conflicting option values or sections are allowed and will raise an exception.

Parameters
inputInput stream to read from.
Exceptions
syntax_errorRaised if there is a syntax error in the configuration file and the configuration file have to be corrected.
bad_sectionRaised if there is a duplicate section (section with the same name and key) in the input stream.
bad_optionRaised if there is a duplicate definition of an option (same option is given twice in a section).

◆ remove() [1/2]

bool mysql_harness::Config::remove ( const SectionKey section_key)
noexcept

Remove section from configuration.

Parameters
section_keysection+key to remove.
Returns
true if section was removed, false if section did not exist

◆ remove() [2/2]

bool mysql_harness::Config::remove ( const std::string &  section,
const std::string &  key = std::string() 
)
noexcept

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ section_names()

std::list< Config::SectionKey > mysql_harness::Config::section_names ( ) const
inline

◆ sections()

Config::ConstSectionList mysql_harness::Config::sections ( ) const

Get a list of all sections in the configuration.

◆ set_default()

void mysql_harness::Config::set_default ( const std::string &  option,
const std::string &  value 
)

◆ set_reserved()

template<class SeqT >
void mysql_harness::Config::set_reserved ( const SeqT &  reserved)
inline

◆ update()

void mysql_harness::Config::update ( const Config other)

Update configuration using another configuration.

This will incorporate all the sections and options from the other configuration by adding sections that are missing and overwriting option values for sections that exist in the current configuration.

Parameters
otherConfiguration to read section, options, and values from.

Member Data Documentation

◆ allow_keys

constexpr unsigned int mysql_harness::Config::allow_keys = 1U
staticconstexpr

Flags for construction of configurations.

◆ config_overwrites_

ConfigOverwrites mysql_harness::Config::config_overwrites_
protected

◆ DEFAULT_PATTERN

constexpr const char* mysql_harness::Config::DEFAULT_PATTERN = "*.cfg"
staticconstexpr

Default pattern to used to identify configuration files.

◆ defaults_

std::shared_ptr<ConfigSection> mysql_harness::Config::defaults_
protected

◆ error_on_unsupported_option

bool mysql_harness::Config::error_on_unsupported_option {false}

◆ flags_

unsigned int mysql_harness::Config::flags_
protected

◆ reserved_

ReservedList mysql_harness::Config::reserved_
protected

◆ sections_

SectionMap mysql_harness::Config::sections_
protected

The documentation for this class was generated from the following files: