1 #ifndef CONFIGURATION_PARSER_HPP
2 #define CONFIGURATION_PARSER_HPP
11 #include <unordered_map>
12 #include <boost/format.hpp>
13 #include <boost/any.hpp>
14 #include <boost/optional.hpp>
37 template<
typename T >
39 const std::string& _key,
41 root_[_key] = boost::any(_val);
42 return boost::any_cast<T&>(
root_[_key]);
46 template<
typename T >
51 if ( _keys.empty() ) {
52 THROW( -1,
"\"set\" requires at least one key");
54 boost::optional<boost::any&> cur_val;
55 for (
const auto& key : _keys ) {
57 cur_val.reset(
root_[key]);
61 if ( cur_val->empty()) {
62 *cur_val = std::unordered_map<std::string, boost::any>();
65 cur_val.reset(boost::any_cast<std::unordered_map<std::string, boost::any>&>(*cur_val)[key]);
66 }
catch (
const boost::bad_any_cast& ) {
71 return boost::any_cast<T&>(*cur_val);
75 template<
typename T >
76 T&
get(
const std::string& _key ) {
78 return boost::any_cast<T&>(
root_.at(_key));
79 }
catch (
const boost::bad_any_cast& ) {
81 }
catch (
const std::out_of_range& ) {
87 template<
typename T >
90 if ( _keys.empty() ) {
91 THROW( -1,
"\"get\" requires at least one key");
93 boost::optional<boost::any&> cur_val;
94 for (
const auto& key : _keys ) {
97 cur_val.reset(
root_.at(key));
100 cur_val.reset(boost::any_cast<std::unordered_map<std::string, boost::any>&>(*cur_val).at(key));
101 }
catch (
const boost::bad_any_cast& ) {
105 }
catch (
const std::out_of_range& ) {
110 return boost::any_cast<T&>(*cur_val);
111 }
catch (
const boost::bad_any_cast& ) {
118 template<
typename T >
120 auto find_it =
root_.find(_key);
121 if ( find_it ==
root_.end() ) {
122 THROW (
KEY_NOT_FOUND, (boost::format(
"key \"%s\" not found in map.") % _key).str() );
124 T val = find_it->second;
125 root_.erase(find_it);
129 void remove(
const std::string& _key);
131 std::unordered_map<std::string, boost::any>&
map() {
137 const std::string& );
142 const std::unordered_map<std::string, boost::any>& );
145 std::unordered_map<std::string, boost::any>
root_;
149 std::string
to_env(
const std::string& );
153 #endif // CONFIGURATION_PARSER_HPP