aboutsummaryrefslogtreecommitdiff
path: root/include/__qmacros.h
blob: fd7b8039c111ae440e50bca089bb80090fa5ebaf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 */