2016-05-30 23:58:58 -04:00
|
|
|
#pragma once
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
#include <boost/property_tree/ptree.hpp>
|
|
|
|
#include <boost/property_tree/ini_parser.hpp>
|
|
|
|
|
|
|
|
#include "exception.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
|
|
|
|
namespace config
|
|
|
|
{
|
2016-05-30 23:58:58 -04:00
|
|
|
DefineBaseException(ConfigException);
|
|
|
|
DefineChildException(UnexistingFileError, ConfigException);
|
|
|
|
DefineChildException(ParseError, ConfigException);
|
|
|
|
DefineChildException(MissingValueException, ConfigException);
|
|
|
|
DefineChildException(MissingListValueException, ConfigException);
|
|
|
|
DefineChildException(InvalidVariableException, ConfigException);
|
|
|
|
DefineChildException(InvalidReferenceException, ConfigException);
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
static std::recursive_mutex mtx;
|
|
|
|
|
|
|
|
std::string get_bar_path();
|
2016-06-20 21:59:43 -04:00
|
|
|
void set_bar_path(std::string path);
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-20 21:59:43 -04:00
|
|
|
void load(std::string path);
|
|
|
|
void load(const char *dir, std::string path);
|
2016-05-30 23:58:58 -04:00
|
|
|
// void reload();
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
boost::property_tree::ptree get_tree();
|
|
|
|
|
2016-06-20 21:59:43 -04:00
|
|
|
std::string build_path(std::string section, std::string key);
|
2016-05-19 10:41:06 -04:00
|
|
|
|
2016-06-20 12:44:03 -04:00
|
|
|
std::string get_file_path();
|
|
|
|
|
2016-05-19 10:41:06 -04:00
|
|
|
template<typename T>
|
2016-06-20 21:59:43 -04:00
|
|
|
T dereference_var(std::string ref_section, std::string ref_key, std::string var, const T ref_val)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(config::mtx);
|
|
|
|
|
|
|
|
std::string::size_type n, m;
|
|
|
|
|
|
|
|
if ((n = var.find("${")) != 0) return ref_val;
|
|
|
|
if ((m = var.find("}")) != var.length()-1) return ref_val;
|
|
|
|
|
|
|
|
auto path = var.substr(2, m-2);
|
|
|
|
auto ref_path = build_path(ref_section, ref_key);
|
|
|
|
|
|
|
|
if ((n = path.find(".")) == std::string::npos)
|
|
|
|
throw InvalidVariableException("Invalid variable "+ ref_path +" => ${"+ path +"}");
|
|
|
|
|
|
|
|
auto section = string::replace(path.substr(0, n), "BAR", get_bar_path());
|
|
|
|
auto key = path.substr(n+1, path.length()-n-1);
|
|
|
|
|
|
|
|
auto val = get_tree().get_optional<T>(build_path(section, key));
|
|
|
|
|
|
|
|
if (val == boost::none)
|
|
|
|
throw InvalidReferenceException("Variable defined at ["+ ref_path +"] points to a non existing parameter: ["+ build_path(section, key) +"]");
|
|
|
|
|
|
|
|
auto str_val = get_tree().get<std::string>(build_path(section, key));
|
|
|
|
|
|
|
|
return dereference_var<T>(section, key, str_val, val.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-06-20 21:59:43 -04:00
|
|
|
T get(std::string section, std::string key)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(config::mtx);
|
|
|
|
|
|
|
|
auto val = get_tree().get_optional<T>(build_path(section, key));
|
|
|
|
|
|
|
|
if (val == boost::none)
|
|
|
|
throw MissingValueException("Missing property \""+ key +"\" in section ["+ section +"]");
|
|
|
|
|
|
|
|
auto str_val = get_tree().get<std::string>(build_path(section, key));
|
|
|
|
|
|
|
|
return dereference_var<T>(section, key, str_val, val.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-06-20 21:59:43 -04:00
|
|
|
T get(std::string section, std::string key, T default_value)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(config::mtx);
|
|
|
|
|
|
|
|
auto val = get_tree().get_optional<T>(build_path(section, key));
|
|
|
|
auto str_val = get_tree().get_optional<std::string>(build_path(section, key));
|
|
|
|
|
|
|
|
return dereference_var<T>(section, key, str_val.get_value_or(""), val.get_value_or(default_value));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-06-20 21:59:43 -04:00
|
|
|
std::vector<T> get_list(std::string section, std::string key)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(config::mtx);
|
|
|
|
|
|
|
|
std::vector<T> vec;
|
|
|
|
boost::optional<T> value;
|
|
|
|
|
2016-05-25 20:14:56 -04:00
|
|
|
while ((value = get_tree().get_optional<T>(build_path(section, key) + "-"+ std::to_string(vec.size()))) != boost::none) {
|
|
|
|
auto str_val = get_tree().get<std::string>(build_path(section, key) + "-"+ std::to_string(vec.size()));
|
2016-05-19 10:41:06 -04:00
|
|
|
vec.emplace_back(dereference_var<T>(section, key, str_val, value.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
2016-05-25 20:14:56 -04:00
|
|
|
throw MissingListValueException("Missing property \""+ key + "-0\" in section ["+ section +"]");
|
2016-05-19 10:41:06 -04:00
|
|
|
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2016-06-20 21:59:43 -04:00
|
|
|
std::vector<T> get_list(std::string section, std::string key, std::vector<T> default_value)
|
2016-05-19 10:41:06 -04:00
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(config::mtx);
|
|
|
|
|
|
|
|
std::vector<T> vec;
|
|
|
|
boost::optional<T> value;
|
|
|
|
|
2016-05-25 20:14:56 -04:00
|
|
|
while ((value = get_tree().get_optional<T>(build_path(section, key) + "-"+ std::to_string(vec.size()))) != boost::none) {
|
|
|
|
auto str_val = get_tree().get<std::string>(build_path(section, key) + "-"+ std::to_string(vec.size()));
|
2016-05-19 10:41:06 -04:00
|
|
|
vec.emplace_back(dereference_var<T>(section, key, str_val, value.get()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vec.empty())
|
|
|
|
return default_value;
|
|
|
|
else
|
|
|
|
return vec;
|
|
|
|
}
|
|
|
|
}
|