27#define QCONFIG_PARSER_CXX
28#define QCONFIG_INSTANCE
30#undef QCONFIG_INSTANCE
34#define QCONFIG_SETTING(name, type) \
35 struct qon_mxcat3(q, name, _t) \
38 constexpr qon_mxcat3(q, name, _t)(type s) : v(s) {} \
40 constexpr qon_mxcat3(q, name, _t) name(type v) { return (qon_mxcat3(q, name, _t)(v)); }
42#define QCONFIG_SETTING_TEMPLATE(name) \
43 template <typename T> \
44 struct qon_mxcat3(q, name, _t) \
47 constexpr qon_mxcat3(q, name, _t)(const T& s) : v(s) {} \
49 template <typename T> \
50 constexpr qon_mxcat3(q, name, _t)<T> name(T v) \
52 return (qon_mxcat3(q, name, _t) < T > (v)); \
61static inline const char* getOptName(
const char** argv, int32_t
i)
63 while (
i > 1 && argv[
i][0] !=
'-') {
83static int32_t qAddOptionType(
qConfigSettings<T>& settings, T&
ref, int32_t&
i,
const char** argv,
const int argc, T def);
93template <
typename... X>
102template <
typename... X>
113 if (settings.
doSet) {
115 }
else if ((
retVal = qAddOptionType<T>(settings,
ref,
i, argv, argc, def))) {
118 if ((
retVal = qAddOptionMinMax<T>(settings,
ref, iOrg < argc ? argv[iOrg] :
""))) {
121 qAddOptionMessage<T>(settings,
ref);
129 return (qAddOptionType<T>(settings,
ref,
i, argv, argc, def));
131template <typename T, int32_t index = 0, int32_t left = std::tuple_size<T>::value>
135 auto&
ref = std::get<index>(tup);
136 int32_t
retVal = qAddOptionMainTupleElem(settings,
ref,
i, argv, argc);
139 printf(
"Invalid number of arguments for option %s\n", getOptName(argv,
i));
147template <
typename T,
int32_t index>
151template <
typename... X>
162 static_assert(!std::is_same<T, bool>::value,
"min option not supported for boolean settings");
164 settings.
min = minval.v;
168 static_assert(!std::is_same<T, bool>::value,
"max option not supported for boolean settings");
170 settings.
max = maxval.v;
173 template <
typename S>
176 settings.
doSet =
true;
177 settings.
set = set.v;
179 template <
typename S>
183 settings.
set = set.v;
187 template <
typename Arg1,
typename... Args>
194 template <
typename... Args>
195 static inline int32_t
qAddOption(T&
ref, int32_t&
i,
const char** argv,
const int argc, T def,
const char* , Args&&... args)
201 template <
typename... Args>
202 static inline int32_t
qAddOptionVec(std::vector<T>&
ref, int32_t&
i,
const char** argv,
const int argc,
const char* , Args&&... args)
205 int32_t iFirst =
i, iLast;
208 T tmp = T(), def = T();
214 if (
i == iFirst ||
i != iLast) {
217 }
while (
i != iLast);
221 template <
typename... Args>
222 static inline int32_t
qAddOptionArray(T*
ref, int32_t
count, int32_t&
i,
const char** argv,
const int argc,
const char* , Args&&... args)
225 int32_t iFirst =
i, iLast;
228 T tmp = T(), def = T();
234 if (
i - iFirst >=
count) {
235 printf(
"Too many values provided for option %s\n", getOptName(argv,
i));
238 if (
i == iFirst ||
i != iLast) {
239 ref[
i - iFirst] = tmp;
241 }
while (
i != iLast);
245 template <
typename... Args>
246 static inline void qConfigHelpOption(
const char*
name,
const char*
type,
const char* def,
const char* optname,
char optnameshort,
const char* preopt,
char preoptshort, int32_t optionType,
const char* help, Args&&... args)
249 const bool boolType = optionType != 1 && std::is_same<T, bool>::value;
250 const char*
arguments = settings.
doSet ?
" (" : (settings.
doDefault || optionType == 1 || boolType) ?
" [arg] (" : optionType == 2 ?
" [...] (" :
" arg (";
251 char argBuffer[4] = {0};
252 uint32_t argBufferPos = 0;
253 if (optnameshort && preoptshort) {
254 argBuffer[argBufferPos++] =
'-';
257 argBuffer[argBufferPos++] = preoptshort == 0 ?
'-' : preoptshort;
258 argBuffer[argBufferPos++] = optnameshort;
260 std::cout <<
"\t" <<
name <<
": " << argBuffer << (optnameshort == 0 ?
"" :
arguments) <<
"--";
261 if (optname[0] == 0) {
262 std::cout << preopt <<
name;
264 std::cout << optname;
266 std::cout << (optnameshort == 0 ?
arguments :
", ") <<
"type: " <<
type;
267 if (optionType == 0) {
268 std::cout <<
", default: " << def;
270 if (optionType == 1) {
271 std::cout <<
", sets " <<
name <<
" to " << def;
274 std::cout <<
", minimum: " << settings.
min;
277 std::cout <<
", maximum: " << settings.
max;
279 std::cout <<
")\n\t\t" << help <<
".\n";
280 if (settings.
doSet) {
281 std::cout <<
"\t\tSets " <<
name <<
" to " << settings.
set <<
".\n";
283 std::cout <<
"\t\tIf no argument is supplied, " <<
name <<
" is set to " << settings.
set <<
".\n";
284 }
else if (boolType) {
285 std::cout <<
"\t\tIf no argument is supplied, " <<
name <<
" is set to true.\n";
287 if (optionType == 2) {
288 std::cout <<
"\t\tCan be set multiple times, accepts multiple arguments.\n";
293 template <
typename... Args>
302static inline const char* getArg(int32_t&
i,
const char** argv,
const int argc,
bool allowOption =
false)
304 if (
i + 1 < argc && argv[
i + 1][0] && ((allowOption && argv[
i + 1][0] ==
'-' && argv[
i + 1][1] !=
'-') || argv[
i + 1][0] !=
'-')) {
311static inline int32_t qAddOptionGeneric(qConfigSettings<T>& settings, T&
ref, int32_t&
i,
const char** argv,
const int argc, T def, std::function<
T(
const char*)>
func,
bool allowDefault =
false)
313 const char* arg = getArg(
i, argv, argc, !allowDefault);
317 }
else if (allowDefault) {
321 if (!settings.allowEmpty) {
322 printf(
"Argument missing for option %s!\n", getOptName(argv,
i));
331 return qAddOptionGeneric<bool>(
332 settings,
ref,
i, argv, argc, settings.
doDefault ? settings.
set :
true, [](
const char*
a) ->
bool {
340 return qAddOptionGeneric<int8_t>(
341 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int8_t {
349 return qAddOptionGeneric<uint8_t>(
350 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint8_t {
358 return qAddOptionGeneric<int32_t>(
359 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int32_t {
367 return qAddOptionGeneric<uint32_t>(
368 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint32_t {
369 return strtoul(a, nullptr, 0);
376 return qAddOptionGeneric<int16_t>(
377 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int16_t {
385 return qAddOptionGeneric<uint16_t>(
386 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint16_t {
387 return strtoul(a, nullptr, 0);
394 return qAddOptionGeneric<int64_t>(
395 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> int64_t {
396 return strtoll(a, nullptr, 0);
403 return qAddOptionGeneric<uint64_t>(
404 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> uint64_t {
405 return strtoull(a, nullptr, 0);
412 return qAddOptionGeneric<float>(
413 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
float {
414 return (float)atof(a);
421 return qAddOptionGeneric<double>(
422 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
double {
430 return qAddOptionGeneric<const char*>(
431 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) ->
const char* {
439 return qAddOptionGeneric<std::string>(
440 settings,
ref,
i, argv, argc, settings.
set, [](
const char*
a) -> std::string {
448static inline int32_t qAddOptionMinMax(qConfigSettings<T>& settings, T&
ref,
const char* arg)
450 if (settings.checkMin &&
ref < settings.min) {
451 std::cout <<
"Invalid setting for " << arg <<
": minimum threshold exceeded (" <<
ref <<
" < " << settings.min <<
")!\n";
454 if (settings.checkMax &&
ref > settings.max) {
455 std::cout <<
"Invalid setting for " << arg <<
": maximum threshold exceeded (" <<
ref <<
" > " << settings.max <<
")!\n";
471 std::string
msg = std::string(settings.
message) +
"\n";
472 printf(
msg.c_str(), tmp.c_str());
476static inline void qConfigHelp(
const char* subConfig =
nullptr, int32_t followSub = 0)
479 printf(
"Usage Info:");
490static inline int32_t qConfigParse(
int argc,
const char** argv,
const char* )
492 for (int32_t
i = 1;
i < argc;
i++) {
493 const char* thisoption = argv[
i];
499 if (found ==
false) {
500 printf(
"Invalid argument: %s\n", argv[
i]);
GLuint const GLchar * name
GLint GLint GLsizei GLint GLenum GLenum type
typedef void(APIENTRYP PFNGLCULLFACEPROC)(GLenum mode)
GLuint GLsizei const GLchar * message
GLboolean GLboolean GLboolean GLboolean a
bpo::variables_map arguments
int32_t qAddOptionType< float >(qConfigSettings< float > &settings, float &ref, int32_t &i, const char **argv, const int argc, float)
int32_t qAddOptionType< int8_t >(qConfigSettings< int8_t > &settings, int8_t &ref, int32_t &i, const char **argv, const int argc, int8_t)
std::vector< std::function< void()> > qprint_global
int32_t qAddOptionType< uint32_t >(qConfigSettings< uint32_t > &settings, uint32_t &ref, int32_t &i, const char **argv, const int argc, uint32_t)
int32_t qAddOptionType< std::string >(qConfigSettings< std::string > &settings, std::string &ref, int32_t &i, const char **argv, const int argc, std::string)
int32_t qAddOptionType< int64_t >(qConfigSettings< int64_t > &settings, int64_t &ref, int32_t &i, const char **argv, const int argc, int64_t)
int32_t qAddOptionType< uint16_t >(qConfigSettings< uint16_t > &settings, uint16_t &ref, int32_t &i, const char **argv, const int argc, uint16_t)
std::string print_type(T val)
int32_t qAddOptionType< int16_t >(qConfigSettings< int16_t > &settings, int16_t &ref, int32_t &i, const char **argv, const int argc, int16_t)
int32_t qAddOptionType< uint8_t >(qConfigSettings< uint8_t > &settings, uint8_t &ref, int32_t &i, const char **argv, const int argc, uint8_t)
int32_t qAddOptionType< uint64_t >(qConfigSettings< uint64_t > &settings, uint64_t &ref, int32_t &i, const char **argv, const int argc, uint64_t)
int32_t qAddOptionType< bool >(qConfigSettings< bool > &settings, bool &ref, int32_t &i, const char **argv, const int argc, bool)
int32_t qAddOptionType< double >(qConfigSettings< double > &settings, double &ref, int32_t &i, const char **argv, const int argc, double)
int32_t qAddOptionType< const char * >(qConfigSettings< const char * > &settings, const char *&ref, int32_t &i, const char **argv, const int argc, const char *)
int32_t qAddOptionType< int32_t >(qConfigSettings< int32_t > &settings, int32_t &ref, int32_t &i, const char **argv, const int argc, int32_t)
int32_t qAddOptionMinMax< bool >(qConfigSettings< bool > &, bool &, const char *)
Defining DataPointCompositeObject explicitly as copiable.
#define QCONFIG_SETTING(name, type)
#define QCONFIG_SETTING_TEMPLATE(name)
int32_t qConfigParse(int argc, const char **argv, const char *filename)
static int32_t qAddOptionMainTuple(qConfigSettings< typename qSettingsType< T >::settingsType >, T &, int32_t &, const char **, const int32_t)
static int32_t qAddOptionMainTuple(qConfigSettings< typename qSettingsType< T >::settingsType > settings, T &tup, int32_t &i, const char **argv, const int argc)
qConfigSettings(const qConfigSettings< S > v)
static int32_t qAddOptionMain(qConfigSettings< T > &settings, T &ref, int32_t &i, const char **argv, const int argc, T def)
static int32_t qAddOption(T &ref, int32_t &i, const char **argv, const int argc, T def, const char *, Args &&... args)
static int32_t qAddOptionArray(T *ref, int32_t count, int32_t &i, const char **argv, const int argc, const char *, Args &&... args)
static void qProcessSetting(qConfigSettings< T > &settings, qmin_t< T > minval)
static void qAddOptionSettings(qConfigSettings< T > &settings, Arg1 &&arg1, Args &&... args)
static int32_t qAddOptionVec(std::vector< T > &ref, int32_t &i, const char **argv, const int argc, const char *, Args &&... args)
static void qProcessSetting(qConfigSettings< T > &settings, qset_t< S > set)
static void qProcessSetting(qConfigSettings< T > &settings, qmessage_t msg)
static void qProcessSetting(qConfigSettings< T > &settings, qmax_t< T > maxval)
static void qConfigHelpOption(const char *name, const char *type, const char *def, const char *optname, char optnameshort, const char *preopt, char preoptshort, int32_t optionType, const char *help, Args &&... args)
static auto qConfigGetSettings(Args &... args)
static void qProcessSetting(qConfigSettings< T > &settings, qdef_t< S > set)
static void qAddOptionSettings(qConfigSettings< T > &)
uint64_t const void const *restrict const msg