EUDAQ
 All Classes Namespaces Files Functions Variables Pages
Configuration.hh
1 #ifndef EUDAQ_INCLUDED_Configuration
2 #define EUDAQ_INCLUDED_Configuration
3 
4 #include "eudaq/Utils.hh"
5 #include "eudaq/Exception.hh"
6 #include "eudaq/Platform.hh"
7 #include <string>
8 #include <map>
9 
10 namespace eudaq {
11 
12  class DLLEXPORT Configuration {
13  public:
14  Configuration(const std::string &config = "",
15  const std::string &section = "");
16  Configuration(std::istream &conffile, const std::string &section = "");
17  Configuration(const Configuration &other);
18  void Save(std::ostream &file) const;
19  void Load(std::istream &file, const std::string &section);
20  bool SetSection(const std::string &section) const;
21  bool SetSection(const std::string &section);
22  std::string operator[](const std::string &key) const {
23  return GetString(key);
24  }
25  std::string Get(const std::string &key, const std::string &def) const;
26  double Get(const std::string &key, double def) const;
27  int64_t Get(const std::string &key, int64_t def) const;
28  uint64_t Get(const std::string &key, uint64_t def) const;
29  template <typename T> T Get(const std::string &key, T def) const {
30  return eudaq::from_string(Get(key, to_string(def)), def);
31  }
32  int Get(const std::string &key, int def) const;
33  template <typename T>
34  T Get(const std::string &key, const std::string fallback,
35  const T &def) const {
36  return Get(key, Get(fallback, def));
37  }
38  std::string Get(const std::string &key, const char *def) const {
39  std::string ret(Get(key, std::string(def)));
40  return ret;
41  }
42  std::string Get(const std::string &key, const std::string fallback,
43  std::string def) const {
44  return Get(key, Get(fallback, def));
45  }
46  // std::string Get(const std::string & key, const std::string & def = "");
47  template <typename T> void Set(const std::string &key, const T &val);
48  std::string Name() const;
49  Configuration &operator=(const Configuration &other);
50  void Print(std::ostream &out) const;
51  void Print() const;
52 
53  private:
54  std::string GetString(const std::string &key) const;
55  void SetString(const std::string &key, const std::string &val);
56  typedef std::map<std::string, std::string> section_t;
57  typedef std::map<std::string, section_t> map_t;
58  map_t m_config;
59  mutable std::string m_section;
60  mutable section_t *m_cur;
61  };
62 
63  inline std::ostream &operator<<(std::ostream &os, const Configuration &c) {
64  c.Save(os);
65  return os;
66  }
67 
68  template <typename T>
69  inline void Configuration::Set(const std::string &key, const T &val) {
70  SetString(key, to_string(val));
71  }
72 }
73 
74 #endif // EUDAQ_INCLUDED_Configuration
Definition: Configuration.hh:12
T DLLEXPORT from_string(const std::string &x, const T &def=0)
Definition: Utils.hh:115
std::string to_string(const T &x, int digits=0)
Definition: Utils.hh:54