1 #ifndef CCAN_CAST_IF_TYPE_H
2 #define CCAN_CAST_IF_TYPE_H
5 #if HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P
7 * cast_if_type - only cast an expression if test matches a given type
8 * @desttype: the type to cast to
9 * @expr: the expression to cast
10 * @test: the expression to test
11 * @oktype: the type we allow
13 * This macro is used to create functions which allow multiple types.
14 * The result of this macro is used somewhere that a @desttype type is
15 * expected: if @expr was of type @oktype, it will be cast to
16 * @desttype type. As a result, if @expr is any type other than
17 * @oktype or @desttype, a compiler warning will be issued.
19 * This macro can be used in static initializers.
21 * This is merely useful for warnings: if the compiler does not
22 * support the primitives required for cast_if_type(), it becomes an
23 * unconditional cast, and the @test and @oktype argument is not used. In
24 * particular, this means that @oktype can be a type which uses
25 * the "typeof": it will not be evaluated if typeof is not supported.
28 * // We can take either an unsigned long or a void *.
29 * void _set_some_value(void *val);
30 * #define set_some_value(e) \
31 * _set_some_value(cast_if_type(void *, (e), (e), unsigned long))
33 #define cast_if_type(desttype, expr, test, oktype) \
34 __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), \
35 (desttype)(expr), (expr))
37 #define cast_if_type(desttype, expr, test, oktype) ((desttype)(expr))
41 * cast_if_any - only cast an expression if it is one of the three given types
42 * @desttype: the type to cast to
43 * @expr: the expression to cast
44 * @test: the expression to test
45 * @ok1: the first type we allow
46 * @ok2: the second type we allow
47 * @ok3: the third type we allow
49 * This is a convenient wrapper for multiple cast_if_type() calls. You can
50 * chain them inside each other (ie. use cast_if_any() for expr) if you need
51 * more than 3 arguments.
54 * // We can take either a long, unsigned long, void * or a const void *.
55 * void _set_some_value(void *val);
56 * #define set_some_value(expr) \
57 * _set_some_value(cast_if_any(void *, (expr), (expr), \
58 * long, unsigned long, const void *))
60 #define cast_if_any(desttype, expr, test, ok1, ok2, ok3) \
61 cast_if_type(desttype, \
62 cast_if_type(desttype, \
63 cast_if_type(desttype, (expr), (test), ok1), \
70 * typesafe_cb - cast a callback function if it matches the arg
71 * @rtype: the return type of the callback function
72 * @fn: the callback function to cast
73 * @arg: the (pointer) argument to hand to the callback function.
75 * If a callback function takes a single argument, this macro does
76 * appropriate casts to a function which takes a single void * argument if the
77 * callback provided matches the @arg (or a const or volatile version).
79 * It is assumed that @arg is of pointer type: usually @arg is passed
80 * or assigned to a void * elsewhere anyway.
83 * void _register_callback(void (*fn)(void *arg), void *arg);
84 * #define register_callback(fn, arg) \
85 * _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
87 #define typesafe_cb(rtype, fn, arg) \
88 cast_if_type(rtype (*)(void *), (fn), (fn)(arg), rtype)
91 * typesafe_cb_const - cast a const callback function if it matches the arg
92 * @rtype: the return type of the callback function
93 * @fn: the callback function to cast
94 * @arg: the (pointer) argument to hand to the callback function.
96 * If a callback function takes a single argument, this macro does appropriate
97 * casts to a function which takes a single const void * argument if the
98 * callback provided matches the @arg.
100 * It is assumed that @arg is of pointer type: usually @arg is passed
101 * or assigned to a void * elsewhere anyway.
104 * void _register_callback(void (*fn)(const void *arg), const void *arg);
105 * #define register_callback(fn, arg) \
106 * _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
108 #define typesafe_cb_const(rtype, fn, arg) \
109 sizeof((fn)((const void *)0)), \
110 cast_if_type(rtype (*)(const void *), \
111 (fn), (fn)(arg), rtype (*)(typeof(arg)))
114 * typesafe_cb_preargs - cast a callback function if it matches the arg
115 * @rtype: the return type of the callback function
116 * @fn: the callback function to cast
117 * @arg: the (pointer) argument to hand to the callback function.
119 * This is a version of typesafe_cb() for callbacks that take other arguments
123 * void _register_callback(void (*fn)(int, void *arg), void *arg);
124 * #define register_callback(fn, arg) \
125 * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
128 #define typesafe_cb_preargs(rtype, fn, arg, ...) \
129 cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), (fn), \
130 rtype (*)(__VA_ARGS__, typeof(arg)))
132 * typesafe_cb_postargs - cast a callback function if it matches the arg
133 * @rtype: the return type of the callback function
134 * @fn: the callback function to cast
135 * @arg: the (pointer) argument to hand to the callback function.
137 * This is a version of typesafe_cb() for callbacks that take other arguments
141 * void _register_callback(void (*fn)(void *arg, int), void *arg);
142 * #define register_callback(fn, arg) \
143 * _register_callback(typesafe_cb_postargs(void, (fn), (arg), int),\
146 #define typesafe_cb_postargs(rtype, fn, arg, ...) \
147 cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn), \
148 rtype (*)(typeof(arg), __VA_ARGS__))
150 * typesafe_cb_cmp - cast a compare function if it matches the arg
151 * @rtype: the return type of the callback function
152 * @fn: the callback function to cast
153 * @arg: the (pointer) argument(s) to hand to the compare function.
155 * If a callback function takes two matching-type arguments, this macro does
156 * appropriate casts to a function which takes two const void * arguments if
157 * the callback provided takes two a const pointers to @arg.
159 * It is assumed that @arg is of pointer type: usually @arg is passed
160 * or assigned to a void * elsewhere anyway. Note also that the type
161 * arg points to must be defined.
164 * void _my_qsort(void *base, size_t nmemb, size_t size,
165 * int (*cmp)(const void *, const void *));
166 * #define my_qsort(base, nmemb, cmpfn) \
167 * _my_qsort((base), (nmemb), sizeof(*(base)), \
168 * typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
170 #define typesafe_cb_cmp(rtype, cmpfn, arg) \
171 cast_if_type(rtype (*)(const void *, const void *), (cmpfn), \
172 rtype (*)(const typeof(*arg)*, const typeof(*arg)*))
174 #endif /* CCAN_CAST_IF_TYPE_H */