]> git.ozlabs.org Git - ccan/blob - ccan/typesafe_cb/typesafe_cb.h
typesafe_cb: handle pointers to undefined struct types.
[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 @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.
18  *
19  * This macro can be used in static initializers.
20  *
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.
26  *
27  * Example:
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))
32  */
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))
36 #else
37 #define cast_if_type(desttype, expr, test, oktype) ((desttype)(expr))
38 #endif
39
40 /**
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
48  *
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.
52  *
53  * Example:
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 *))
59  */
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), \
64                                   ok2),                                 \
65                      ok3)
66
67 /**
68  * typesafe_cb - cast a callback function if it matches the arg
69  * @rtype: the return type of the callback function
70  * @fn: the callback function to cast
71  * @arg: the (pointer) argument to hand to the callback function.
72  *
73  * If a callback function takes a single argument, this macro does
74  * appropriate casts to a function which takes a single void * argument if the
75  * callback provided matches the @arg (or a const or volatile version).
76  *
77  * It is assumed that @arg is of pointer type: usually @arg is passed
78  * or assigned to a void * elsewhere anyway.
79  *
80  * Example:
81  *      void _register_callback(void (*fn)(void *arg), void *arg);
82  *      #define register_callback(fn, arg) \
83  *              _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
84  */
85 #define typesafe_cb(rtype, fn, arg)                     \
86         cast_if_type(rtype (*)(void *), (fn), (fn)(arg), rtype)
87
88 /**
89  * typesafe_cb_const - cast a const 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  * If a callback function takes a single argument, this macro does appropriate
95  * casts to a function which takes a single const void * argument if the
96  * callback provided matches the @arg.
97  *
98  * It is assumed that @arg is of pointer type: usually @arg is passed
99  * or assigned to a void * elsewhere anyway.
100  *
101  * Example:
102  *      void _register_callback(void (*fn)(const void *arg), const void *arg);
103  *      #define register_callback(fn, arg) \
104  *              _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
105  */
106 #define typesafe_cb_const(rtype, fn, arg)                               \
107         sizeof((fn)((const void *)0)),                                  \
108                 cast_if_type(rtype (*)(const void *),                   \
109                              (fn), (fn)(arg), rtype (*)(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_type(rtype (*)(__VA_ARGS__, void *), (fn), (fn),        \
128                      rtype (*)(__VA_ARGS__, typeof(arg)))
129 /**
130  * typesafe_cb_postargs - cast a callback function if it matches the arg
131  * @rtype: the return type of the callback function
132  * @fn: the callback function to cast
133  * @arg: the (pointer) argument to hand to the callback function.
134  *
135  * This is a version of typesafe_cb() for callbacks that take other arguments
136  * after the @arg.
137  *
138  * Example:
139  *      void _register_callback(void (*fn)(void *arg, int), void *arg);
140  *      #define register_callback(fn, arg) \
141  *              _register_callback(typesafe_cb_postargs(void, (fn), (arg), int),\
142  *                                 (arg))
143  */
144 #define typesafe_cb_postargs(rtype, fn, arg, ...)                       \
145         cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn),        \
146                      rtype (*)(typeof(arg), __VA_ARGS__))
147 /**
148  * typesafe_cb_cmp - cast a compare function if it matches the arg
149  * @rtype: the return type of the callback function
150  * @fn: the callback function to cast
151  * @arg: the (pointer) argument(s) to hand to the compare function.
152  *
153  * If a callback function takes two matching-type arguments, this macro does
154  * appropriate casts to a function which takes two const void * arguments if
155  * the callback provided takes two a const pointers to @arg.
156  *
157  * It is assumed that @arg is of pointer type: usually @arg is passed
158  * or assigned to a void * elsewhere anyway.  Note also that the type
159  * arg points to must be defined.
160  *
161  * Example:
162  *      void _my_qsort(void *base, size_t nmemb, size_t size,
163  *                     int (*cmp)(const void *, const void *));
164  *      #define my_qsort(base, nmemb, cmpfn) \
165  *              _my_qsort((base), (nmemb), sizeof(*(base)), \
166  *                        typesafe_cb_cmp(int, (cmpfn), (base)), (arg))
167  */
168 #define typesafe_cb_cmp(rtype, cmpfn, arg)                              \
169         cast_if_type(rtype (*)(const void *, const void *), (cmpfn),    \
170                      rtype (*)(const typeof(*arg)*, const typeof(*arg)*))
171                      
172 #endif /* CCAN_CAST_IF_TYPE_H */