Arbutils/src/MacroUtilsGenerator.py

75 lines
3.5 KiB
Python

def generator(num):
file = open("MacroUtils.hpp", "w")
file.write("//\n//\n// THIS FILE IS AUTOGENERATED, DO NOT EDIT!\n//\n//\n")
file.write("// Defines a for each macro utility, with up to " + str(num) + " elements.\n")
file.write("#define ___MACRO_UTILS_FE_0(FUNC, arg)\n")
file.write("#define ___MACRO_UTILS_FE_1(FUNC, arg, X) FUNC(X, arg)\n")
for i in range(2, num + 1):
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")
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")
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")
file.write("#define ___MACRO_UTILS_GET_MACRO(")
for i in range(0, num + 2):
file.write("_" + str(i) + ", ")
file.write("NAME, ...) NAME\n")
file.write("#define MACRO_UTILS_FOR_EACH(action, arg, ...) ___MACRO_UTILS_GET_MACRO(_0, __VA_ARGS__")
for i in range(num + 1, 0, -1):
file.write(", ___MACRO_UTILS_FE_" + str(i))
file.write(") (action, arg, __VA_ARGS__)\n")
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")
file.write("// count arguments\n")
file.write("#define ___MACRO_UTILS_NARGS(...) ___MACRO_UTILS_NARGS_(__VA_ARGS__")
for i in range(num, -1, -1):
file.write(", " + str(i))
file.write(")\n")
file.write("#define ___MACRO_UTILS_NARGS_(")
for i in range(num, 0, -1):
file.write("_" + str(i) + ", ")
file.write("N, ...) N\n")
file.write("// utility (concatenation)\n")
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")
for i in range(0, num + 1):
file.write("#define ___MACRO_UTILS_GET_ELEM_" + str(i) +"(")
for j in range(0, i + 1):
file.write("_" + str(j) + ", ")
file.write("...) _" + str(i) + "\n")
file.write("#define MACRO_UTILS_GET_LAST(...) ___MACRO_UTILS_GET_ELEM( ___MACRO_UTILS_NARGS(__VA_ARGS__), _, __VA_ARGS__ ,")
for i in range(0, num):
file.write(",")
file.write(")\n")
file.write("#define MACRO_UTILS_GET_FIRST(...) ___MACRO_UTILS_GET_ELEM_0(__VA_ARGS__)\n")
file.close()
def main():
generator(256)
if __name__ == "__main__":
main()