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 [[maybe_unused]] constexpr const char* preopt = preoptname; \
101 [[maybe_unused]] constexpr const char preoptshort = preoptnameshort; \
102 [[maybe_unused]] name& tmp = parent.instance; \
103 bool tmpfound = true; \
104 if (found) { \
105 }
106
107#define BeginHiddenConfig(name, instance) \
108 { \
109 bool tmpfound; \
110 if (0) { \
111 }
112
113#define EndConfig() \
114 else { tmpfound = false; } \
115 if (tmpfound) { \
116 found = true; \
117 } \
118 }
119
120#define AddHelp(cmd, cmdshort) \
121 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
122 { \
123 const char* arg = getArg(i, argv, argc); \
124 qConfigHelp(arg ? arg : preopt); \
125 return (qcrHelp); \
126 }
127
128#define AddHelpAll(cmd, cmdshort) \
129 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
130 { \
131 const char* arg = getArg(i, argv, argc); \
132 qConfigHelp(arg ? arg : "", true); \
133 return (qcrHelp); \
134 }
135
136#define AddCommand(cmd, cmdshort, command, help) \
137 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
138 { \
139 if (command) { \
140 return (qcrCmd); \
141 } \
142 }
143
144#define AddShortcut(cmd, cmdshort, forward, help, ...) \
145 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
146 { \
147 const char* options[] = {"", __VA_ARGS__, nullptr}; \
148 const int32_t nOptions = sizeof(options) / sizeof(options[0]) - 1; \
149 qConfigParse(nOptions, options, nullptr); \
150 thisoption = forward; \
151 goto repeat; \
152 }
153
154// End QCONFIG_PARSE
155#elif defined(QCONFIG_HELP)
156#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__);
157#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__);
158#define AddOptionVec(name, type, optname, optnameshort, ...) qConfigType<type>::qConfigHelpOption(qon_mxstr(name), qon_mxstr(type), nullptr, optname, optnameshort, preopt, preoptshort, 2, __VA_ARGS__);
159#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__);
160#define AddSubConfig(name, instance) \
161 printf("\t%s\n\n", qon_mxcat(qConfig_subconfig_, name)); \
162 if (followSub) { \
163 qConfigHelp(qon_mxstr(name), 2); \
164 }
165#define BeginConfig(name, instance) \
166 if (subConfig == nullptr || *subConfig == 0) { \
167 constexpr const char* preopt = ""; \
168 constexpr const char preoptshort = 0; \
169 printf("\n");
170#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
171 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); \
172 (void)qon_mxcat(qConfig_subconfig_, name); \
173 if (subConfig == nullptr || strcmp(subConfig, followSub == 2 ? qon_mxstr(name) : preoptname) == 0) { \
174 [[maybe_unused]] constexpr const char* preopt = preoptname; \
175 [[maybe_unused]] constexpr const char preoptshort = preoptnameshort; \
176 char argBuffer[2] = {preoptnameshort, 0}; \
177 printf("\n %s: (--%s%s%s)\n", descr, preoptname, preoptnameshort == 0 ? "" : " or -", argBuffer);
178#define BeginHiddenConfig(name, instance) {
179#define EndConfig() }
180#define AddHelp(cmd, cmdshort) qConfigType<void*>::qConfigHelpOption("help", "help", nullptr, cmd, cmdshort, preopt, preoptshort, 3, "Show usage information");
181#define AddHelpAll(cmd, cmdshort) qConfigType<void*>::qConfigHelpOption("help all", "help all", nullptr, cmd, cmdshort, preopt, preoptshort, 3, "Show usage info including all subparameters");
182#define AddCommand(cmd, cmdshort, command, help) qConfigType<void*>::qConfigHelpOption("command", "command", nullptr, cmd, cmdshort, preopt, preoptshort, 4, help);
183#define AddShortcut(cmd, cmdshort, forward, help, ...) qConfigType<void*>::qConfigHelpOption("shortcut", "shortcut", nullptr, cmd, cmdshort, preopt, preoptshort, 4, help);
184#define AddHelpText(text) printf("\n " text ":\n");
185
186// End QCONFIG_HELP
187#elif defined(QCONFIG_PRINT)
188#define AddOption(name, type, default, optname, optnameshort, ...) std::cout << "\t" << blockName << qon_mxstr(name) << ": " << qConfig::print_type(qconfig_tmp_object.name) << "\n";
189#define AddVariable(name, type, default) std::cout << "\t" << blockName << qon_mxstr(name) << ": " << qConfig::print_type(qconfig_tmp_object.name) << "\n";
190#define AddOptionSet(name, type, value, optname, optnameshort, ...)
191#define AddOptionVec(name, type, optname, optnameshort, ...) \
192 { \
193 std::cout << "\t" << blockName << qon_mxstr(name) << "[]: "; \
194 for (uint32_t i = 0; i < qconfig_tmp_object.name.size(); i++) { \
195 if (i) { \
196 std::cout << ", "; \
197 } \
198 std::cout << qConfig::print_type(qconfig_tmp_object.name[i]); \
199 } \
200 std::cout << "\n"; \
201 }
202#define AddOptionArray(name, type, count, default, optname, optnameshort, ...) \
203 { \
204 std::cout << "\t" << blockName << qon_mxstr(name) << "[" << count << "]: " << qConfig::print_type(qconfig_tmp_object.name[0]); \
205 for (int32_t i = 1; i < count; i++) { \
206 std::cout << ", " << qConfig::print_type(qconfig_tmp_object.name[i]); \
207 } \
208 std::cout << "\n"; \
209 }
210#define AddSubConfig(name, instance) qConfigPrint(qconfig_tmp_object.instance, blockName + qon_mxstr(instance.));
211#define AddHelpText(text) printf(" " text ":\n");
212#define BeginConfig(name, instance) \
213 void qConfigPrint(const name& qconfig_tmp_object); \
214 bool qon_mxcat(qprint_global_, instance) = []() { qprint_global.emplace_back([]() { qConfigPrint(instance); }); return true; }(); \
215 void qConfigPrint(const name& qconfig_tmp_object) \
216 { \
217 std::string blockName = "";
218#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
219 void qConfigPrint(const name& qconfig_tmp_object, std::string blockName = "") \
220 { \
221 std::cout << "\n\t" << qon_mxstr(name) << ":\n";
222#define BeginHiddenConfig(name, instance) BeginSubConfig(name, instance, x, x, x, x)
223
224#define EndConfig() }
225
226// End QCONFIG_PRINT
227#elif defined(QCONFIG_INSTANCE)
228#define BeginNamespace(name) \
229 namespace name \
230 {
231#define EndNamespace() }
232#define AddOption(name, type, default, optname, optnameshort, help, ...)
233#define AddVariable(name, type, default)
234#define AddOptionSet(name, type, value, optname, optnameshort, help, ...)
235#define AddOptionVec(name, type, optname, optnameshort, help, ...)
236#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...)
237#define AddSubConfig(name, instance)
238#define BeginConfig(name, instance) name instance;
239#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...)
240#define EndConfig()
241
242// End QCONFIG_INSTANCE
243#elif defined(QCONFIG_PRINT_RTC)
244#define AddOption(name, type, default, optname, optnameshort, help, ...) out << qon_mxstr(type) << " " << qon_mxstr(name) << ";\n";
245#define AddVariable(name, type, default) out << qon_mxstr(type) << " " << qon_mxstr(name) << ";\n";
246#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) out << qon_mxstr(type) << " " << qon_mxstr(name) << "[" << qon_mxstr(count) << "];\n";
247#define AddOptionVec(name, type, optname, optnameshort, help, ...) out << "std::vector<" << qon_mxstr(type) << "> " << qon_mxstr(name) << ";\n";
248#define AddVariableRTC(name, type, default) \
249 if (useConstexpr) { \
250 out << "static constexpr " << qon_mxstr(type) << " " << qon_mxstr(name) << " = " << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name, true) << ";\n"; \
251 out << qon_mxstr(type) << " " << qon_mxstr(qon_mxcat(_dummy_, name)) << ";\n"; \
252 } else { \
253 AddOption(name, type, default, optname, optnameshort, help); \
254 }
255#define AddOptionRTC(name, type, default, optname, optnameshort, help, ...) AddVariableRTC(name, type, default)
256#define AddOptionArrayRTC(name, type, count, default, optname, optnameshort, help, ...) \
257 if (useConstexpr) { \
258 out << "static constexpr " << qon_mxstr(type) << " " << qon_mxstr(name) << "[" << count << "] = {" << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name[0]); \
259 for (int32_t i = 1; i < count; i++) { \
260 out << ", " << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name[i]); \
261 } \
262 out << "};\n"; \
263 out << qon_mxstr(type) << " " << qon_mxstr(qon_mxcat(_dummy_, name)) << ";\n"; \
264 } else { \
265 AddOptionArray(name, type, count, default, optname, optnameshort, help); \
266 }
267#define BeginConfig(name, instance) \
268 { \
269 using qConfigCurrentType = name; \
270 out << "struct " << qon_mxstr(name) << " {\n";
271#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) BeginConfig(name, instance)
272#define EndConfig() \
273 out << "};"; \
274 }
275#define BeginNamespace(name) out << "namespace " << qon_mxstr(name) << " {\n";
276#define EndNamespace() out << "}\n";
277#define AddSubConfig(name, instance) out << qon_mxstr(name) << " " << qon_mxstr(instance) << ";";
278#define AddOptionSet(...)
279
280// End QCONFIG_PRINT_RTC
281#else // Define structures
282#if defined(QCONFIG_HEADER_GUARD) && !defined(QCONFIG_GENRTC)
283#define QCONFIG_HEADER_GUARD_NO_INCLUDE
284#else
285#define QCONFIG_HEADER_GUARD
286
287#ifndef BeginNamespace
288#define BeginNamespace(name) \
289 namespace name \
290 {
291#define EndNamespace() }
292#endif
293#if !defined(QCONFIG_GPU)
294#define AddOption(name, type, default, optname, optnameshort, help, ...) type name = default;
295#define AddVariable(name, type, default) type name = default;
296#define _AddOptionArray_INTERNAL_EXPAND(...) __VA_ARGS__
297#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) type name[count] = {_AddOptionArray_INTERNAL_EXPAND default};
298#define AddOptionVec(name, type, optname, optnameshort, help, ...) std::vector<type> name;
299#else
300#define AddOption(name, type, default, optname, optnameshort, help, ...) type name;
301#define AddVariable(name, type, default) type name;
302#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) type name[count];
303#define AddOptionVec(name, type, optname, optnameshort, help, ...) void* name[sizeof(std::vector<type>) / sizeof(void*)];
304#endif
305#ifdef QCONFIG_GENRTC
306#define AddVariableRTC(name, type, default) \
307 static constexpr type name = default; \
308 type _dummy_##name = default;
309#define AddOptionRTC(name, type, default, optname, optnameshort, help, ...) AddVariableRTC(name, type, default)
310#define _AddOptionArray_INTERNAL_EXPAND(...) __VA_ARGS__
311#define AddOptionArrayRTC(name, type, count, default, optname, optnameshort, help, ...) \
312 static constexpr type name[count] = {_AddOptionArray_INTERNAL_EXPAND default}; \
313 type _dummy_##name[count] = {_AddOptionArray_INTERNAL_EXPAND default};
314#else
315#define AddCustomCPP(...) __VA_ARGS__
316#endif
317#define AddOptionSet(name, type, value, optname, optnameshort, help, ...)
318#define AddSubConfig(name, instance) name instance;
319#if !defined(QCONFIG_GENRTC) && !defined(GPUCA_GPUCODE_DEVICE)
320#define BeginConfig(name, instance) \
321 struct name { \
322 bool operator==(const name&) const = default;
323#else
324#define BeginConfig(name, instance) struct name {
325#endif
326#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) BeginConfig(name, instance)
327#define EndConfig() \
328 } \
329 ;
330
331#endif
332#endif
333
334#ifndef QCONFIG_HEADER_GUARD_NO_INCLUDE
335#ifndef AddHelp
336#define AddHelp(cmd, cmdshort)
337#endif
338#ifndef AddHelpAll
339#define AddHelpAll(cmd, cmdshort)
340#endif
341#ifndef AddCommand
342#define AddCommand(cmd, cmdshort, command)
343#endif
344#ifndef AddShortcut
345#define AddShortcut(cmd, cmdshort, forward, help, ...)
346#endif
347#ifndef AddVariable
348#define AddVariable(name, type, default)
349#endif
350#ifndef AddHelpText
351#define AddHelpText(text)
352#endif
353#ifndef BeginNamespace
354#define BeginNamespace(name)
355#endif
356#ifndef EndNamespace
357#define EndNamespace()
358#endif
359#ifndef AddCustomCPP
360#define AddCustomCPP(...)
361#endif
362#ifndef AddOptionRTC
363#define AddOptionRTC(...) AddOption(__VA_ARGS__)
364#endif
365#ifndef AddVariableRTC
366#define AddVariableRTC(...) AddVariable(__VA_ARGS__)
367#endif
368#ifndef AddOptionArrayRTC
369#define AddOptionArrayRTC(...) AddOptionArray(__VA_ARGS__)
370#endif
371#ifndef BeginHiddenConfig
372#define BeginHiddenConfig(name, instance) BeginSubConfig(name, instance, , , , )
373#endif
374
375#include "qconfigoptions.h"
376
377#undef AddOption
378#undef AddOptionRTC
379#undef AddVariable
380#undef AddVariableRTC
381#undef AddOptionSet
382#undef AddOptionVec
383#undef AddOptionArray
384#undef AddOptionArrayRTC
385#undef AddArrayDefaults
386#undef AddSubConfig
387#undef BeginConfig
388#undef BeginSubConfig
389#undef BeginHiddenConfig
390#undef EndConfig
391#undef AddHelp
392#undef AddHelpAll
393#undef AddHelpText
394#undef AddCommand
395#undef AddShortcut
396#undef BeginNamespace
397#undef EndNamespace
398#undef AddCustomCPP
399#ifdef QCONFIG_COMPARE
400#undef QCONFIG_COMPARE
401#endif
402
403#else
404#undef QCONFIG_HEADER_GUARD_NO_INCLUDE
405#endif
406
407#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:515
int32_t qConfigParse(int argc, const char **argv, const char *filename=nullptr)
Definition qconfig.cxx:513