aboutsummaryrefslogtreecommitdiff
path: root/include/__qmacros.h
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-03-06 16:41:59 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-03-06 16:43:45 +0100
commit5ecd0fc61a1a292426538fcb1e791342e04953b5 (patch)
tree6cdaaf37b22cf834dc1f6089693183d6e6504878 /include/__qmacros.h
Genesis
Diffstat (limited to 'include/__qmacros.h')
-rw-r--r--include/__qmacros.h25
1 files changed, 25 insertions, 0 deletions
diff --git a/include/__qmacros.h b/include/__qmacros.h
new file mode 100644
index 0000000..d765864
--- /dev/null
+++ b/include/__qmacros.h
@@ -0,0 +1,25 @@
+#ifndef MLIB___QMACROS_H
+#define MLIB___QMACROS_H
+
+/* Macros for qualifier-preserving functions. These are wrappers around some
+ functions declared above which will return a const-qualified pointer if the
+ input string is const-qualified, and a non-const-qualified pointer otherwise.
+
+ The macros are taken from the N3020 proposal for C23. */
+
+/* clang-format off */
+#define _RUNE_PTR_IS_CONST(P) \
+ _Generic(1 ? (P) : (void *)(P), \
+ const void *: 1, \
+ default: 0)
+#define _RUNE_STATIC_IF(P, T, E) \
+ _Generic(&(char[!!(P) + 1]){0}, \
+ char(*)[2]: T, \
+ char(*)[1]: E)
+#define _RUNE_Q_PTR(T, F, S, ...) \
+ _RUNE_STATIC_IF(_RUNE_PTR_IS_CONST((S)), \
+ (const T *)(F)(__VA_ARGS__), \
+ (T *)(F)(__VA_ARGS__))
+/* clang-format on */
+
+#endif /* !MLIB___QMACROS_H */