]> git.ozlabs.org Git - ccan/blob - ccan/typesafe_cb/typesafe_cb.h
typesafe_cb: more support for typesafe compare functions.
[ccan] / ccan / typesafe_cb / typesafe_cb.h
1 #ifndef CCAN_CAST_IF_TYPE_H
2 #define CCAN_CAST_IF_TYPE_H
3 #include "config.h"
4
5 #if HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P
6 /**
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
11  *
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.
17  *
18  * This macro can be used in static initializers.
19  *
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.
25  *
26  * Example:
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 *))
31  */
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))
35 #else
36 #define cast_if_type(expr, oktype, desttype) ((desttype)(expr))
37 #endif
38
39 /**
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.
44  *
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).
48  *
49  * It is assumed that @arg is of pointer type: usually @arg is passed
50  * or assigned to a void * elsewhere anyway.
51  *
52  * Example:
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))
56  */
57 #define typesafe_cb(rtype, fn, arg)                                     \
58         cast_if_type(cast_if_type(cast_if_type((fn),                    \
59                                                rtype (*)(const typeof(*arg)*), \
60                                                rtype (*)(void *)),      \
61                                   rtype (*)(volatile typeof(*arg) *),   \
62                                   rtype (*)(void *)),                   \
63                      rtype (*)(typeof(arg)),                            \
64                      rtype (*)(void *))
65
66 /**
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.
71  *
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.
75  *
76  * It is assumed that @arg is of pointer type: usually @arg is passed
77  * or assigned to a void * elsewhere anyway.
78  *
79  * Example:
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))
83  */
84 #define typesafe_cb_const(rtype, fn, arg)                               \
85         cast_if_type((fn),                                              \
86                      rtype (*)(const typeof(*arg)*), rtype (*)(const void *))
87
88 /**
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.
93  *
94  * This is a version of typesafe_cb() for callbacks that take other arguments
95  * before the @arg.
96  *
97  * Example:
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),\
101  *                                 (arg))
102  */
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__,   \
108                                                          void *)),      \
109                                   rtype (*)(__VA_ARGS__,                \
110                                             volatile typeof(*arg) *),   \
111                                   rtype (*)(__VA_ARGS__, void *)),      \
112                      rtype (*)(__VA_ARGS__, typeof(arg)),               \
113                      rtype (*)(__VA_ARGS__, void *))
114
115 /**
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.
120  *
121  * This is a version of typesafe_cb() for callbacks that take other arguments
122  * after the @arg.
123  *
124  * Example:
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),\
128  *                                 (arg))
129  */
130 #define typesafe_cb_postargs(rtype, fn, arg, ...)                       \
131         cast_if_type(cast_if_type(cast_if_type((fn),                    \
132                                                rtype (*)(const typeof(*arg) *, \
133                                                          __VA_ARGS__),  \
134                                                rtype (*)(void *,        \
135                                                          __VA_ARGS__)), \
136                                   rtype (*)(volatile typeof(*arg) *,    \
137                                             __VA_ARGS__),               \
138                                   rtype (*)(void *, __VA_ARGS__)),      \
139                      rtype (*)(typeof(arg), __VA_ARGS__),               \
140                      rtype (*)(void *, __VA_ARGS__))
141
142 /**
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.
147  *
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.
151  *
152  * It is assumed that @arg is of pointer type: usually @arg is passed
153  * or assigned to a void * elsewhere anyway.
154  *
155  * Example:
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))
161  */
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 */