Recast  1
Game with custom magic
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
Config.hpp
Go to the documentation of this file.
1 
11 #ifndef RECAST_CONFIG_H
12 #define RECAST_CONFIG_H
13 
14 #include <boost/property_tree/ptree.hpp>
15 
24 class Config {
25 public:
26  Config(const std::string &filename);
27 
28  ~Config();
29 
30  Config(const Config &other) = delete;
31 
32  boost::property_tree::ptree &tree();
33 
34  void save();
35 
36  void load();
37 
38  static Config *instance();
39 
48  template<class T>
49  T get(const std::string &key, T defaultVar) {
50  try {
51  return tree().get<T>(key);
52  } catch (std::exception &e) {
53  tree().put(key, defaultVar);
54  return defaultVar;
55  }
56  }
57 
58  template<class T>
59  static T g(const std::string &key, T defaultVar) {
60  return instance()->get(key, defaultVar);
61  }
62 
63 private:
64  std::string filename;
65  boost::property_tree::ptree pt;
66 };
67 
68 
69 #endif //RECAST_CONFIG_H
void load()
Definition: Config.cpp:62
Config(const std::string &filename)
void save()
Definition: Config.cpp:50
boost::property_tree::ptree & tree()
Definition: Config.cpp:46
Config class.
Definition: Config.hpp:24
static Config * instance()
Definition: Config.cpp:27
T get(const std::string &key, T defaultVar)
Definition: Config.hpp:49
~Config()
Definition: Config.cpp:66
static T g(const std::string &key, T defaultVar)
Definition: Config.hpp:59