DASH  0.3.0
Config.h
1 #ifndef DASH__UTIL__CONFIG_H__
2 #define DASH__UTIL__CONFIG_H__
3 
4 #include <dash/internal/Logging.h>
5 #include <dash/util/StaticConfig.h>
6 
7 #include <string>
8 #include <sstream>
9 #include <unordered_map>
10 #include <functional>
11 #include <type_traits>
12 #include <cstdlib>
13 
14 
15 namespace dash {
16 namespace util {
17 
41 class Config
42 {
43 private:
44 
45  typedef void (*callback_fun)(const std::string &);
46 
47  static std::unordered_map<std::string, callback_fun> callbacks_;
48  static std::unordered_map<std::string, std::string> config_values_;
49 
50 private:
51  static std::string get_str(
52  const std::string & key)
53  {
54  std::string value;
55  auto kv = Config::config_values_.find(key);
56  if (kv != Config::config_values_.end()) {
57  value = kv->second;
58  }
59  DASH_LOG_TRACE("util::Config::get_str >", key, "->", value);
60  return value;
61  }
62 
63  static void set_str(
64  const std::string & key,
65  const std::string & value)
66  {
67  DASH_LOG_TRACE("util::Config::set_str >", key, "->", value);
68  Config::config_values_[key] = value;
69  }
70 
71 
72 public:
73 
75  // Config::get<T>(key)
77 
78  template<typename ValueT>
79  static
80  typename std::enable_if<
81  std::is_same<ValueT, bool>::value, ValueT>::type
82  get(const std::string & key)
83  {
84  bool value = (get_str(key) == "1") ||
85  (get_str(key + "_BOOL") == "1");
86 
87  DASH_LOG_TRACE("util::Config::get<bool>", key, "->", value);
88  return value;
89  }
90 
91  template<typename ValueT>
92  static
93  typename std::enable_if<
94  std::is_floating_point<ValueT>::value, ValueT>::type
95  get(const std::string & key)
96  {
97  ValueT value = std::stod(get_str(key).c_str());
98  DASH_LOG_TRACE("util::Config::get<fp>", key, "->", value);
99  return value;
100  }
101 
102  template<typename ValueT>
103  static
104  typename std::enable_if<
105  !std::is_same<ValueT, bool>::value &&
106  std::is_integral<ValueT>::value, ValueT >::type
107  get(const std::string & key)
108  {
109  ValueT value = std::strtoll(get_str(key).c_str(), nullptr, 10);
110  DASH_LOG_TRACE("util::Config::get<integral>", key, "->", value);
111  return value;
112  }
113 
114  template<typename ValueT>
115  static
116  typename std::enable_if<
117  !std::is_floating_point<ValueT>::value &&
118  !std::is_integral<ValueT>::value, ValueT>::type
119  get(const std::string & key)
120  {
121  std::istringstream ss(get_str(key));
122  ValueT value;
123  ss >> value;
124 
125  DASH_LOG_TRACE("util::Config::get<T>", key, "->", value);
126  return value;
127  }
128 
130  // Config::set(key, T)
132 
133  template<typename ValueT>
134  static
135  typename std::enable_if<std::is_arithmetic<ValueT>::value, void>::type
136  set(
137  const std::string & key,
138  ValueT value)
139  {
140  DASH_LOG_TRACE("util::Config::set(string,T)", key, value);
141  std::ostringstream ss;
142  ss << value;
143  std::string value_s = ss.str();
144 
145  Config::config_values_[key] = value_s;
146  Config::on_change(key, value_s);
147  }
148 
149  static void
150  set(
151  const std::string & key,
152  std::string value);
153 
154  static inline void
155  set(
156  const std::string & key,
157  const char * cstr)
158  {
159  set(key, std::string(cstr));
160  }
161 
163  // Config::begin(), Config::end()
165 
166  static typename std::unordered_map<std::string, std::string>::iterator
167  begin() {
168  return Config::config_values_.begin();
169  }
170 
171  static typename std::unordered_map<std::string, std::string>::iterator
172  end() {
173  return Config::config_values_.end();
174  }
175 
176  static bool is_set(const std::string & key)
177  {
178  auto kv = Config::config_values_.find(key);
179  if (kv == Config::config_values_.end()) {
180  return false;
181  }
182  return true;
183  }
184 
185  static void init();
186 
187 private:
188 
189  static void on_change(
190  const std::string & key,
191  const std::string & value)
192  {
193  auto callback_it = Config::callbacks_.find(key);
194  if (Config::callbacks_.end() != callback_it) {
195  ((*callback_it).second)(value);
196  }
197  }
198 
199  static void dash_enable_logging_callback(
200  const std::string & value)
201  {
202  if (value == "1") {
203  dash::internal::logging::enable_log();
204  DASH_LOG_TRACE("util::Config::set", "Log enabled");
205  } else if (value == "0") {
206  DASH_LOG_TRACE("util::Config::set", "Disabling log");
207  dash::internal::logging::disable_log();
208  }
209  }
210 
211 };
212 
213 } // namespace util
214 } // namespace dash
215 
216 #endif // DASH__UTIL__CONFIG_H__
This class is a simple memory pool which holds allocates elements of size ValueType.
Definition: AllOf.h:8