OmniSketch  0.1
Oh my sketch!
OmniSketch::Util::ConfigParser Class Reference

Parse config file and return its configurations in a versatile manner. More...

#include <utils.h>

Public Member Functions

 ConfigParser (const std::string_view config_file)
 Open the config file. More...
 
bool succeed () const
 Return whether the parsing succeeds. More...
 
void setWorkingNode (const std::string_view path="")
 Set the working node in the config file. More...
 
template<typename T >
bool parseConfig (T &arg, const std::string_view arg_name, const bool error_logging=true) const
 Get the configuration and store it directly into the object. More...
 

Detailed Description

Parse config file and return its configurations in a versatile manner.

Author
dromniscience (you@d.nosp@m.omai.nosp@m.n.com)

Constructor & Destructor Documentation

◆ ConfigParser()

OmniSketch::Util::ConfigParser::ConfigParser ( const std::string_view  config_file)

Open the config file.

Parameters
config_filepath to the config file

Member Function Documentation

◆ parseConfig()

template<typename T >
bool OmniSketch::Util::ConfigParser::parseConfig ( T &  arg,
const std::string_view  arg_name,
const bool  error_logging = true 
) const
inline

Get the configuration and store it directly into the object.

Template Parameters
Tsupported types:
  • int32_t
  • size_t
  • double
  • bool
  • std::string
  • std::vector<int32_t>
  • std::vector<size_t>
  • std::vector<double>
  • std::vector<std::string>
  • toml::array
Parameters
argthe object to be written to
arg_namethe name in config file
error_loggingEnable / disable error logging when parsing fails (by default enabled)
Returns
true on success; false otherwise. Possible reasons for a false return:
  • Misspell the key name in config file.
  • Working node unset or is wrong.
Attention
On failure, the object may be in an inconsistent state, particularly when arg is a vector or an toml::array, and an error be logged. To disable error logging, which might be particularly useful when the config is meant to be optional, set logging_error to false.

◆ setWorkingNode()

void OmniSketch::Util::ConfigParser::setWorkingNode ( const std::string_view  path = "")

Set the working node in the config file.

The data about to read should always locate in the working node. See example below for a better understanding. An argument of "" (or void) means the root. This method can be invoked upon a single instances multiple times.

Parameters
pathpath to the working node

Example

Suppose the toml file is

# toml.toml
key = 2 # then this!
[A.B.C]
key = 1 # first parse this!

and you want to fetch the value for key. This code snippet works for you.

# example.cpp
using namespace OmniSketch::Util;
// open file and parse
auto parser = ConfigParser("toml.toml");
// check whether the parsing succeed
if(!parser.succeed()) exit(-1);
// set working node to [A.B.C]
parser.setWorkingNode("A.B.C");
int first_key;
parser.parseConfig(first_key, "key");
// reset working node to the root
parser.setWorkingNode();
// Equivalently, parser.setWorkingNode("");
int second_key;
parser.parseConfig(second_key, "key");

◆ succeed()

bool OmniSketch::Util::ConfigParser::succeed ( ) const

Return whether the parsing succeeds.

OmniSketch::Util
Utils of manipulating integers, parsing configuration files and so on.
Definition: utils.h:19
OmniSketch::Util::ConfigParser::ConfigParser
ConfigParser(const std::string_view config_file)
Open the config file.
Definition: utils.cpp:59