32 key_not_found(
const std::string &key): std::runtime_error(
"Key not found: \"" + key +
"\"") {}
44 class config :
public std::map<std::string, std::string> {
61 initialize(key_value_pairs,
sizeof(T)/
sizeof(
char*[2]));
72 void initialize(
const char*
const key_value_pairs[][2],
const int n);
94 static std::string
parse(
const std::string& v, std::string) {
101 int r = sscanf(v.c_str(),
"%lg", &d);
105 static double parse(
const std::string& v,
double) {
107 int r = sscanf(v.c_str(),
"%lg", &d);
114 int r = sscanf(v.c_str(),
"%g", &d);
118 static float parse(
const std::string& v,
float) {
120 int r = sscanf(v.c_str(),
"%g", &d);
127 int r = sscanf(v.c_str(),
"%i", &d);
131 static int parse(
const std::string& v,
int) {
133 int r = sscanf(v.c_str(),
"%i", &d);
138 bool valid(
const std::string& key)
const{
139 return count(key) && (at(key).size() > 0);
148 template<
typename T>
bool valid(
const std::string& key , T t )
const{
159 template<
typename T> T
optional(
const std::string& key ,
const T& default_value)
const{
161 std::string value = at(key);
164 return default_value;
181 template<
typename T> T
require(
const std::string& key,
const T& = T() )
const{
183 return parse(at(key),T());