2020-03-11 10:38:36 +00:00
|
|
|
def generator(num):
|
|
|
|
file = open("MacroUtils.hpp", "w")
|
|
|
|
|
|
|
|
file.write("//\n//\n// THIS FILE IS AUTOGENERATED, DO NOT EDIT!\n//\n//\n")
|
|
|
|
|
2020-04-18 12:57:23 +00:00
|
|
|
file.write("// Defines a for each macro utility, with up to " + str(num) + " elements.\n")
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_FE_0(FUNC, arg)\n")
|
|
|
|
file.write("#define ___MACRO_UTILS_FE_1(FUNC, arg, X) FUNC(X, arg)\n")
|
2020-03-11 10:38:36 +00:00
|
|
|
for i in range(2, num + 1):
|
2020-04-18 12:57:23 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_FE_" + str(i) + "(FUNC, arg, X, ...) FUNC(X, arg) ___MACRO_UTILS_FE_" +
|
|
|
|
str(i - 1) + "(FUNC, arg, __VA_ARGS__)\n")
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_FE_" + str(num + 1) +"(FUNC, arg, X, ...) THE_FOREACH_MACRO_CURRENTLY_ONLY_SUPPORTS_UP_TO_" +
|
|
|
|
str(num) + "_VALUES ___MACRO_UTILS_FE_" + str(num) + "(FUNC, arg, __VA_ARGS__)\n")
|
2020-04-18 12:57:23 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_FE_VAL_0(FUNC, arg)\n")
|
|
|
|
file.write("#define ___MACRO_UTILS_FE_VAL_1(FUNC, arg, X) FUNC(X, arg)\n")
|
|
|
|
for i in range(2, num + 1):
|
|
|
|
file.write("#define ___MACRO_UTILS_FE_VAL_" + str(i) + "(FUNC, arg, X, ...) FUNC(X, arg -" + str(i - 1) + ") ___MACRO_UTILS_FE_VAL_" +
|
|
|
|
str(i - 1) + "(FUNC, arg , __VA_ARGS__)\n")
|
|
|
|
file.write("#define ___MACRO_UTILS_FE_VAL_" + str(num + 1) +"(FUNC, arg, X, ...) THE_FOREACH_MACRO_CURRENTLY_ONLY_SUPPORTS_UP_TO_" +
|
|
|
|
str(num) + "_VALUES ___MACRO_UTILS_FE_VAL_" + str(num) + "(FUNC, arg, __VA_ARGS__)\n")
|
|
|
|
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_GET_MACRO(")
|
2020-03-11 10:38:36 +00:00
|
|
|
for i in range(0, num + 2):
|
|
|
|
file.write("_" + str(i) + ", ")
|
|
|
|
file.write("NAME, ...) NAME\n")
|
2020-04-18 12:57:23 +00:00
|
|
|
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define MACRO_UTILS_FOR_EACH(action, arg, ...) ___MACRO_UTILS_GET_MACRO(_0, __VA_ARGS__")
|
2020-03-11 10:38:36 +00:00
|
|
|
for i in range(num + 1, 0, -1):
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write(", ___MACRO_UTILS_FE_" + str(i))
|
2020-03-11 10:38:36 +00:00
|
|
|
file.write(") (action, arg, __VA_ARGS__)\n")
|
|
|
|
|
2020-04-18 12:57:23 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_GET_MACRO_WITH_VALUE(")
|
|
|
|
for i in range(0, num + 2):
|
|
|
|
file.write("_" + str(i) + ", ")
|
|
|
|
file.write("NAME, ...) NAME\n")
|
|
|
|
|
|
|
|
file.write("#define MACRO_UTILS_FOR_EACH_WITH_VALUE(action, arg, ...) ___MACRO_UTILS_GET_MACRO_WITH_VALUE(_0, __VA_ARGS__")
|
|
|
|
for i in range(num + 1, 0, -1):
|
|
|
|
file.write(", ___MACRO_UTILS_FE_VAL_" + str(i))
|
|
|
|
file.write(") (action, arg, __VA_ARGS__)\n")
|
|
|
|
|
2020-03-11 10:38:36 +00:00
|
|
|
file.write("// count arguments\n")
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_NARGS(...) ___MACRO_UTILS_NARGS_(__VA_ARGS__")
|
2020-03-11 10:38:36 +00:00
|
|
|
for i in range(num, -1, -1):
|
|
|
|
file.write(", " + str(i))
|
|
|
|
file.write(")\n")
|
|
|
|
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_NARGS_(")
|
2020-03-11 10:38:36 +00:00
|
|
|
for i in range(num, 0, -1):
|
|
|
|
file.write("_" + str(i) + ", ")
|
|
|
|
file.write("N, ...) N\n")
|
|
|
|
file.write("// utility (concatenation)\n")
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_CONC(A, B) ___MACRO_UTILS_CONC_(A, B)\n")
|
|
|
|
file.write("#define ___MACRO_UTILS_CONC_(A, B) A##B\n\n")
|
|
|
|
file.write("#define ___MACRO_UTILS_GET_ELEM(N, ...) ___MACRO_UTILS_CONC( ___MACRO_UTILS_GET_ELEM_, N)(__VA_ARGS__)\n")
|
2020-03-11 10:38:36 +00:00
|
|
|
|
|
|
|
for i in range(0, num + 1):
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define ___MACRO_UTILS_GET_ELEM_" + str(i) +"(")
|
2020-03-11 10:38:36 +00:00
|
|
|
for j in range(0, i + 1):
|
|
|
|
file.write("_" + str(j) + ", ")
|
|
|
|
file.write("...) _" + str(i) + "\n")
|
|
|
|
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define MACRO_UTILS_GET_LAST(...) ___MACRO_UTILS_GET_ELEM( ___MACRO_UTILS_NARGS(__VA_ARGS__), _, __VA_ARGS__ ,")
|
2020-03-11 10:38:36 +00:00
|
|
|
for i in range(0, num):
|
|
|
|
file.write(",")
|
|
|
|
file.write(")\n")
|
2020-03-11 10:49:56 +00:00
|
|
|
file.write("#define MACRO_UTILS_GET_FIRST(...) ___MACRO_UTILS_GET_ELEM_0(__VA_ARGS__)\n")
|
2020-03-11 10:38:36 +00:00
|
|
|
|
|
|
|
file.close()
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
generator(256)
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|