Documentation
Class to inherit: ffd::ConfigParser
Usage
Create a config class that inherits from ffd::ConfigParser, and call ffd::ConfigParser::get() to initialize the config class's members.
Example Usage
Fail Flag
#include <45d/config/ConfigParser.hpp>
#include <cassert>
#include <iostream>
bool parse_failed_ = false;
public:
bool bool_test_ = get<bool>("Bool Test", &parse_failed_);
int integer_test_ = get<int>("Integer Test", &parse_failed_);
unsigned unsigned_test_ = get<unsigned>("Unsigned Test", &parse_failed_);
float float_test_ = get<float>("Float Test", &parse_failed_);
double double_test_ = get<double>("Double Test", &parse_failed_);
std::string string_test_ = get<std::string>("String Test", &parse_failed_);
ConfigFailFlag(std::string path) :
ffd::ConfigParser(path) {
if (parse_failed_)
"################ Config Fail Flag ################\nOne or more options missing from configuration.");
}
void dump(void) {
std::cout << "Bool Test: " << std::boolalpha << bool_test_ << std::endl;
std::cout << "Integer Test: " << integer_test_ << std::endl;
std::cout << "Unsigned Test: " << unsigned_test_ << std::endl;
std::cout << "Float Test: " << float_test_ << std::endl;
std::cout << "Double Test: " << double_test_ << std::endl;
std::cout << "String Test: " << string_test_ << std::endl;
}
bool ok(void) {
return !parse_failed_;
}
};
int main(int argc, char *argv[]) {
assert(argc == 2);
{
try {
ConfigFailFlag config(argv[1]);
config.dump();
std::cerr << err.
what() << std::endl;
}
}
return 0;
}
Fallback Values
#include <45d/config/ConfigParser.hpp>
#include <cassert>
#include <iostream>
public:
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");
ConfigFallback(std::string path) :
ffd::ConfigParser(path) {}
void dump(void) {
std::cout << "Integer Test: " << integer_test_ << std::endl;
std::cout << "Unsigned Test: " << unsigned_test_ << std::endl;
std::cout << "Float Test: " << float_test_ << std::endl;
std::cout << "Double Test: " << double_test_ << std::endl;
std::cout << "String Test: " << string_test_ << std::endl;
}
};
int main(int argc, char *argv[]) {
assert(argc == 2);
{
ConfigFallback config(argv[1]);
config.dump();
}
return 0;
}
Static Subsections
#include <45d/config/ConfigParser.hpp>
#include <cassert>
#include <iostream>
private:
bool parse_failed_ = false;
public:
bool global_bool_test_ = get<bool>("Bool Test", &parse_failed_);
int global_integer_test_ = get<int>("Integer Test", &parse_failed_);
unsigned global_unsigned_test_ = get<unsigned>("Unsigned Test", &parse_failed_);
float global_float_test_ = get<float>("Float Test", &parse_failed_);
double global_double_test_ = get<double>("Double Test", &parse_failed_);
std::string global_string_test_ = get<std::string>("String Test", &parse_failed_);
bool section1_bool_test_ = get_from<bool>("Section 1", "Bool Test", &parse_failed_);
int section1_integer_test_ = get_from<int>("Section 1", "Integer Test", &parse_failed_);
unsigned section1_unsigned_test_ =
get_from<unsigned>("Section 1", "Unsigned Test", &parse_failed_);
float section1_float_test_ = get_from<float>("Section 1", "Float Test", &parse_failed_);
double section1_double_test_ = get_from<double>("Section 1", "Double Test", &parse_failed_);
std::string section1_string_test_ =
get_from<std::string>("Section 1", "String Test", &parse_failed_);
bool section2_bool_test_ = get_from<bool>("Section 2", "Bool Test", &parse_failed_);
int section2_integer_test_ = get_from<int>("Section 2", "Integer Test", &parse_failed_);
unsigned section2_unsigned_test_ =
get_from<unsigned>("Section 2", "Unsigned Test", &parse_failed_);
float section2_float_test_ = get_from<float>("Section 2", "Float Test", &parse_failed_);
double section2_double_test_ = get_from<double>("Section 2", "Double Test", &parse_failed_);
std::string section2_string_test_ =
get_from<std::string>("Section 2", "String Test", &parse_failed_);
ConfigSubSections(std::string path) :
ffd::ConfigParser(path) {}
void dump(void) {
std::cout << "Global Bool Test: " << std::boolalpha << global_bool_test_ << std::endl;
std::cout << "Global Integer Test: " << global_integer_test_ << std::endl;
std::cout << "Global Unsigned Test: " << global_unsigned_test_ << std::endl;
std::cout << "Global Float Test: " << global_float_test_ << std::endl;
std::cout << "Global Double Test: " << global_double_test_ << std::endl;
std::cout << "Global String Test: " << global_string_test_ << std::endl;
std::cout << "Section 1 Bool Test: " << std::boolalpha << section1_bool_test_ << std::endl;
std::cout << "Section 1 Integer Test: " << section1_integer_test_ << std::endl;
std::cout << "Section 1 Unsigned Test: " << section1_unsigned_test_ << std::endl;
std::cout << "Section 1 Float Test: " << section1_float_test_ << std::endl;
std::cout << "Section 1 Double Test: " << section1_double_test_ << std::endl;
std::cout << "Section 1 String Test: " << section1_string_test_ << std::endl;
std::cout << "Section 2 Bool Test: " << std::boolalpha << section2_bool_test_ << std::endl;
std::cout << "Section 2 Integer Test: " << section2_integer_test_ << std::endl;
std::cout << "Section 2 Unsigned Test: " << section2_unsigned_test_ << std::endl;
std::cout << "Section 2 Float Test: " << section2_float_test_ << std::endl;
std::cout << "Section 2 Double Test: " << section2_double_test_ << std::endl;
std::cout << "Section 2 String Test: " << section2_string_test_ << std::endl;
}
};
int main(int argc, char *argv[]) {
assert(argc == 2);
ConfigSubSections config(argv[1]);
config.dump();
return 0;
}
Dynamic Subsections
using ffd::ConfigSubsectionGuard
#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;
}
};
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) {
{
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;
}