nx_video_source_sdk  1.0
Video Source 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 
78 class NX_KIT_API IniConfig
79 {
80 public:
86  static bool isEnabled();
87 
88  static void setEnabled(bool value);
89 
96  static void setOutput(std::ostream* output);
97 
99  static const char* iniFilesDir();
100 
108  static void setIniFilesDir(const char* iniFilesDir);
109 
111  explicit IniConfig(const char* iniFile);
112 
113  virtual ~IniConfig();
114 
115  IniConfig(const IniConfig&) = delete;
116  IniConfig(IniConfig&&) = delete;
117  IniConfig& operator=(const IniConfig&) = delete;
118  IniConfig& operator=(IniConfig&&) = delete;
119 
120  const char* iniFile() const;
121  const char* iniFilePath() const;
124  void reload();
125 
126  class Tweaks;
127 
128 protected:
129  #define NX_INI_FLAG(DEFAULT, PARAM, DESCRIPTION) \
130  const bool PARAM = regBoolParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
131 
132  #define NX_INI_INT(DEFAULT, PARAM, DESCRIPTION) \
133  const int PARAM = regIntParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
134 
135  #define NX_INI_STRING(DEFAULT, PARAM, DESCRIPTION) \
136  const char* const PARAM = regStringParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
137 
138  #define NX_INI_FLOAT(DEFAULT, PARAM, DESCRIPTION) \
139  const float PARAM = regFloatParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
140 
141  #define NX_INI_DOUBLE(DEFAULT, PARAM, DESCRIPTION) \
142  const double PARAM = regDoubleParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
143 
144 protected: // Used by the above macros.
145  bool regBoolParam(const bool* pValue, bool defaultValue,
146  const char* paramName, const char* description);
147 
148  int regIntParam(const int* pValue, int defaultValue,
149  const char* paramName, const char* description);
150 
151  const char* regStringParam(const char* const* pValue, const char* defaultValue,
152  const char* paramName, const char* description);
153 
154  float regFloatParam(const float* pValue, float defaultValue,
155  const char* paramName, const char* description);
156 
157  double regDoubleParam(const double* pValue, double defaultValue,
158  const char* paramName, const char* description);
159 
160 private:
161  class Impl;
162  Impl* const d;
163 };
164 
165 //-------------------------------------------------------------------------------------------------
166 
180 class NX_KIT_API IniConfig::Tweaks
181 {
182 public:
183  Tweaks()
184  {
185  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
186 
187  if (++s_tweaksInstanceCount == 1)
188  {
189  s_originalValueOfIsEnabled = isEnabled();
190  setEnabled(false);
191  }
192  }
193 
194  ~Tweaks()
195  {
196  for (const auto& guard: *m_guards)
197  guard();
198 
199  {
200  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
201 
202  if (--s_tweaksInstanceCount == 0)
203  setEnabled(s_originalValueOfIsEnabled);
204  }
205 
206  delete m_guards;
207  }
208 
209  Tweaks(const Tweaks&) = delete;
210  Tweaks(Tweaks&&) = delete;
211  Tweaks& operator=(const Tweaks&) = delete;
212  Tweaks& operator=(Tweaks&&) = delete;
213 
214  template<typename T>
215  void set(const T* field, T newValue)
216  {
217  const auto oldValue = *field;
218  T* const mutableField = const_cast<T*>(field);
219  m_guards->push_back([=]() { *mutableField = oldValue; });
220  *mutableField = newValue;
221  }
222 
223 private:
224  struct MutexHolder
225  {
226  std::mutex* const mutex = new std::mutex();
227  ~MutexHolder() { delete mutex; }
228  };
229 
230  static MutexHolder s_mutexHolder;
231  static int s_tweaksInstanceCount;
232  static bool s_originalValueOfIsEnabled;
233 
234  std::vector<std::function<void()>>* const m_guards =
235  new std::vector<std::function<void()>>();
236 };
237 
238 } // namespace kit
239 } // namespace nx
Definition: ini_config.h:78
Definition: apple_utils.h:6
Definition: ini_config.cpp:326