Fire Core  8.0.0.alpha
GIS framework per tutti gli usi
 Tutto Classi Namespace Funzioni Variabili Ridefinizioni di tipo (typedef) Tipi enumerati (enum) Valori del tipo enumerato Friend
Properties.hpp
1 // Copyright 2015 GEOSYSTEMS SRL
2 // All Rights Reserved.
3 
4 #ifndef FIRE_RESOURCES_PROPERTIES_HPP_INCLUDED
5 #define FIRE_RESOURCES_PROPERTIES_HPP_INCLUDED
6 
7 #include <boost/filesystem.hpp>
8 #include <boost/smart_ptr/shared_ptr.hpp>
9 #include <boost/property_tree/ptree.hpp>
10 #include <boost/property_tree/json_parser.hpp>
11 
12 #include <exception>
13 #include <cstddef>
14 #include <map>
15 #include <string>
16 #include <utility>
17 
18 #include "fire/export.hpp"
19 
20 #include "fire/Object.hpp"
21 
26 namespace fire {
31 namespace resources {
36 class FIRE_ENGINE_DLL Properties : public fire::Object {
37  private:
42  boost::filesystem::path path;
47  boost::shared_ptr< boost::property_tree::ptree > tree;
48 
49  public:
57  explicit Properties(const std::string& path);
62  virtual ~Properties();
76  template < class T >
77  T get(const std::string& key) {
78  T value;
79  try {
80  try {
81  value = this->tree->get< T >(key);
82  } catch (const std::exception&) {
83  boost::property_tree::ptree tree;
84  tree = this->tree->get_child(key);
85  std::stringstream stringStream;
86  boost::property_tree::write_json(stringStream, tree, false);
87  stringStream >> value;
88  }
89  } catch (const std::exception& exception) {
90  throw;
91  }
92  return value;
93  }
110  template < class T >
111  T get(const std::string& key, const T& defaultValue) {
112  T value;
113  try {
114  try {
115  value = this->tree->get< T >(key, defaultValue);
116  } catch (const std::exception& exception) {
117  boost::property_tree::ptree tree;
118  tree = this->tree->get_child(key);
119  std::stringstream stringStream;
120  boost::property_tree::write_json(stringStream, tree, false);
121  stringStream >> value;
122  }
123  } catch (const std::exception& exception) {
124  throw;
125  }
126  return value;
127  }
141  template < class T >
142  void put(const std::string& key, const T& value) {
143  try {
144  try {
145  boost::property_tree::ptree tree;
146  std::stringstream stream;
147  stream << value;
148  boost::property_tree::read_json(stream, tree);
149  this->tree->put_child(key, tree);
150  } catch (const std::exception& exception) {
151  this->tree->put< T >(key, value);
152  }
153  } catch (const std::exception& exception) {
154  throw;
155  }
156  }
164  void update(bool pretty = true);
165 };
166 } // namespace resources
167 } // namespace fire
168 
169 #endif
Classe per la gestione di un oggetto.
Definition: Object.hpp:29
Classe per la gestione delle proprietà contenute in una risorsa.
Definition: Properties.hpp:36
void put(const std::string &key, const T &value)
Inserisce un valore di una proprietà
Definition: Properties.hpp:142