Project
Loading...
Searching...
No Matches
qconfig.h
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
14
15#ifndef QCONFIG_H_GENERAL
16#define QCONFIG_H_GENERAL
17#ifndef GPUCA_GPUCODE_DEVICE
18#include <cstdint>
19#endif
20
21extern int32_t qConfigParse(int argc, const char** argv, const char* filename = nullptr);
22extern void qConfigPrint();
23namespace qConfig
24{
34}
35#endif
36
37#if !defined(QCONFIG_HEADER_GUARD_NO_INCLUDE) || defined(QCONFIG_GENRTC)
38
39#define AddArrayDefaults(...) \
40 { \
41 __VA_ARGS__ \
42 }
43
44#ifdef QCONFIG_PARSE
45
46#define QCONFIG_COMPARE(name, optname, optnameshort) \
47 (thisoption[0] == '-' && ((thisoption[1] == '-' && optname[0] != 0 && strcmp(&thisoption[2], optname) == 0) || \
48 (preoptshort == 0 && thisoption[1] == optnameshort && thisoption[2] == 0) || (thisoption[1] == '-' && strlen(preopt) == 0 && strcmp(&thisoption[2], name) == 0) || \
49 (preoptshort != 0 && thisoption[1] == preoptshort && thisoption[2] == optnameshort && thisoption[3] == 0) || (thisoption[1] == '-' && strlen(preopt) && strncmp(&thisoption[2], preopt, strlen(preopt)) == 0 && strcmp(&thisoption[2 + strlen(preopt)], name) == 0)))
50
51#define AddOption(name, type, default, optname, optnameshort, ...) \
52 else if (QCONFIG_COMPARE(#name, optname, optnameshort)) \
53 { \
54 int32_t retVal = qConfigType<type>::qAddOption(tmp.name, i, argv, argc, default, __VA_ARGS__); \
55 if (retVal) { \
56 return (retVal); \
57 } \
58 }
59
60#define AddOptionSet(name, type, value, optname, optnameshort, ...) \
61 else if (QCONFIG_COMPARE(optname, "", optnameshort)) \
62 { \
63 int32_t retVal = qConfigType<type>::qAddOption(tmp.name, i, nullptr, 0, value, __VA_ARGS__, set(value)); \
64 if (retVal) { \
65 return (retVal); \
66 } \
67 }
68
69#define AddOptionVec(name, type, optname, optnameshort, ...) \
70 else if (QCONFIG_COMPARE(#name, optname, optnameshort)) \
71 { \
72 int32_t retVal = qConfigType<type>::qAddOptionVec(tmp.name, i, argv, argc, __VA_ARGS__); \
73 if (retVal) { \
74 return (retVal); \
75 } \
76 }
77
78#define AddOptionArray(name, type, count, default, optname, optnameshort, ...) \
79 else if (QCONFIG_COMPARE(#name, optname, optnameshort)) \
80 { \
81 int32_t retVal = qConfigType<type>::qAddOptionArray(tmp.name, count, i, argv, argc, __VA_ARGS__); \
82 if (retVal) { \
83 return (retVal); \
84 } \
85 }
86
87#define AddSubConfig(name, instance)
88
89#define BeginConfig(name, instance) \
90 { \
91 constexpr const char* preopt = ""; \
92 constexpr const char preoptshort = 0; \
93 name& tmp = instance; \
94 bool tmpfound = true; \
95 if (found) { \
96 }
97
98#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
99 { \
100 constexpr const char* preopt = preoptname; \
101 (void)preopt; \
102 constexpr const char preoptshort = preoptnameshort; \
103 (void)preoptshort; \
104 name& tmp = parent.instance; \
105 (void)tmp; \
106 bool tmpfound = true; \
107 if (found) { \
108 }
109
110#define BeginHiddenConfig(name, instance) \
111 { \
112 bool tmpfound; \
113 if (0) { \
114 }
115
116#define EndConfig() \
117 else { tmpfound = false; } \
118 if (tmpfound) { \
119 found = true; \
120 } \
121 }
122
123#define AddHelp(cmd, cmdshort) \
124 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
125 { \
126 const char* arg = getArg(i, argv, argc); \
127 qConfigHelp(arg ? arg : preopt); \
128 return (qcrHelp); \
129 }
130
131#define AddHelpAll(cmd, cmdshort) \
132 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
133 { \
134 const char* arg = getArg(i, argv, argc); \
135 qConfigHelp(arg ? arg : "", true); \
136 return (qcrHelp); \
137 }
138
139#define AddCommand(cmd, cmdshort, command, help) \
140 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
141 { \
142 if (command) { \
143 return (qcrCmd); \
144 } \
145 }
146
147#define AddShortcut(cmd, cmdshort, forward, help, ...) \
148 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
149 { \
150 const char* options[] = {"", __VA_ARGS__, nullptr}; \
151 const int32_t nOptions = sizeof(options) / sizeof(options[0]) - 1; \
152 qConfigParse(nOptions, options, nullptr); \
153 thisoption = forward; \
154 goto repeat; \
155 }
156
157// End QCONFIG_PARSE
158#elif defined(QCONFIG_HELP)
159#define AddOption(name, type, default, optname, optnameshort, ...) qConfigType<type>::qConfigHelpOption(qon_mxstr(name), qon_mxstr(type), qon_mxstr(default), optname, optnameshort, preopt, preoptshort, 0, __VA_ARGS__);
160#define AddOptionSet(name, type, value, optname, optnameshort, ...) qConfigType<type>::qConfigHelpOption(qon_mxstr(name), qon_mxstr(type), qon_mxstr(value), optname, optnameshort, preopt, preoptshort, 1, __VA_ARGS__);
161#define AddOptionVec(name, type, optname, optnameshort, ...) qConfigType<type>::qConfigHelpOption(qon_mxstr(name), qon_mxstr(type), nullptr, optname, optnameshort, preopt, preoptshort, 2, __VA_ARGS__);
162#define AddOptionArray(name, type, count, default, optname, optnameshort, ...) qConfigType<type>::qConfigHelpOption(qon_mxstr(name), qon_mxstr(type) "[" qon_mxstr(count) "]", nullptr, optname, optnameshort, preopt, preoptshort, 2, __VA_ARGS__);
163#define AddSubConfig(name, instance) \
164 printf("\t%s\n\n", qon_mxcat(qConfig_subconfig_, name)); \
165 if (followSub) { \
166 qConfigHelp(qon_mxstr(name), 2); \
167 }
168#define BeginConfig(name, instance) \
169 if (subConfig == nullptr || *subConfig == 0) { \
170 constexpr const char* preopt = ""; \
171 constexpr const char preoptshort = 0; \
172 printf("\n");
173#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
174 const char* qon_mxcat(qConfig_subconfig_, name) = preoptnameshort == 0 ? (qon_mxstr(name) ": --" preoptname "\n\t\t" descr) : (qon_mxstr(name) ": -" qon_mxstr('a') " (--" preoptname ")\n\t\t" descr); \
175 (void)qon_mxcat(qConfig_subconfig_, name); \
176 if (subConfig == nullptr || strcmp(subConfig, followSub == 2 ? qon_mxstr(name) : preoptname) == 0) { \
177 constexpr const char* preopt = preoptname; \
178 (void)preopt; \
179 constexpr const char preoptshort = preoptnameshort; \
180 (void)preoptshort; \
181 char argBuffer[2] = {preoptnameshort, 0}; \
182 printf("\n %s: (--%s%s%s)\n", descr, preoptname, preoptnameshort == 0 ? "" : " or -", argBuffer);
183#define BeginHiddenConfig(name, instance) {
184#define EndConfig() }
185#define AddHelp(cmd, cmdshort) qConfigType<void*>::qConfigHelpOption("help", "help", nullptr, cmd, cmdshort, preopt, preoptshort, 3, "Show usage information");
186#define AddHelpAll(cmd, cmdshort) qConfigType<void*>::qConfigHelpOption("help all", "help all", nullptr, cmd, cmdshort, preopt, preoptshort, 3, "Show usage info including all subparameters");
187#define AddCommand(cmd, cmdshort, command, help) qConfigType<void*>::qConfigHelpOption("command", "command", nullptr, cmd, cmdshort, preopt, preoptshort, 4, help);
188#define AddShortcut(cmd, cmdshort, forward, help, ...) qConfigType<void*>::qConfigHelpOption("shortcut", "shortcut", nullptr, cmd, cmdshort, preopt, preoptshort, 4, help);
189#define AddHelpText(text) printf("\n " text ":\n");
190
191// End QCONFIG_HELP
192#elif defined(QCONFIG_PRINT)
193#define AddOption(name, type, default, optname, optnameshort, ...) std::cout << "\t" << blockName << qon_mxstr(name) << ": " << qConfig::print_type(qconfig_tmp_object.name) << "\n";
194#define AddVariable(name, type, default) std::cout << "\t" << blockName << qon_mxstr(name) << ": " << qConfig::print_type(qconfig_tmp_object.name) << "\n";
195#define AddOptionSet(name, type, value, optname, optnameshort, ...)
196#define AddOptionVec(name, type, optname, optnameshort, ...) \
197 { \
198 std::cout << "\t" << blockName << qon_mxstr(name) << "[]: "; \
199 for (uint32_t i = 0; i < qconfig_tmp_object.name.size(); i++) { \
200 if (i) { \
201 std::cout << ", "; \
202 } \
203 std::cout << qConfig::print_type(qconfig_tmp_object.name[i]); \
204 } \
205 std::cout << "\n"; \
206 }
207#define AddOptionArray(name, type, count, default, optname, optnameshort, ...) \
208 { \
209 std::cout << "\t" << blockName << qon_mxstr(name) << "[" << count << "]: " << qConfig::print_type(qconfig_tmp_object.name[0]); \
210 for (int32_t i = 1; i < count; i++) { \
211 std::cout << ", " << qConfig::print_type(qconfig_tmp_object.name[i]); \
212 } \
213 std::cout << "\n"; \
214 }
215#define AddSubConfig(name, instance) qConfigPrint(qconfig_tmp_object.instance, blockName + qon_mxstr(instance.));
216#define AddHelpText(text) printf(" " text ":\n");
217#define BeginConfig(name, instance) \
218 void qConfigPrint(const name& qconfig_tmp_object); \
219 bool qon_mxcat(qprint_global_, instance) = []() { qprint_global.emplace_back([]() { qConfigPrint(instance); }); return true; }(); \
220 void qConfigPrint(const name& qconfig_tmp_object) \
221 { \
222 std::string blockName = "";
223#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
224 void qConfigPrint(const name& qconfig_tmp_object, std::string blockName = "") \
225 { \
226 std::cout << "\n\t" << qon_mxstr(name) << ":\n";
227#define BeginHiddenConfig(name, instance) BeginSubConfig(name, instance, x, x, x, x)
228
229#define EndConfig() }
230
231// End QCONFIG_PRINT
232#elif defined(QCONFIG_INSTANCE)
233#define BeginNamespace(name) \
234 namespace name \
235 {
236#define EndNamespace() }
237#define AddOption(name, type, default, optname, optnameshort, help, ...)
238#define AddVariable(name, type, default)
239#define AddOptionSet(name, type, value, optname, optnameshort, help, ...)
240#define AddOptionVec(name, type, optname, optnameshort, help, ...)
241#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...)
242#define AddSubConfig(name, instance)
243#define BeginConfig(name, instance) name instance;
244#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...)
245#define EndConfig()
246
247// End QCONFIG_INSTANCE
248#elif defined(QCONFIG_PRINT_RTC)
249#define AddOption(name, type, default, optname, optnameshort, help, ...) out << qon_mxstr(type) << " " << qon_mxstr(name) << ";\n";
250#define AddVariable(name, type, default) out << qon_mxstr(type) << " " << qon_mxstr(name) << ";\n";
251#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) out << qon_mxstr(type) << " " << qon_mxstr(name) << "[" << qon_mxstr(count) << "];\n";
252#define AddOptionVec(name, type, optname, optnameshort, help, ...) out << "std::vector<" << qon_mxstr(type) << "> " << qon_mxstr(name) << ";\n";
253#define AddVariableRTC(name, type, default) \
254 if (useConstexpr) { \
255 out << "static constexpr " << qon_mxstr(type) << " " << qon_mxstr(name) << " = " << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name) << ";\n"; \
256 out << qon_mxstr(type) << " " << qon_mxstr(qon_mxcat(_dummy_, name)) << ";\n"; \
257 } else { \
258 AddOption(name, type, default, optname, optnameshort, help); \
259 }
260#define AddOptionRTC(name, type, default, optname, optnameshort, help, ...) AddVariableRTC(name, type, default)
261#define AddOptionArrayRTC(name, type, count, default, optname, optnameshort, help, ...) \
262 if (useConstexpr) { \
263 out << "static constexpr " << qon_mxstr(type) << " " << qon_mxstr(name) << "[" << count << "] = {" << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name[0]); \
264 for (int32_t i = 1; i < count; i++) { \
265 out << ", " << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name[i]); \
266 } \
267 out << "};\n"; \
268 out << qon_mxstr(type) << " " << qon_mxstr(qon_mxcat(_dummy_, name)) << ";\n"; \
269 } else { \
270 AddOptionArray(name, type, count, default, optname, optnameshort, help); \
271 }
272#define BeginConfig(name, instance) \
273 { \
274 using qConfigCurrentType = name; \
275 out << "struct " << qon_mxstr(name) << " {\n";
276#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) BeginConfig(name, instance)
277#define EndConfig() \
278 out << "};"; \
279 }
280#define BeginNamespace(name) out << "namespace " << qon_mxstr(name) << " {\n";
281#define EndNamespace() out << "}\n";
282#define AddSubConfig(name, instance) out << qon_mxstr(name) << " " << qon_mxstr(instance) << ";";
283#define AddOptionSet(...)
284
285// End QCONFIG_PRINT_RTC
286#else // Define structures
287#if defined(QCONFIG_HEADER_GUARD) && !defined(QCONFIG_GENRTC)
288#define QCONFIG_HEADER_GUARD_NO_INCLUDE
289#else
290#define QCONFIG_HEADER_GUARD
291
292#ifndef BeginNamespace
293#define BeginNamespace(name) \
294 namespace name \
295 {
296#define EndNamespace() }
297#endif
298#if !defined(QCONFIG_GPU)
299#define AddOption(name, type, default, optname, optnameshort, help, ...) type name = default;
300#define AddVariable(name, type, default) type name = default;
301#define _AddOptionArray_INTERNAL_EXPAND(...) __VA_ARGS__
302#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) type name[count] = {_AddOptionArray_INTERNAL_EXPAND default};
303#define AddOptionVec(name, type, optname, optnameshort, help, ...) std::vector<type> name;
304#else
305#define AddOption(name, type, default, optname, optnameshort, help, ...) type name;
306#define AddVariable(name, type, default) type name;
307#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) type name[count];
308#define AddOptionVec(name, type, optname, optnameshort, help, ...) void* name[sizeof(std::vector<type>) / sizeof(void*)];
309#endif
310#ifdef QCONFIG_GENRTC
311#define AddVariableRTC(name, type, default) \
312 static constexpr type name = default; \
313 type _dummy_##name = default;
314#define AddOptionRTC(name, type, default, optname, optnameshort, help, ...) AddVariableRTC(name, type, default)
315#define _AddOptionArray_INTERNAL_EXPAND(...) __VA_ARGS__
316#define AddOptionArrayRTC(name, type, count, default, optname, optnameshort, help, ...) \
317 static constexpr type name[count] = {_AddOptionArray_INTERNAL_EXPAND default}; \
318 type _dummy_##name[count] = {_AddOptionArray_INTERNAL_EXPAND default};
319#else
320#define AddCustomCPP(...) __VA_ARGS__
321#endif
322#define AddOptionSet(name, type, value, optname, optnameshort, help, ...)
323#define AddSubConfig(name, instance) name instance;
324#if !defined(QCONFIG_GENRTC) && !defined(GPUCA_GPUCODE_DEVICE)
325#define BeginConfig(name, instance) \
326 struct name { \
327 bool operator==(const name&) const = default;
328#else
329#define BeginConfig(name, instance) struct name {
330#endif
331#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) BeginConfig(name, instance)
332#define EndConfig() \
333 } \
334 ;
335
336#endif
337#endif
338
339#ifndef QCONFIG_HEADER_GUARD_NO_INCLUDE
340#ifndef AddHelp
341#define AddHelp(cmd, cmdshort)
342#endif
343#ifndef AddHelpAll
344#define AddHelpAll(cmd, cmdshort)
345#endif
346#ifndef AddCommand
347#define AddCommand(cmd, cmdshort, command)
348#endif
349#ifndef AddShortcut
350#define AddShortcut(cmd, cmdshort, forward, help, ...)
351#endif
352#ifndef AddVariable
353#define AddVariable(name, type, default)
354#endif
355#ifndef AddHelpText
356#define AddHelpText(text)
357#endif
358#ifndef BeginNamespace
359#define BeginNamespace(name)
360#endif
361#ifndef EndNamespace
362#define EndNamespace()
363#endif
364#ifndef AddCustomCPP
365#define AddCustomCPP(...)
366#endif
367#ifndef AddOptionRTC
368#define AddOptionRTC(...) AddOption(__VA_ARGS__)
369#endif
370#ifndef AddVariableRTC
371#define AddVariableRTC(...) AddVariable(__VA_ARGS__)
372#endif
373#ifndef AddOptionArrayRTC
374#define AddOptionArrayRTC(...) AddOptionArray(__VA_ARGS__)
375#endif
376#ifndef BeginHiddenConfig
377#define BeginHiddenConfig(name, instance) BeginSubConfig(name, instance, , , , )
378#endif
379
380#include "qconfigoptions.h"
381
382#undef AddOption
383#undef AddOptionRTC
384#undef AddVariable
385#undef AddVariableRTC
386#undef AddOptionSet
387#undef AddOptionVec
388#undef AddOptionArray
389#undef AddOptionArrayRTC
390#undef AddArrayDefaults
391#undef AddSubConfig
392#undef BeginConfig
393#undef BeginSubConfig
394#undef BeginHiddenConfig
395#undef EndConfig
396#undef AddHelp
397#undef AddHelpAll
398#undef AddHelpText
399#undef AddCommand
400#undef AddShortcut
401#undef BeginNamespace
402#undef EndNamespace
403#undef AddCustomCPP
404#ifdef QCONFIG_COMPARE
405#undef QCONFIG_COMPARE
406#endif
407
408#else
409#undef QCONFIG_HEADER_GUARD_NO_INCLUDE
410#endif
411
412#endif // QCONFIG_HEADER_GUARD_NO_INCLUDE
qConfigRetVal
Definition qconfig.h:25
@ qcrHelp
Definition qconfig.h:29
@ qcrOK
Definition qconfig.h:25
@ qcrArgMissing
Definition qconfig.h:31
@ qcrMinFailure
Definition qconfig.h:27
@ qcrArrayOverflow
Definition qconfig.h:33
@ qcrMaxFailure
Definition qconfig.h:28
@ qcrArgIncomplete
Definition qconfig.h:32
@ qcrError
Definition qconfig.h:26
@ qcrCmd
Definition qconfig.h:30
std::string filename()
void qConfigPrint()
Definition qconfig.cxx:517
int32_t qConfigParse(int argc, const char **argv, const char *filename=nullptr)
Definition qconfig.cxx:515