aboutsummaryrefslogtreecommitdiff
path: root/include/_qmacros.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/_qmacros.h')
-rw-r--r--include/_qmacros.h26
1 files changed, 26 insertions, 0 deletions
diff --git a/include/_qmacros.h b/include/_qmacros.h
new file mode 100644
index 0000000..d496581
--- /dev/null
+++ b/include/_qmacros.h
@@ -0,0 +1,26 @@
+#ifndef MLIB__QMACROS_H
+#define MLIB__QMACROS_H
+
+/* Macros for qualifier-preserving functions. These are used to create wrappers
+ around some functions which will return a const-qualified pointer if the
+ input pointer is const-qualified, and a non-const-qualified pointer
+ otherwise.
+
+ The macros are taken from the N3020 proposal for C23. */
+
+/* clang-format off */
+#define _MLIB_PTR_IS_CONST(P) \
+ _Generic(1 ? (P) : (void *)(P), \
+ const void *: 1, \
+ default: 0)
+#define _MLIB_STATIC_IF(P, T, E) \
+ _Generic(&(char[!!(P) + 1]){0}, \
+ char(*)[2]: T, \
+ char(*)[1]: E)
+#define _MLIB_Q_PTR(T, F, S, ...) \
+ _MLIB_STATIC_IF(_MLIB_PTR_IS_CONST((S)), \
+ (const T *)(F)(__VA_ARGS__), \
+ (T *)(F)(__VA_ARGS__))
+/* clang-format on */
+
+#endif /* !MLIB__QMACROS_H */