irods  4.2.8
About: iRODS (the integrated Rule Oriented Data System) is a distributed data-management system for creating data grids, digital libraries, persistent archives, and real-time data systems.
  Fossies Dox: irods-4.2.8.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

irods_configuration_parser.hpp
Go to the documentation of this file.
1 #ifndef CONFIGURATION_PARSER_HPP
2 #define CONFIGURATION_PARSER_HPP
3 
4 #include "irods_exception.hpp"
5 #include "irods_error.hpp"
6 #include "rodsErrorTable.h"
7 
8 #include <vector>
9 #include <string>
10 
11 #include <unordered_map>
12 #include <boost/format.hpp>
13 #include <boost/any.hpp>
14 #include <boost/optional.hpp>
15 
16 #include "json.hpp"
17 
18 namespace irods {
19 
21  public:
22  typedef std::vector< std::string > key_path_t;
23 
27  configuration_parser( const std::string& );
29  void clear();
30  error load( const std::string& );
31  error write( const std::string& );
33 
34  bool has_entry(
35  const std::string& ); // key
36 
37  template< typename T >
38  T& set(
39  const std::string& _key,
40  const T& _val ) {
41  root_[_key] = boost::any(_val);
42  return boost::any_cast<T&>(root_[_key]);
43 
44  } // set
45 
46  template< typename T >
47  T& set(
48  const key_path_t& _keys,
49  const T& _val ) {
50 
51  if ( _keys.empty() ) {
52  THROW( -1, "\"set\" requires at least one key");
53  }
54  boost::optional<boost::any&> cur_val;
55  for ( const auto& key : _keys ) {
56  if ( !cur_val ) {
57  cur_val.reset(root_[key]);
58  continue;
59  }
60 
61  if ( cur_val->empty()) {
62  *cur_val = std::unordered_map<std::string, boost::any>();
63  }
64  try {
65  cur_val.reset(boost::any_cast<std::unordered_map<std::string, boost::any>&>(*cur_val)[key]);
66  } catch ( const boost::bad_any_cast& ) {
67  THROW( INVALID_ANY_CAST, "value was not a map");
68  }
69  }
70  *cur_val = _val;
71  return boost::any_cast<T&>(*cur_val);
72 
73  } // set with path
74 
75  template< typename T >
76  T& get( const std::string& _key ) {
77  try {
78  return boost::any_cast<T&>(root_.at(_key));
79  } catch ( const boost::bad_any_cast& ) {
80  THROW( INVALID_ANY_CAST, (boost::format("value at %s was incorrect type") % _key).str());
81  } catch ( const std::out_of_range& ) {
82  THROW ( KEY_NOT_FOUND, (boost::format("key \"%s\" not found in map.") % _key).str() );
83  }
84 
85  } // get
86 
87  template< typename T >
88  T& get(const key_path_t& _keys) {
89 
90  if ( _keys.empty() ) {
91  THROW( -1, "\"get\" requires at least one key");
92  }
93  boost::optional<boost::any&> cur_val;
94  for ( const auto& key : _keys ) {
95  try {
96  if ( !cur_val ) {
97  cur_val.reset(root_.at(key));
98  } else {
99  try {
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& ) {
102  THROW( INVALID_ANY_CAST, "value was not a map");
103  }
104  }
105  } catch ( const std::out_of_range& ) {
106  THROW ( KEY_NOT_FOUND, (boost::format("key \"%s\" not found in map.") % key).str() );
107  }
108  }
109  try {
110  return boost::any_cast<T&>(*cur_val);
111  } catch ( const boost::bad_any_cast& ) {
112  THROW(INVALID_ANY_CAST, "value was incorrect type");
113  }
114 
115  } // get with path
116 
117 
118  template< typename T >
119  T remove(const std::string& _key) {
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() );
123  }
124  T val = find_it->second;
125  root_.erase(find_it);
126  return val;
127  }
128 
129  void remove(const std::string& _key);
130 
131  std::unordered_map<std::string, boost::any>& map() {
132  return root_;
133  }
134 
135  private:
137  const std::string& ); // file name
138 
139  boost::any convert_json(const nlohmann::json&);
140 
142  const std::unordered_map<std::string, boost::any>& ); // object to swap in
143 
144  std::string file_name_; // full path to file
145  std::unordered_map<std::string, boost::any> root_; // root config object
146 
147  }; // class configuration_parser
148 
149  std::string to_env( const std::string& );
150 
151 }; // namespace irods
152 
153 #endif // CONFIGURATION_PARSER_HPP
irods::configuration_parser::write
error write()
irods::configuration_parser::map
std::unordered_map< std::string, boost::any > & map()
Definition: irods_configuration_parser.hpp:131
THROW
#define THROW(_code, _msg)
Definition: irods_exception.hpp:68
irods::configuration_parser::remove
T remove(const std::string &_key)
Definition: irods_configuration_parser.hpp:119
irods::configuration_parser::clear
void clear()
Definition: irods_configuration_parser.cpp:51
irods_exception.hpp
irods::configuration_parser::get
T & get(const std::string &_key)
Definition: irods_configuration_parser.hpp:76
irods::to_env
std::string to_env(const std::string &)
Definition: irods_configuration_parser.cpp:172
irods::configuration_parser::get
T & get(const key_path_t &_keys)
Definition: irods_configuration_parser.hpp:88
json
nlohmann::json json
Definition: group.cpp:5
irods::configuration_parser::set
T & set(const std::string &_key, const T &_val)
Definition: irods_configuration_parser.hpp:38
irods::configuration_parser::has_entry
bool has_entry(const std::string &)
Definition: irods_configuration_parser.cpp:67
irods::configuration_parser::file_name_
std::string file_name_
Definition: irods_configuration_parser.hpp:144
irods
Definition: apiHandler.hpp:35
irods::configuration_parser::convert_json
boost::any convert_json(const nlohmann::json &)
Definition: irods_configuration_parser.cpp:126
irods::configuration_parser
Definition: irods_configuration_parser.hpp:20
irods::configuration_parser::load
error load(const std::string &)
Definition: irods_configuration_parser.cpp:72
irods::configuration_parser::set
T & set(const key_path_t &_keys, const T &_val)
Definition: irods_configuration_parser.hpp:47
INVALID_ANY_CAST
@ INVALID_ANY_CAST
Definition: rodsErrorTable.h:771
irods::configuration_parser::root_
std::unordered_map< std::string, boost::any > root_
Definition: irods_configuration_parser.hpp:145
KEY_NOT_FOUND
@ KEY_NOT_FOUND
Definition: rodsErrorTable.h:749
irods::configuration_parser::key_path_t
std::vector< std::string > key_path_t
Definition: irods_configuration_parser.hpp:22
irods::configuration_parser::~configuration_parser
~configuration_parser()
Definition: irods_configuration_parser.cpp:21
irods::error
Definition: irods_error.hpp:23
irods::configuration_parser::write
error write(const std::string &)
irods::configuration_parser::copy_and_swap
error copy_and_swap(const std::unordered_map< std::string, boost::any > &)
Definition: irods_configuration_parser.cpp:57
rodsErrorTable.h
irods_error.hpp
irods::configuration_parser::operator=
configuration_parser & operator=(const configuration_parser &)
Definition: irods_configuration_parser.cpp:42
irods::configuration_parser::load_json_object
error load_json_object(const std::string &)
Definition: irods_configuration_parser.cpp:83
irods::configuration_parser::configuration_parser
configuration_parser()
Definition: irods_configuration_parser.cpp:17