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 it is of a given type
8 * @expr: the expression to cast
9 * @oktype: the type we allow
10 * @desttype: the type to cast to
12 * This macro is used to create functions which allow multiple types.
13 * The result of this macro is used somewhere that a @desttype type is
14 * expected: if @expr was of type @oktype, it will be cast to
15 * @desttype type. As a result, if @expr is any type other than
16 * @oktype or @desttype, a compiler warning will be issued.
18 * This macro can be used in static initializers.
20 * This is merely useful for warnings: if the compiler does not
21 * support the primitives required for cast_if_type(), it becomes an
22 * unconditional cast, and the @oktype argument is not used. In
23 * particular, this means that @oktype can be a type which uses
24 * the "typeof": it will not be evaluated if typeof is not supported.
27 * // We can take either an unsigned long or a void *.
28 * void _set_some_value(void *val);
29 * #define set_some_value(expr) \
30 * _set_some_value(cast_if_type((expr), unsigned long, void *))
32 #define cast_if_type(expr, oktype, desttype) \
33 __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), \
34 (desttype)(expr), (expr))
36 #define cast_if_type(expr, oktype, desttype) ((desttype)(expr))
40 * typesafe_cb - cast a callback function if it matches the arg
41 * @rtype: the return type of the callback function
42 * @fn: the callback function to cast
43 * @arg: the (pointer) argument to hand to the callback function.
45 * If a callback function takes a single argument, this macro does
46 * appropriate casts to a function which takes a single void * argument if the
47 * callback provided matches the @arg (or a const or volatile version).
49 * It is assumed that @arg is of pointer type: usually @arg is passed
50 * or assigned to a void * elsewhere anyway.
53 * void _register_callback(void (*fn)(void *arg), void *arg);
54 * #define register_callback(fn, arg) \
55 * _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
57 #define typesafe_cb(rtype, fn, arg) \
58 cast_if_type(cast_if_type(cast_if_type((fn), \
59 rtype (*)(const typeof(*arg)*), \
61 rtype (*)(volatile typeof(*arg) *), \
63 rtype (*)(typeof(arg)), \
67 * typesafe_cb_const - cast a const callback function if it matches the arg
68 * @rtype: the return type of the callback function
69 * @fn: the callback function to cast
70 * @arg: the (pointer) argument to hand to the callback function.
72 * If a callback function takes a single argument, this macro does appropriate
73 * casts to a function which takes a single const void * argument if the
74 * callback provided matches the @arg.
76 * It is assumed that @arg is of pointer type: usually @arg is passed
77 * or assigned to a void * elsewhere anyway.
80 * void _register_callback(void (*fn)(const void *arg), const void *arg);
81 * #define register_callback(fn, arg) \
82 * _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
84 #define typesafe_cb_const(rtype, fn, arg) \
86 rtype (*)(const typeof(*arg)*), rtype (*)(const void *))
89 * typesafe_cb_preargs - cast a callback function if it matches the arg
90 * @rtype: the return type of the callback function
91 * @fn: the callback function to cast
92 * @arg: the (pointer) argument to hand to the callback function.
94 * This is a version of typesafe_cb() for callbacks that take other arguments
98 * void _register_callback(void (*fn)(int, void *arg), void *arg);
99 * #define register_callback(fn, arg) \
100 * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
103 #define typesafe_cb_preargs(rtype, fn, arg, ...) \
104 cast_if_type(cast_if_type(cast_if_type((fn), \
105 rtype (*)(__VA_ARGS__, \
106 const typeof(*arg) *),\
107 rtype (*)(__VA_ARGS__, \
109 rtype (*)(__VA_ARGS__, \
110 volatile typeof(*arg) *), \
111 rtype (*)(__VA_ARGS__, void *)), \
112 rtype (*)(__VA_ARGS__, typeof(arg)), \
113 rtype (*)(__VA_ARGS__, void *))
116 * typesafe_cb_postargs - cast a callback function if it matches the arg
117 * @rtype: the return type of the callback function
118 * @fn: the callback function to cast
119 * @arg: the (pointer) argument to hand to the callback function.
121 * This is a version of typesafe_cb() for callbacks that take other arguments
125 * void _register_callback(void (*fn)(void *arg, int), void *arg);
126 * #define register_callback(fn, arg) \
127 * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
130 #define typesafe_cb_postargs(rtype, fn, arg, ...) \
131 cast_if_type(cast_if_type(cast_if_type((fn), \
132 rtype (*)(const typeof(*arg) *, \
136 rtype (*)(volatile typeof(*arg) *, \
138 rtype (*)(void *, __VA_ARGS__)), \
139 rtype (*)(typeof(arg), __VA_ARGS__), \
140 rtype (*)(void *, __VA_ARGS__))
143 * typesafe_cb_cmp - cast a compare function if it matches the arg
144 * @rtype: the return type of the callback function
145 * @fn: the callback function to cast
146 * @arg: the (pointer) argument(s) to hand to the compare function.
148 * If a callback function takes two matching-type arguments, this macro does
149 * appropriate casts to a function which takes two const void * arguments if
150 * the callback provided takes two a const pointers to @arg.
152 * It is assumed that @arg is of pointer type: usually @arg is passed
153 * or assigned to a void * elsewhere anyway.
156 * void _my_qsort(void *base, size_t nmemb, size_t size,
157 * int (*cmp)(const void *, const void *));
158 * #define my_qsort(base, nmemb, cmpfn) \
159 * _my_qsort((base), (nmemb), sizeof(*(base)), \
160 * typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
162 #define typesafe_cb_cmp(rtype, cmpfn, arg) \
163 cast_if_type((cmpfn), \
164 rtype (*)(const typeof(*arg)*, const typeof(*arg)*), \
165 rtype (*)(const void *, const void *))
166 #endif /* CCAN_CAST_IF_TYPE_H */