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 
62 class NX_KIT_API IniConfig
63 {
64 public:
70  static bool isEnabled();
71 
72  static void setEnabled(bool value);
73 
80  static void setOutput(std::ostream* output);
81 
83  static const char* iniFilesDir();
84 
92  static void setIniFilesDir(const char* iniFilesDir);
93 
95  explicit IniConfig(const char* iniFile);
96 
97  virtual ~IniConfig();
98 
99  IniConfig(const IniConfig&) = delete;
100  IniConfig(IniConfig&&) = delete;
101  IniConfig& operator=(const IniConfig&) = delete;
102  IniConfig& operator=(IniConfig&&) = delete;
103 
104  const char* iniFile() const;
105  const char* iniFilePath() const;
108  void reload();
109 
110  class Tweaks;
111 
112 protected:
113  #define NX_INI_FLAG(DEFAULT, PARAM, DESCRIPTION) \
114  const bool PARAM = regBoolParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
115 
116  #define NX_INI_INT(DEFAULT, PARAM, DESCRIPTION) \
117  const int PARAM = regIntParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
118 
119  #define NX_INI_STRING(DEFAULT, PARAM, DESCRIPTION) \
120  const char* const PARAM = regStringParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
121 
122  #define NX_INI_FLOAT(DEFAULT, PARAM, DESCRIPTION) \
123  const float PARAM = regFloatParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
124 
125  #define NX_INI_DOUBLE(DEFAULT, PARAM, DESCRIPTION) \
126  const double PARAM = regDoubleParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
127 
128 protected: // Used by the above macros.
129  bool regBoolParam(const bool* pValue, bool defaultValue,
130  const char* paramName, const char* description);
131 
132  int regIntParam(const int* pValue, int defaultValue,
133  const char* paramName, const char* description);
134 
135  const char* regStringParam(const char* const* pValue, const char* defaultValue,
136  const char* paramName, const char* description);
137 
138  float regFloatParam(const float* pValue, float defaultValue,
139  const char* paramName, const char* description);
140 
141  double regDoubleParam(const double* pValue, double defaultValue,
142  const char* paramName, const char* description);
143 
144 private:
145  class Impl;
146  Impl* const d;
147 };
148 
149 //-------------------------------------------------------------------------------------------------
150 
164 class NX_KIT_API IniConfig::Tweaks
165 {
166 public:
167  Tweaks()
168  {
169  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
170 
171  if (++s_tweaksInstanceCount == 1)
172  {
173  s_originalValueOfIsEnabled = isEnabled();
174  setEnabled(false);
175  }
176  }
177 
178  ~Tweaks()
179  {
180  for (const auto& guard: *m_guards)
181  guard();
182 
183  {
184  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
185 
186  if (--s_tweaksInstanceCount == 0)
187  setEnabled(s_originalValueOfIsEnabled);
188  }
189 
190  delete m_guards;
191  }
192 
193  Tweaks(const Tweaks&) = delete;
194  Tweaks(Tweaks&&) = delete;
195  Tweaks& operator=(const Tweaks&) = delete;
196  Tweaks& operator=(Tweaks&&) = delete;
197 
198  template<typename T>
199  void set(const T* field, T newValue)
200  {
201  const auto oldValue = *field;
202  T* const mutableField = const_cast<T*>(field);
203  m_guards->push_back([=]() { *mutableField = oldValue; });
204  *mutableField = newValue;
205  }
206 
207 private:
208  struct MutexHolder
209  {
210  std::mutex* const mutex = new std::mutex();
211  ~MutexHolder() { delete mutex; }
212  };
213 
214  static MutexHolder s_mutexHolder;
215  static int s_tweaksInstanceCount;
216  static bool s_originalValueOfIsEnabled;
217 
218  std::vector<std::function<void()>>* const m_guards =
219  new std::vector<std::function<void()>>();
220 };
221 
222 } // namespace kit
223 } // namespace nx
Definition: ini_config.h:62
Definition: apple_utils.h:6
Definition: ini_config.cpp:341