]> git.ozlabs.org Git - ccan/blob - ccan/typesafe_cb/typesafe_cb.h
tdb2: check PID if we are holding a lock.
[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 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
12  *
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 @test is exactly of type @oktype, then @expr will be
16  * cast to @desttype type, otherwise left alone.
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 @test and @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(e)                       \
30  *              _set_some_value(cast_if_type(void *, (e), (e), unsigned long))
31  */
32 #define cast_if_type(desttype, expr, test, oktype)                      \
33         __builtin_choose_expr(__builtin_types_compatible_p(typeof(test), oktype), \
34                         (desttype)(expr), (expr))
35 #else
36 #define cast_if_type(desttype, expr, test, 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  * @test: the expression to test
44  * @ok1: the first type we allow
45  * @ok2: the second type we allow
46  * @ok3: the third type we allow
47  *
48  * This is a convenient wrapper for multiple cast_if_type() calls.  You can
49  * chain them inside each other (ie. use cast_if_any() for expr) if you need
50  * more than 3 arguments.
51  *
52  * Example:
53  *      // We can take either a long, unsigned long, void * or a const void *.
54  *      void _set_some_value(void *val);
55  *      #define set_some_value(expr)                                    \
56  *              _set_some_value(cast_if_any(void *, (expr), (expr),     \
57  *                                          long, unsigned long, const void *))
58  */
59 #define cast_if_any(desttype, expr, test, ok1, ok2, ok3)                \
60         cast_if_type(desttype,                                          \
61                      cast_if_type(desttype,                             \
62                                   cast_if_type(desttype, (expr), (test), ok1), \
63                                   (test),                               \
64                                   ok2),                                 \
65                      (test),                                            \
66                      ok3)
67
68 /**
69  * typesafe_cb - cast a callback function if it matches the arg
70  * @rtype: the return type of the callback function
71  * @fn: the callback function to cast
72  * @arg: the (pointer) argument to hand to the callback function.
73  *
74  * If a callback function takes a single argument, this macro does
75  * appropriate casts to a function which takes a single void * argument if the
76  * callback provided matches the @arg (or a const or volatile version).
77  *
78  * It is assumed that @arg is of pointer type: usually @arg is passed
79  * or assigned to a void * elsewhere anyway.
80  *
81  * This will not work with a NULL @fn argument: see typesafe_cb_def or
82  * typesafe_cb_exact.
83  *
84  * Example:
85  *      void _register_callback(void (*fn)(void *arg), void *arg);
86  *      #define register_callback(fn, arg) \
87  *              _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
88  */
89 #define typesafe_cb(rtype, fn, arg)                     \
90         cast_if_type(rtype (*)(void *), (fn), (fn)(arg), rtype)
91
92 /**
93  * typesafe_cb_def - cast a callback fn if it matches arg (of defined type)
94  * @rtype: the return type of the callback function
95  * @fn: the callback function to cast
96  * @arg: the (pointer) argument to hand to the callback function.
97  *
98  * This is typesafe_cb(), except the type must be defined (eg. if it's
99  * struct foo *, the definition of struct foo must be visible).  For many
100  * applications, this is reasonable.
101  *
102  * This variant can accept @fn equal to NULL.
103  *
104  * Example:
105  *      void _register_callback(void (*fn)(void *arg), void *arg);
106  *      #define register_callback(fn, arg) \
107  *              _register_callback(typesafe_cb_def(void, (fn), (arg)), (arg))
108  */
109 #define typesafe_cb_def(rtype, fn, arg)                         \
110         cast_if_any(rtype (*)(void *), (fn), 0?(fn):(fn),       \
111                     rtype (*)(typeof(*arg)*),                   \
112                     rtype (*)(const typeof(*arg)*),             \
113                     rtype (*)(volatile typeof(*arg)*))
114
115 /**
116  * typesafe_cb_exact - cast a callback fn if it exactly matches 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 typesafe_cb(), except the @fn can be NULL, or must exactly match
122  * the @arg type (no const or volatile).
123  *
124  * Example:
125  *      void _register_callback(void (*fn)(void *arg), void *arg);
126  *      #define register_callback(fn, arg) \
127  *              _register_callback(typesafe_cb_exact(void, (fn), (arg)), (arg))
128  */
129 #define typesafe_cb_exact(rtype, fn, arg)                               \
130         cast_if_type(rtype (*)(void *), (fn), 0?(fn):(fn),              \
131                      rtype (*)(typeof(arg)))
132
133 /**
134  * typesafe_cb_const - cast a const callback function if it matches the arg
135  * @rtype: the return type of the callback function
136  * @fn: the callback function to cast
137  * @arg: the (pointer) argument to hand to the callback function.
138  *
139  * If a callback function takes a single argument, this macro does appropriate
140  * casts to a function which takes a single const void * argument if the
141  * callback provided matches the @arg.
142  *
143  * It is assumed that @arg is of pointer type: usually @arg is passed
144  * or assigned to a void * elsewhere anyway.
145  *
146  * Example:
147  *      void _register_callback(void (*fn)(const void *arg), const void *arg);
148  *      #define register_callback(fn, arg) \
149  *              _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
150  */
151 #define typesafe_cb_const(rtype, fn, arg)                       \
152         cast_if_type(rtype (*)(const void *), (fn), (fn)(arg), rtype)
153
154 /**
155  * typesafe_cb_preargs - cast a callback function if it matches the arg
156  * @rtype: the return type of the callback function
157  * @fn: the callback function to cast
158  * @arg: the (pointer) argument to hand to the callback function.
159  *
160  * This is a version of typesafe_cb() for callbacks that take other arguments
161  * before the @arg.
162  *
163  * Example:
164  *      void _register_callback(void (*fn)(int, void *arg), void *arg);
165  *      #define register_callback(fn, arg) \
166  *              _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
167  *                                 (arg))
168  */
169 #define typesafe_cb_preargs(rtype, fn, arg, ...)                        \
170         cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), 0?(fn):(fn), \
171                      rtype (*)(__VA_ARGS__, typeof(arg)))
172
173 /**
174  * typesafe_cb_postargs - cast a callback function if it matches the arg
175  * @rtype: the return type of the callback function
176  * @fn: the callback function to cast
177  * @arg: the (pointer) argument to hand to the callback function.
178  *
179  * This is a version of typesafe_cb() for callbacks that take other arguments
180  * after the @arg.
181  *
182  * Example:
183  *      void _register_callback(void (*fn)(void *arg, int), void *arg);
184  *      #define register_callback(fn, arg) \
185  *              _register_callback(typesafe_cb_postargs(void, (fn), (arg), int),\
186  *                                 (arg))
187  */
188 #define typesafe_cb_postargs(rtype, fn, arg, ...)                       \
189         cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), 0?(fn):(fn), \
190                      rtype (*)(typeof(arg), __VA_ARGS__))
191
192 /**
193  * typesafe_cb_cmp - cast a compare function if it matches the arg
194  * @rtype: the return type of the callback function
195  * @fn: the callback function to cast
196  * @arg: the (pointer) argument(s) to hand to the compare function.
197  *
198  * If a callback function takes two matching-type arguments, this macro does
199  * appropriate casts to a function which takes two const void * arguments if
200  * the callback provided takes two a const pointers to @arg.
201  *
202  * It is assumed that @arg is of pointer type: usually @arg is passed
203  * or assigned to a void * elsewhere anyway.  Note also that the type
204  * arg points to must be defined.
205  *
206  * Example:
207  *      void _my_qsort(void *base, size_t nmemb, size_t size,
208  *                     int (*cmp)(const void *, const void *));
209  *      #define my_qsort(base, nmemb, cmpfn) \
210  *              _my_qsort((base), (nmemb), sizeof(*(base)), \
211  *                        typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
212  */
213 #define typesafe_cb_cmp(rtype, cmpfn, arg)                              \
214         cast_if_type(rtype (*)(const void *, const void *),             \
215                      (cmpfn), 0?(cmpfn):(cmpfn),                        \
216                      rtype (*)(const typeof(*arg)*, const typeof(*arg)*))
217                      
218 #endif /* CCAN_CAST_IF_TYPE_H */