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