Improve configure.in so it can be used outside the Samba source tree.
[Samba/gebeck_regimport.git] / lib / ccan / cast / cast.h
blob9f3ecdb7179e78ae9fdea84e801252b989d5f540
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_CAST_H
3 #define CCAN_CAST_H
4 #include "config.h"
5 #include <stdint.h>
6 #include <ccan/build_assert/build_assert.h>
8 /**
9 * cast_signed - cast a (const) char * to/from (const) signed/unsigned char *.
10 * @type: some char * variant.
11 * @expr: expression (of some char * variant) to cast.
13 * Some libraries insist on an unsigned char in various places; cast_signed
14 * makes sure (with suitable compiler) that the expression you are casting
15 * only differs in signed/unsigned, not in type or const-ness.
17 #define cast_signed(type, expr) \
18 ((type)(expr) \
19 + BUILD_ASSERT_OR_ZERO(cast_sign_compatible(type, (expr))))
21 /**
22 * cast_const - remove a const qualifier from a pointer.
23 * @type: some pointer type.
24 * @expr: expression to cast.
26 * This ensures that you are only removing the const qualifier from an
27 * expression. The expression must otherwise match @type.
29 * If @type is a pointer to a pointer, you must use cast_const2 (etc).
31 * Example:
32 * // Dumb open-coded strstr variant.
33 * static char *find_needle(const char *haystack)
34 * {
35 * size_t i;
36 * for (i = 0; i < strlen(haystack); i++)
37 * if (memcmp("needle", haystack+i, strlen("needle")) == 0)
38 * return cast_const(char *, haystack+i);
39 * return NULL;
40 * }
42 #define cast_const(type, expr) \
43 ((type)((intptr_t)(expr) \
44 + BUILD_ASSERT_OR_ZERO(cast_const_compat1((expr), type))))
46 /**
47 * cast_const2 - remove a const qualifier from a pointer to a pointer.
48 * @type: some pointer to pointer type.
49 * @expr: expression to cast.
51 * This ensures that you are only removing the const qualifier from an
52 * expression. The expression must otherwise match @type.
54 #define cast_const2(type, expr) \
55 ((type)((intptr_t)(expr) \
56 + BUILD_ASSERT_OR_ZERO(cast_const_compat2((expr), type))))
58 /**
59 * cast_const3 - remove a const from a pointer to a pointer to a pointer..
60 * @type: some pointer to pointer to pointer type.
61 * @expr: expression to cast.
63 * This ensures that you are only removing the const qualifier from an
64 * expression. The expression must otherwise match @type.
66 #define cast_const3(type, expr) \
67 ((type)((intptr_t)(expr) \
68 + BUILD_ASSERT_OR_ZERO(cast_const_compat3((expr), type))))
71 /**
72 * cast_static - explicit mimic of implicit cast.
73 * @type: some type.
74 * @expr: expression to cast.
76 * This ensures that the cast is not to or from a pointer: it can only be
77 * an implicit cast, such as a pointer to a similar const pointer, or between
78 * integral types.
80 #if HAVE_COMPOUND_LITERALS
81 #define cast_static(type, expr) \
82 ((struct { type x; }){(expr)}.x)
83 #else
84 #define cast_static(type, expr) \
85 ((type)(expr))
86 #endif
88 /* Herein lies the gcc magic to evoke compile errors. */
89 #if HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P && HAVE_TYPEOF
90 #define cast_sign_compatible(t, e) \
91 __builtin_choose_expr( \
92 __builtin_types_compatible_p(__typeof__(t), char *) || \
93 __builtin_types_compatible_p(__typeof__(t), signed char *) || \
94 __builtin_types_compatible_p(__typeof__(t), unsigned char *), \
95 /* if type is not const qualified */ \
96 __builtin_types_compatible_p(__typeof__(e), char *) || \
97 __builtin_types_compatible_p(__typeof__(e), signed char *) || \
98 __builtin_types_compatible_p(__typeof__(e), unsigned char *), \
99 /* and if it is... */ \
100 __builtin_types_compatible_p(__typeof__(e), const char *) || \
101 __builtin_types_compatible_p(__typeof__(e), const signed char *) || \
102 __builtin_types_compatible_p(__typeof__(e), const unsigned char *) ||\
103 __builtin_types_compatible_p(__typeof__(e), char *) || \
104 __builtin_types_compatible_p(__typeof__(e), signed char *) || \
105 __builtin_types_compatible_p(__typeof__(e), unsigned char *) \
108 #define cast_const_strip1(expr) \
109 __typeof__(*(struct { int z; __typeof__(expr) x; }){0}.x)
110 #define cast_const_strip2(expr) \
111 __typeof__(**(struct { int z; __typeof__(expr) x; }){0}.x)
112 #define cast_const_strip3(expr) \
113 __typeof__(***(struct { int z; __typeof__(expr) x; }){0}.x)
114 #define cast_const_compat1(expr, type) \
115 __builtin_types_compatible_p(cast_const_strip1(expr), \
116 cast_const_strip1(type))
117 #define cast_const_compat2(expr, type) \
118 __builtin_types_compatible_p(cast_const_strip2(expr), \
119 cast_const_strip2(type))
120 #define cast_const_compat3(expr, type) \
121 __builtin_types_compatible_p(cast_const_strip3(expr), \
122 cast_const_strip3(type))
123 #else
124 #define cast_sign_compatible(type, expr) \
125 (sizeof(*(type)0) == 1 && sizeof(*(expr)) == 1)
126 #define cast_const_compat1(expr, type) (1)
127 #define cast_const_compat2(expr, type) (1)
128 #define cast_const_compat3(expr, type) (1)
129 #endif
130 #endif /* CCAN_CAST_H */