nx_metadata_sdk  1.0
Metadata SDK
ini_config.h
1 // Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
2 
3 #pragma once
4 
5 #include <functional>
6 #include <iostream>
7 #include <vector>
8 #include <mutex>
9 
10 #if !defined(NX_KIT_API)
11  #define NX_KIT_API
12 #endif
13 
14 namespace nx {
15 namespace kit {
16 
67 class NX_KIT_API IniConfig
68 {
69 public:
75  static bool isEnabled();
76 
77  static void setEnabled(bool value);
78 
85  static void setOutput(std::ostream* output);
86 
88  static const char* iniFilesDir();
89 
97  static void setIniFilesDir(const char* iniFilesDir);
98 
100  explicit IniConfig(const char* iniFile);
101 
102  virtual ~IniConfig();
103 
104  IniConfig(const IniConfig&) = delete;
105  IniConfig(IniConfig&&) = delete;
106  IniConfig& operator=(const IniConfig&) = delete;
107  IniConfig& operator=(IniConfig&&) = delete;
108 
109  const char* iniFile() const;
110  const char* iniFilePath() const;
113  void reload();
114 
115  class Tweaks;
116 
117 protected:
118  #define NX_INI_FLAG(DEFAULT, PARAM, DESCRIPTION) \
119  const bool PARAM = regBoolParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
120 
121  #define NX_INI_INT(DEFAULT, PARAM, DESCRIPTION) \
122  const int PARAM = regIntParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
123 
124  #define NX_INI_STRING(DEFAULT, PARAM, DESCRIPTION) \
125  const char* const PARAM = regStringParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
126 
127  #define NX_INI_FLOAT(DEFAULT, PARAM, DESCRIPTION) \
128  const float PARAM = regFloatParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
129 
130  #define NX_INI_DOUBLE(DEFAULT, PARAM, DESCRIPTION) \
131  const double PARAM = regDoubleParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
132 
133 protected: // Used by the above macros.
134  bool regBoolParam(const bool* pValue, bool defaultValue,
135  const char* paramName, const char* description);
136 
137  int regIntParam(const int* pValue, int defaultValue,
138  const char* paramName, const char* description);
139 
140  const char* regStringParam(const char* const* pValue, const char* defaultValue,
141  const char* paramName, const char* description);
142 
143  float regFloatParam(const float* pValue, float defaultValue,
144  const char* paramName, const char* description);
145 
146  double regDoubleParam(const double* pValue, double defaultValue,
147  const char* paramName, const char* description);
148 
149 private:
150  class Impl;
151  Impl* const d;
152 };
153 
154 //-------------------------------------------------------------------------------------------------
155 
169 class NX_KIT_API IniConfig::Tweaks
170 {
171 public:
172  Tweaks()
173  {
174  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
175 
176  if (++s_tweaksInstanceCount == 1)
177  {
178  s_originalValueOfIsEnabled = isEnabled();
179  setEnabled(false);
180  }
181  }
182 
183  ~Tweaks()
184  {
185  for (const auto& guard: *m_guards)
186  guard();
187 
188  {
189  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
190 
191  if (--s_tweaksInstanceCount == 0)
192  setEnabled(s_originalValueOfIsEnabled);
193  }
194 
195  delete m_guards;
196  }
197 
198  Tweaks(const Tweaks&) = delete;
199  Tweaks(Tweaks&&) = delete;
200  Tweaks& operator=(const Tweaks&) = delete;
201  Tweaks& operator=(Tweaks&&) = delete;
202 
203  template<typename T>
204  void set(const T* field, T newValue)
205  {
206  const auto oldValue = *field;
207  T* const mutableField = const_cast<T*>(field);
208  m_guards->push_back([=]() { *mutableField = oldValue; });
209  *mutableField = newValue;
210  }
211 
212 private:
213  struct MutexHolder
214  {
215  std::mutex* const mutex = new std::mutex();
216  ~MutexHolder() { delete mutex; }
217  };
218 
219  static MutexHolder s_mutexHolder;
220  static int s_tweaksInstanceCount;
221  static bool s_originalValueOfIsEnabled;
222 
223  std::vector<std::function<void()>>* const m_guards =
224  new std::vector<std::function<void()>>();
225 };
226 
227 } // namespace kit
228 } // namespace nx
Definition: ini_config.h:67
Definition: apple_utils.h:6
Definition: ini_config.cpp:341