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  enum class ParamType
129  {
130  boolean,
131  string,
132  integer,
133  float_,
134  double_
135  };
136 
141  bool getParamTypeAndValue(
142  const char* paramName, ParamType* outType, const void** outData) const;
143 
144 protected:
145  #define NX_INI_FLAG(DEFAULT, PARAM, DESCRIPTION) \
146  const bool PARAM = regBoolParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
147 
148  #define NX_INI_INT(DEFAULT, PARAM, DESCRIPTION) \
149  const int PARAM = regIntParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
150 
151  #define NX_INI_STRING(DEFAULT, PARAM, DESCRIPTION) \
152  const char* const PARAM = regStringParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
153 
154  #define NX_INI_FLOAT(DEFAULT, PARAM, DESCRIPTION) \
155  const float PARAM = regFloatParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
156 
157  #define NX_INI_DOUBLE(DEFAULT, PARAM, DESCRIPTION) \
158  const double PARAM = regDoubleParam(&PARAM, DEFAULT, #PARAM, DESCRIPTION)
159 
160 protected: // Used by the above macros.
161  bool regBoolParam(const bool* pValue, bool defaultValue,
162  const char* paramName, const char* description);
163 
164  int regIntParam(const int* pValue, int defaultValue,
165  const char* paramName, const char* description);
166 
167  const char* regStringParam(const char* const* pValue, const char* defaultValue,
168  const char* paramName, const char* description);
169 
170  float regFloatParam(const float* pValue, float defaultValue,
171  const char* paramName, const char* description);
172 
173  double regDoubleParam(const double* pValue, double defaultValue,
174  const char* paramName, const char* description);
175 
176 private:
177  class Impl;
178  Impl* const d;
179 };
180 
181 //-------------------------------------------------------------------------------------------------
182 
196 class NX_KIT_API IniConfig::Tweaks
197 {
198 public:
199  Tweaks()
200  {
201  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
202 
203  if (++s_tweaksInstanceCount == 1)
204  {
205  s_originalValueOfIsEnabled = isEnabled();
206  setEnabled(false);
207  }
208  }
209 
210  ~Tweaks()
211  {
212  for (const auto& guard: *m_guards)
213  guard();
214 
215  {
216  const std::lock_guard<std::mutex> lock(*s_mutexHolder.mutex);
217 
218  if (--s_tweaksInstanceCount == 0)
219  setEnabled(s_originalValueOfIsEnabled);
220  }
221 
222  delete m_guards;
223  }
224 
225  Tweaks(const Tweaks&) = delete;
226  Tweaks(Tweaks&&) = delete;
227  Tweaks& operator=(const Tweaks&) = delete;
228  Tweaks& operator=(Tweaks&&) = delete;
229 
230  template<typename T>
231  void set(const T* field, T newValue)
232  {
233  const auto oldValue = *field;
234  T* const mutableField = const_cast<T*>(field);
235  m_guards->push_back([=]() { *mutableField = oldValue; });
236  *mutableField = newValue;
237  }
238 
239 private:
240  struct MutexHolder
241  {
242  std::mutex* const mutex = new std::mutex();
243  ~MutexHolder() { delete mutex; }
244  };
245 
246  static MutexHolder s_mutexHolder;
247  static int s_tweaksInstanceCount;
248  static bool s_originalValueOfIsEnabled;
249 
250  std::vector<std::function<void()>>* const m_guards =
251  new std::vector<std::function<void()>>();
252 };
253 
254 } // namespace kit
255 } // namespace nx
Definition: ini_config.h:78
ParamType
Definition: ini_config.h:128
Definition: apple_utils.h:6