aboutsummaryrefslogtreecommitdiff
path: root/buildtools/codegen/CodegenMacros.hpp
diff options
context:
space:
mode:
authorrtk0c <[email protected]>2022-05-29 18:30:03 -0700
committerrtk0c <[email protected]>2022-05-29 18:30:03 -0700
commit5112858f9a4adcf76240bcddad19179a5c56d4f3 (patch)
tree22baae0ae1f74be2c9aee241f061cb91f3c30e88 /buildtools/codegen/CodegenMacros.hpp
parent32f00c086b35c5bc32d800b96253b378b9367580 (diff)
Changeset: 47 Add fromstring codegen for enums
Diffstat (limited to 'buildtools/codegen/CodegenMacros.hpp')
-rw-r--r--buildtools/codegen/CodegenMacros.hpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/buildtools/codegen/CodegenMacros.hpp b/buildtools/codegen/CodegenMacros.hpp
new file mode 100644
index 0000000..84c9d09
--- /dev/null
+++ b/buildtools/codegen/CodegenMacros.hpp
@@ -0,0 +1,30 @@
+#pragma once
+
+#include <algorithm>
+
+// I give up, hopefully nothing overflows this buffer
+// TODO handle buffer sizing properly
+
+#define APPEND_LIT(out, str) out += str
+
+#define APPEND_FMT(out, format, ...) \
+ { \
+ char buffer[65536]; \
+ snprintf(buffer, sizeof(buffer), format, __VA_ARGS__); \
+ out += buffer; \
+ }
+
+#define WRITE_LIT(file, str) fwrite(str, sizeof(char), sizeof(str) - 1, file)
+
+// NOTE: snprintf() returns the size written (given an infinite buffer) not including \0
+#define WRITE_FMT(file, format, ...) \
+ { \
+ char buffer[65536]; \
+ int size = snprintf(buffer, sizeof(buffer), format, __VA_ARGS__); \
+ fwrite(buffer, sizeof(char), std::min<int>(size, sizeof(buffer)), file); \
+ }
+
+#define APPEND_LIT_LN(out, str) APPEND_LIT(out, (str "\n"))
+#define APPEND_FMT_LN(out, format, ...) APPEND_FMT(out, (format "\n"), __VA_ARGS__)
+#define WRITE_LIT_LN(out, str) WRITE_LIT(out, (str "\n"))
+#define WRITE_FMT_LN(out, format, ...) WRITE_FMT(out, (format "\n"), __VA_ARGS__)