]> git.ozlabs.org Git - ccan/blob - ccan/typesafe_cb/typesafe_cb.h
98d0d4f058657e33b1406a620cce5ba04ef84251
[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  * @desttype: the type to cast to
9  * @expr: the expression to cast
10  * @oktype: the type we allow
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(void *, (expr), unsigned long))
31  */
32 #define cast_if_type(desttype, expr, oktype)                            \
33 __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), \
34                         (desttype)(expr), (expr))
35 #else
36 #define cast_if_type(desttype, expr, oktype) ((desttype)(expr))
37 #endif
38
39 /**
40  * cast_if_any - only cast an expression if it is one of the three given types
41  * @desttype: the type to cast to
42  * @expr: the expression to cast
43  * @ok1: the first type we allow
44  * @ok2: the second type we allow
45  * @ok3: the third type we allow
46  *
47  * This is a convenient wrapper for multiple cast_if_type() calls.  You can
48  * chain them inside each other (ie. use cast_if_any() for expr) if you need
49  * more than 3 arguments.
50  *
51  * Example:
52  *      // We can take either a long, unsigned long, void * or a const void *.
53  *      void _set_some_value(void *val);
54  *      #define set_some_value(expr)                                    \
55  *              _set_some_value(cast_if_any(void *, (expr),             \
56  *                                          long, unsigned long, const void *))
57  */
58 #define cast_if_any(desttype, expr, ok1, ok2, ok3)                      \
59         cast_if_type(desttype,                                          \
60                      cast_if_type(desttype,                             \
61                                   cast_if_type(desttype, (expr), ok1),  \
62                                   ok2),                                 \
63                      ok3)
64
65 /**
66  * typesafe_cb - cast a callback function if it matches the arg
67  * @rtype: the return type of the callback function
68  * @fn: the callback function to cast
69  * @arg: the (pointer) argument to hand to the callback function.
70  *
71  * If a callback function takes a single argument, this macro does
72  * appropriate casts to a function which takes a single void * argument if the
73  * callback provided matches the @arg (or a const or volatile version).
74  *
75  * It is assumed that @arg is of pointer type: usually @arg is passed
76  * or assigned to a void * elsewhere anyway.
77  *
78  * Example:
79  *      void _register_callback(void (*fn)(void *arg), void *arg);
80  *      #define register_callback(fn, arg) \
81  *              _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
82  */
83 #define typesafe_cb(rtype, fn, arg)                     \
84         cast_if_any(rtype (*)(void *), (fn),            \
85                     rtype (*)(typeof(*arg)*),           \
86                     rtype (*)(const typeof(*arg)*),     \
87                     rtype (*)(volatile typeof(*arg)*))
88
89 /**
90  * typesafe_cb_const - cast a const callback function if it matches the arg
91  * @rtype: the return type of the callback function
92  * @fn: the callback function to cast
93  * @arg: the (pointer) argument to hand to the callback function.
94  *
95  * If a callback function takes a single argument, this macro does appropriate
96  * casts to a function which takes a single const void * argument if the
97  * callback provided matches the @arg.
98  *
99  * It is assumed that @arg is of pointer type: usually @arg is passed
100  * or assigned to a void * elsewhere anyway.
101  *
102  * Example:
103  *      void _register_callback(void (*fn)(const void *arg), const void *arg);
104  *      #define register_callback(fn, arg) \
105  *              _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
106  */
107 #define typesafe_cb_const(rtype, fn, arg)                               \
108         cast_if_type(rtype (*)(const void *), (fn),                     \
109                      rtype (*)(const typeof(*arg)*))
110
111 /**
112  * typesafe_cb_preargs - cast a callback function if it matches the arg
113  * @rtype: the return type of the callback function
114  * @fn: the callback function to cast
115  * @arg: the (pointer) argument to hand to the callback function.
116  *
117  * This is a version of typesafe_cb() for callbacks that take other arguments
118  * before the @arg.
119  *
120  * Example:
121  *      void _register_callback(void (*fn)(int, void *arg), void *arg);
122  *      #define register_callback(fn, arg) \
123  *              _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
124  *                                 (arg))
125  */
126 #define typesafe_cb_preargs(rtype, fn, arg, ...)                        \
127         cast_if_any(rtype (*)(__VA_ARGS__, void *), (fn),               \
128                     rtype (*)(__VA_ARGS__, typeof(arg)),                \
129                     rtype (*)(__VA_ARGS__, const typeof(*arg) *),       \
130                     rtype (*)(__VA_ARGS__, volatile typeof(*arg) *))
131
132 /**
133  * typesafe_cb_postargs - cast a callback function if it matches the arg
134  * @rtype: the return type of the callback function
135  * @fn: the callback function to cast
136  * @arg: the (pointer) argument to hand to the callback function.
137  *
138  * This is a version of typesafe_cb() for callbacks that take other arguments
139  * after the @arg.
140  *
141  * Example:
142  *      void _register_callback(void (*fn)(void *arg, int), void *arg);
143  *      #define register_callback(fn, arg) \
144  *              _register_callback(typesafe_cb_postargs(void, (fn), (arg), int),\
145  *                                 (arg))
146  */
147 #define typesafe_cb_postargs(rtype, fn, arg, ...)                       \
148         cast_if_any(rtype (*)(void *, __VA_ARGS__), (fn),               \
149                     rtype (*)(typeof(arg), __VA_ARGS__),                \
150                     rtype (*)(const typeof(*arg) *, __VA_ARGS__),       \
151                     rtype (*)(volatile typeof(*arg) *, __VA_ARGS__))
152
153 /**
154  * typesafe_cb_cmp - cast a compare function if it matches the arg
155  * @rtype: the return type of the callback function
156  * @fn: the callback function to cast
157  * @arg: the (pointer) argument(s) to hand to the compare function.
158  *
159  * If a callback function takes two matching-type arguments, this macro does
160  * appropriate casts to a function which takes two const void * arguments if
161  * the callback provided takes two a const pointers to @arg.
162  *
163  * It is assumed that @arg is of pointer type: usually @arg is passed
164  * or assigned to a void * elsewhere anyway.
165  *
166  * Example:
167  *      void _my_qsort(void *base, size_t nmemb, size_t size,
168  *                     int (*cmp)(const void *, const void *));
169  *      #define my_qsort(base, nmemb, cmpfn) \
170  *              _my_qsort((base), (nmemb), sizeof(*(base)), \
171  *                        typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
172  */
173 #define typesafe_cb_cmp(rtype, cmpfn, arg)                              \
174         cast_if_type(rtype (*)(const void *, const void *), (cmpfn),    \
175                      rtype (*)(const typeof(*arg)*, const typeof(*arg)*))
176                      
177 #endif /* CCAN_CAST_IF_TYPE_H */