aboutsummaryrefslogtreecommitdiff
path: root/macro.h
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2019-01-21 11:32:33 +0100
committerHugo Hörnquist <hugo@lysator.liu.se>2019-01-21 11:32:33 +0100
commitd5a5ce02552c4d58c34226eaa0b9c71743630a32 (patch)
treecd73d4fbcf95913da90a2195e99b7cb369f16b17 /macro.h
parentAdd closedir. (diff)
downloadcalp-d5a5ce02552c4d58c34226eaa0b9c71743630a32.tar.gz
calp-d5a5ce02552c4d58c34226eaa0b9c71743630a32.tar.xz
Bunch of renames + macros.
Diffstat (limited to 'macro.h')
-rw-r--r--macro.h52
1 files changed, 51 insertions, 1 deletions
diff --git a/macro.h b/macro.h
index c4b3fc9b..ca3fce24 100644
--- a/macro.h
+++ b/macro.h
@@ -1,10 +1,60 @@
#ifndef MACRO_H
#define MACRO_H
+/*
+ * NOTE This file uses __VA_OPT__. This is not standard compliant.
+ */
+
+/*
+ * Token paste
+ */
#define TP(a, b) a ## b
+#define TP3(a, b, c) a ## b ## c
+#define TP4(a, b, c, d) a ## b ## c ## d
+
+/*
+ * Get length of __VA_ARGS__
+ * Borrowed fram:
+ * https://stackoverflow.com/a/35986932
+ */
+#define VA_ARGS_NUM_PRIV(P1, P2, P3, P4, P5, P6, Pn, ...) Pn
+#define VA_ARGS_NUM(...) VA_ARGS_NUM_PRIV(-1, ## __VA_ARGS__, 5, 4, 3, 2, 1, 0)
+
+#define NEW_HELPER(T, ARG_COUNT) \
+ TP3(T, _init_, ARG_COUNT)
+
+/*
+ * Constructor type name
+ */
+#define CONSTRUCTOR_T(T, C) TP3(T, _init_, C)
+#define CONSTRUCTOR_GEN(parent, child, C) \
+ CONSTRUCTOR_T(parent ## _ ## child, C)
+
+/*
+ * Returns full type of constructor
+ */
+#define CONSTRUCTOR_DECL(T, ...) \
+ CONSTRUCTOR_T(T, VA_ARGS_NUM(__VA_ARGS__)) (T* this __VA_OPT__(,) __VA_ARGS__)
+
+/*
+ * Call the constructor of an object
+ */
+#define CONSTRUCT(T, N, ...) \
+ CONSTRUCTOR_T(T, VA_ARGS_NUM(__VA_ARGS__)) (N __VA_OPT__(,) __VA_ARGS__)
+
+/*
+ * Allocate a new object on the HEAP
+ */
#define NEW(T, N, ...) \
T* N = malloc(sizeof(*N)); \
- TP(T, _init) (N, __VA_ARGS__);
+ CONSTRUCT(T, N, __VA_ARGS__);
+
+/*
+ * Allocate a new object on the STACK
+ */
+#define SNEW(T, N, ...) \
+ T N; \
+ CONSTRUCT(T, & N, __VA_ARGS__);
#endif /* MACRO_H */