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