summaryrefslogtreecommitdiffstats
path: root/include/c/qual/const/const.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/c/qual/const/const.h')
-rw-r--r--include/c/qual/const/const.h41
1 files changed, 41 insertions, 0 deletions
diff --git a/include/c/qual/const/const.h b/include/c/qual/const/const.h
new file mode 100644
index 0000000..adfe000
--- /dev/null
+++ b/include/c/qual/const/const.h
@@ -0,0 +1,41 @@
+// Copyright 2022 Alejandro Colomar <alx@kernel.org>
+// SPDX-License-Identifier: LGPL-3.0-or-later WITH LGPL-3.0-linking-exception
+
+
+#ifndef INCLUDE_C_QUAL_CONST_CONST_H_
+#define INCLUDE_C_QUAL_CONST_CONST_H_
+
+
+#include <assert.h>
+
+#include <c/qual/type/type.h>
+
+
+#define c_is_const_p(p) \
+({ \
+ auto p_ = &*(p); \
+ const typeof(p_) q_; \
+ \
+ c_is_same_type(p_, q_); \
+})
+
+
+#define c_const_cast(t, p) \
+({ \
+ static_assert(c_is_same_type(typeof(&*(p)), const t)); \
+ (t) (p); \
+})
+
+
+#define c_const_generic_func(func, p, ...) \
+({ \
+ auto p_ = (p); \
+ const typeof(*p_) *cp_; \
+ \
+ cp_ = func(p_ __VA_OPT__(,) __VA_ARGS__); \
+ \
+ c_const_cast(typeof(p_), cp_); \
+})
+
+
+#endif // Header guard