lib45d
45Drives C++ Library API Documentation
Public Member Functions | List of all members
ffd::ConfigSubsectionGuard Class Reference

Use this to switch to a certain config subsection to get a group of values. More...

#include <45d/config/ConfigSubsectionGuard.hpp>

Public Member Functions

 ConfigSubsectionGuard (ConfigParser &config, const std::string &section)
 Construct a new Config Subsection Guard object. More...
 
 ~ConfigSubsectionGuard (void)
 Destroy the Config Subsection Guard object. More...
 

Detailed Description

Use this to switch to a certain config subsection to get a group of values.

Inside ConfigParser method:

{
ConfigSubsectionGuard guard(*this, "Subsection 1");
int section_1_value = get<int>("Value", -1);
// section is switched back when guard goes out of scope
}

Any other scope:

ConfigParser config("/etc/example.conf");
{
ConfigSubsectionGuard guard(config, "Subsection 1");
int section_1_value = config.get<int>("Value", -1);
// section is switched back when guard goes out of scope
}

Full Example:

#include <45d/config/ConfigParser.hpp>
#include <45d/config/ConfigSubsectionGuard.hpp>
#include <cassert>
#include <iostream>
#include <vector>
class SubConf {
public:
std::string name_;
bool bool_test_;
int integer_test_;
unsigned unsigned_test_;
float float_test_;
double double_test_;
std::string string_test_;
void dump(void) {
std::cout << name_ << " Bool Test: " << std::boolalpha << bool_test_ << std::endl;
std::cout << name_ << " Integer Test: " << integer_test_ << std::endl;
std::cout << name_ << " Unsigned Test: " << unsigned_test_ << std::endl;
std::cout << name_ << " Float Test: " << float_test_ << std::endl;
std::cout << name_ << " Double Test: " << double_test_ << std::endl;
std::cout << name_ << " String Test: " << string_test_ << std::endl;
}
};
class DynamicSubsectionConfig : public ffd::ConfigParser {
private:
public:
bool bool_test_ = get<bool>("Bool Test", false);
int integer_test_ = get<int>("Integer Test", -1);
unsigned unsigned_test_ = get<unsigned>("Unsigned Test", (unsigned)-1);
float float_test_ = get<float>("Float Test", -1.0);
double double_test_ = get<double>("Double Test", -1.0);
std::string string_test_ = get<std::string>("String Test", "fallback");
DynamicSubsectionConfig(const std::string &path, std::vector<SubConf> &sub_confs)
: ConfigParser(path) {
for (ffd::ConfigNode *i : sub_confs_) {
{
ffd::ConfigSubsectionGuard guard(*this, i->value_);
sub_confs.emplace_back(SubConf{ i->value_,
get<bool>("Bool Test", false),
get<int>("Integer Test", -1),
get<unsigned>("Unsigned Test", (unsigned)-1),
get<float>("Float Test", -1.0),
get<double>("Double Test", -1.0),
get<std::string>("String Test", "fallback") });
}
}
}
void dump(void) {
std::cout << "Global Bool Test: " << std::boolalpha << bool_test_ << std::endl;
std::cout << "Global Integer Test: " << integer_test_ << std::endl;
std::cout << "Global Unsigned Test: " << unsigned_test_ << std::endl;
std::cout << "Global Float Test: " << float_test_ << std::endl;
std::cout << "Global Double Test: " << double_test_ << std::endl;
std::cout << "Global String Test: " << string_test_ << std::endl;
}
};
int main(int argc, char *argv[]) {
assert(argc == 2);
std::vector<SubConf> sub_confs;
DynamicSubsectionConfig config(argv[1], sub_confs);
config.dump();
for (auto i : sub_confs)
i.dump();
return 0;
}

Constructor & Destructor Documentation

◆ ConfigSubsectionGuard()

ffd::ConfigSubsectionGuard::ConfigSubsectionGuard ( ConfigParser config,
const std::string &  section 
)
inline

Construct a new Config Subsection Guard object.

Parameters
config///< Reference to a ConfigParser or inhereting class
section///< Section name to switch to

◆ ~ConfigSubsectionGuard()

ffd::ConfigSubsectionGuard::~ConfigSubsectionGuard ( void  )
inline

Destroy the Config Subsection Guard object.


The documentation for this class was generated from the following file:
ffd::ConfigSubsectionGuard
Use this to switch to a certain config subsection to get a group of values.
Definition: ConfigSubsectionGuard.hpp:49
ffd::ConfigSubsectionGuard::ConfigSubsectionGuard
ConfigSubsectionGuard(ConfigParser &config, const std::string &section)
Construct a new Config Subsection Guard object.
Definition: ConfigSubsectionGuard.hpp:57
ffd::ConfigParser
Main configuration parser class to inherit from in your code.
Definition: ConfigParser.hpp:68
ffd::ConfigNode
Class for config_map_ entries.
Definition: ConfigNode.hpp:31