Project
Loading...
Searching...
No Matches
qconfig_internal.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// For internal use by qconfig.cxx
16// External usage is possible, but not fully supported
17
18#include "qconfig_general.h"
19#include "qconfig_helpers.h"
20
21#if !defined(QCONFIG_HEADER_GUARD_NO_INCLUDE) || defined(QCONFIG_GENRTC)
22
23#ifdef QCONFIG_PARSE
24
25#define QCONFIG_COMPARE(name, optname, optnameshort) \
26 (thisoption[0] == '-' && ((thisoption[1] == '-' && optname[0] != 0 && strcmp(&thisoption[2], optname) == 0) || \
27 (preoptshort == 0 && thisoption[1] == optnameshort && thisoption[2] == 0) || (thisoption[1] == '-' && strlen(preopt) == 0 && strcmp(&thisoption[2], name) == 0) || \
28 (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)))
29
30#define AddOption(name, type, default, optname, optnameshort, ...) \
31 else if (QCONFIG_COMPARE(#name, optname, optnameshort)) \
32 { \
33 int32_t retVal = qConfigType<type>::qAddOption(tmp.name, i, argv, argc, default, __VA_ARGS__); \
34 if (retVal) { \
35 return (retVal); \
36 } \
37 }
38
39#define AddOptionSet(name, type, value, optname, optnameshort, ...) \
40 else if (QCONFIG_COMPARE(optname, "", optnameshort)) \
41 { \
42 int32_t retVal = qConfigType<type>::qAddOption(tmp.name, i, nullptr, 0, value, __VA_ARGS__, set(value)); \
43 if (retVal) { \
44 return (retVal); \
45 } \
46 }
47
48#define AddOptionVec(name, type, optname, optnameshort, ...) \
49 else if (QCONFIG_COMPARE(#name, optname, optnameshort)) \
50 { \
51 int32_t retVal = qConfigType<type>::qAddOptionVec(tmp.name, i, argv, argc, __VA_ARGS__); \
52 if (retVal) { \
53 return (retVal); \
54 } \
55 }
56
57#define AddOptionArray(name, type, count, default, optname, optnameshort, ...) \
58 else if (QCONFIG_COMPARE(#name, optname, optnameshort)) \
59 { \
60 int32_t retVal = qConfigType<type>::qAddOptionArray(tmp.name, count, i, argv, argc, __VA_ARGS__); \
61 if (retVal) { \
62 return (retVal); \
63 } \
64 }
65
66#define AddSubConfig(name, instance)
67
68#define BeginConfig(name, instance) \
69 { \
70 constexpr const char* preopt = ""; \
71 constexpr const char preoptshort = 0; \
72 name& tmp = instance; \
73 bool tmpfound = true; \
74 if (found) { \
75 }
76
77#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
78 { \
79 [[maybe_unused]] constexpr const char* preopt = preoptname; \
80 [[maybe_unused]] constexpr const char preoptshort = preoptnameshort; \
81 [[maybe_unused]] name& tmp = parent.instance; \
82 bool tmpfound = true; \
83 if (found) { \
84 }
85
86#define BeginHiddenConfig(name, instance) \
87 { \
88 bool tmpfound; \
89 if (0) { \
90 }
91
92// clang-format off
93#define EndConfig() \
94 else { tmpfound = false; } \
95 if (tmpfound) { \
96 found = true; \
97 } \
98 }
99// clang-format on
100
101#define AddHelp(cmd, cmdshort) \
102 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
103 { \
104 const char* arg = getArg(i, argv, argc); \
105 qConfigHelp(arg ? arg : preopt); \
106 return (qcrHelp); \
107 }
108
109#define AddHelpAll(cmd, cmdshort) \
110 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
111 { \
112 const char* arg = getArg(i, argv, argc); \
113 qConfigHelp(arg ? arg : "", true); \
114 return (qcrHelp); \
115 }
116
117#define AddCommand(cmd, cmdshort, command, help) \
118 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
119 { \
120 if (command) { \
121 return (qcrCmd); \
122 } \
123 }
124
125#define AddShortcut(cmd, cmdshort, forward, help, ...) \
126 else if (QCONFIG_COMPARE(cmd, "", cmdshort)) \
127 { \
128 const char* options[] = {"", __VA_ARGS__, nullptr}; \
129 const int32_t nOptions = sizeof(options) / sizeof(options[0]) - 1; \
130 qConfigParse(nOptions, options, nullptr); \
131 thisoption = forward; \
132 goto repeat; \
133 }
134
135// End QCONFIG_PARSE
136#elif defined(QCONFIG_HELP)
137#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__);
138#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__);
139#define AddOptionVec(name, type, optname, optnameshort, ...) qConfigType<type>::qConfigHelpOption(qon_mxstr(name), qon_mxstr(type), nullptr, optname, optnameshort, preopt, preoptshort, 2, __VA_ARGS__);
140#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__);
141#define AddSubConfig(name, instance) \
142 printf("\t%s\n\n", qon_mxcat(qConfig_subconfig_, name)); \
143 if (followSub) { \
144 qConfigHelp(qon_mxstr(name), 2); \
145 }
146#define BeginConfig(name, instance) \
147 if (subConfig == nullptr || *subConfig == 0) { \
148 constexpr const char* preopt = ""; \
149 constexpr const char preoptshort = 0; \
150 printf("\n");
151#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
152 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); \
153 (void)qon_mxcat(qConfig_subconfig_, name); \
154 if (subConfig == nullptr || strcmp(subConfig, followSub == 2 ? qon_mxstr(name) : preoptname) == 0) { \
155 [[maybe_unused]] constexpr const char* preopt = preoptname; \
156 [[maybe_unused]] constexpr const char preoptshort = preoptnameshort; \
157 char argBuffer[2] = {preoptnameshort, 0}; \
158 printf("\n %s: (--%s%s%s)\n", descr, preoptname, preoptnameshort == 0 ? "" : " or -", argBuffer);
159#define BeginHiddenConfig(name, instance) {
160#define EndConfig() }
161#define AddHelp(cmd, cmdshort) qConfigType<void*>::qConfigHelpOption("help", "help", nullptr, cmd, cmdshort, preopt, preoptshort, 3, "Show usage information");
162#define AddHelpAll(cmd, cmdshort) qConfigType<void*>::qConfigHelpOption("help all", "help all", nullptr, cmd, cmdshort, preopt, preoptshort, 3, "Show usage info including all subparameters");
163#define AddCommand(cmd, cmdshort, command, help) qConfigType<void*>::qConfigHelpOption("command", "command", nullptr, cmd, cmdshort, preopt, preoptshort, 4, help);
164#define AddShortcut(cmd, cmdshort, forward, help, ...) qConfigType<void*>::qConfigHelpOption("shortcut", "shortcut", nullptr, cmd, cmdshort, preopt, preoptshort, 4, help);
165#define AddHelpText(text) printf("\n " text ":\n");
166
167// End QCONFIG_HELP
168#elif defined(QCONFIG_PRINT)
169template <class T>
170void qConfigPrint(const T& qconfig_tmp_object, const std::string& blockName); // (Re)declare in the namespace of the include for the specializations below, default argument only in qconfig_general.h
171#define AddOption(name, type, default, optname, optnameshort, ...) std::cout << "\t" << blockName << qon_mxstr(name) << ": " << ::qConfig::print_type(qconfig_tmp_object.name) << "\n";
172#define AddVariable(name, type, default) std::cout << "\t" << blockName << qon_mxstr(name) << ": " << ::qConfig::print_type(qconfig_tmp_object.name) << "\n";
173#define AddOptionSet(name, type, value, optname, optnameshort, ...)
174#define AddOptionVec(name, type, optname, optnameshort, ...) \
175 { \
176 std::cout << "\t" << blockName << qon_mxstr(name) << "[]: "; \
177 for (uint32_t i = 0; i < qconfig_tmp_object.name.size(); i++) { \
178 if (i) { \
179 std::cout << ", "; \
180 } \
181 std::cout << ::qConfig::print_type(qconfig_tmp_object.name[i]); \
182 } \
183 std::cout << "\n"; \
184 }
185#define AddOptionArray(name, type, count, default, optname, optnameshort, ...) \
186 { \
187 std::cout << "\t" << blockName << qon_mxstr(name) << "[" << count << "]: " << ::qConfig::print_type(qconfig_tmp_object.name[0]); \
188 for (int32_t i = 1; i < count; i++) { \
189 std::cout << ", " << ::qConfig::print_type(qconfig_tmp_object.name[i]); \
190 } \
191 std::cout << "\n"; \
192 }
193#define AddSubConfig(name, instance) qConfigPrint(qconfig_tmp_object.instance, blockName + qon_mxstr(instance.));
194#define AddHelpText(text) printf(" " text ":\n");
195#define BeginConfig(name, instance) \
196 void qConfigPrint(const name& qconfig_tmp_object); \
197 bool qon_mxcat(qprint_global_, instance) = []() { qprint_global.emplace_back([]() { qConfigPrint(instance); }); return true; }(); \
198 void qConfigPrint(const name& qconfig_tmp_object) \
199 { \
200 std::string blockName = "";
201#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) \
202 template <> \
203 void qConfigPrint<name>(const name& qconfig_tmp_object, const std::string& blockName) \
204 { \
205 std::cout << "\n\t" << qon_mxstr(name) << ":\n";
206#define BeginHiddenConfig(name, instance) BeginSubConfig(name, instance, x, x, x, x)
207
208#define EndConfig() }
209
210// End QCONFIG_PRINT
211#elif defined(QCONFIG_INSTANCE)
212#define BeginNamespace(name) \
213 namespace name \
214 {
215#define EndNamespace() }
216#define AddOption(name, type, default, optname, optnameshort, help, ...)
217#define AddVariable(name, type, default)
218#define AddOptionSet(name, type, value, optname, optnameshort, help, ...)
219#define AddOptionVec(name, type, optname, optnameshort, help, ...)
220#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...)
221#define AddSubConfig(name, instance)
222#define BeginConfig(name, instance) name instance;
223#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...)
224#define EndConfig()
225
226// End QCONFIG_INSTANCE
227#elif defined(QCONFIG_PRINT_RTC)
228#define AddOption(name, type, default, optname, optnameshort, help, ...) out << qon_mxstr(type) << " " << qon_mxstr(name) << ";\n";
229#define AddVariable(name, type, default) out << qon_mxstr(type) << " " << qon_mxstr(name) << ";\n";
230#define AddOptionArray(name, type, count, default, optname, optnameshort, help, ...) out << qon_mxstr(type) << " " << qon_mxstr(name) << "[" << qon_mxstr(count) << "];\n";
231#define AddOptionVec(name, type, optname, optnameshort, help, ...) out << "std::vector<" << qon_mxstr(type) << "> " << qon_mxstr(name) << ";\n";
232#define AddVariableRTC(name, type, default) \
233 if (useConstexpr) { \
234 out << "static constexpr " << qon_mxstr(type) << " " << qon_mxstr(name) << " = " << ::qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name, true) << ";\n"; \
235 out << qon_mxstr(type) << " " << qon_mxstr(qon_mxcat(_dummy_, name)) << ";\n"; \
236 } else { \
237 AddOption(name, type, default, optname, optnameshort, help); \
238 }
239#define AddOptionRTC(name, type, default, optname, optnameshort, help, ...) AddVariableRTC(name, type, default)
240#define AddOptionArrayRTC(name, type, count, default, optname, optnameshort, help, ...) \
241 if (useConstexpr) { \
242 out << "static constexpr " << qon_mxstr(type) << " " << qon_mxstr(name) << "[" << count << "] = {" << ::qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name[0]); \
243 for (int32_t i = 1; i < count; i++) { \
244 out << ", " << qConfig::print_type(std::get<const qConfigCurrentType*>(tSrc)->name[i]); \
245 } \
246 out << "};\n"; \
247 out << qon_mxstr(type) << " " << qon_mxstr(qon_mxcat(_dummy_, name)) << ";\n"; \
248 } else { \
249 AddOptionArray(name, type, count, default, optname, optnameshort, help); \
250 }
251#define BeginConfig(name, instance) \
252 { \
253 using qConfigCurrentType = name; \
254 out << "struct " << qon_mxstr(name) << " {\n";
255#define BeginSubConfig(name, instance, parent, preoptname, preoptnameshort, descr, ...) BeginConfig(name, instance)
256#define EndConfig() \
257 out << "};"; \
258 }
259#define BeginNamespace(name) out << "namespace " << qon_mxstr(name) << " {\n";
260#define EndNamespace() out << "}\n";
261#define AddSubConfig(name, instance) out << qon_mxstr(name) << " " << qon_mxstr(instance) << ";";
262#define AddOptionSet(...)
263
264// End QCONFIG_PRINT_RTC
265#else // Define structures
266
267#error Internal error, qconfig_internal called with action type
268
269#endif
270
271#include "qconfig_wrapinclude.h"
272
273#endif // QCONFIG_HEADER_GUARD_NO_INCLUDE
void qConfigPrint()
Definition qconfig.cxx:488