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