Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<map>
- #include<vector>
- #include<boost/variant.hpp>
- #include<boost/variant/get.hpp>
- #include<boost/optional.hpp>
- #include<boost/none.hpp>
- class recursive_map {
- class recursive_map_impl {
- std::map<std::string, boost::variant<boost::blank, std::string, recursive_map_impl>> map;
- bool set_value(std::string val, std::string key) {
- map[key] = val;
- return true;
- }
- template<typename... T>
- bool set_value(std::string val, std::string key, T... mappings) {
- if (boost::get<boost::blank>(&map[key])) map[key] = recursive_map_impl{};
- if (recursive_map_impl * sub_map = boost::get<recursive_map_impl>(&(map[key]))) {
- return sub_map->set_value(val, mappings...);
- }
- return false;
- }
- boost::optional<std::string> get_value(const std::string & key) {
- if (std::string * sub_string = boost::get<std::string>(&map[key])) {
- return boost::make_optional<std::string>(*sub_string);
- } else {
- return boost::none;
- }
- }
- template<typename... T>
- boost::optional<std::string> get_value(const std::string & key, T... mappings) {
- if (recursive_map_impl * sub_map = boost::get<recursive_map_impl>(&(map[key]))) {
- return sub_map->get_value(mappings...);
- }
- else {
- return boost::none;
- }
- }
- friend class recursive_map;
- };
- recursive_map_impl map;
- public:
- template<typename... T>
- auto set_value(std::string value, T... mappings) {
- return map.set_value(value, mappings...);
- }
- template<typename... T>
- auto get_value(T... mappings) {
- return map.get_value(mappings...);
- }
- };
Add Comment
Please, Sign In to add comment