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 * @rettype: 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(rettype, fn, arg) \
58 cast_if_type(cast_if_type(cast_if_type((fn), \
59 rettype (*)(const typeof(arg)), \
60 rettype (*)(void *)), \
61 rettype (*)(volatile typeof(arg)), \
62 rettype (*)(void *)), \
63 rettype (*)(typeof(arg)), \
67 * typesafe_cb_preargs - cast a callback function if it matches the arg
68 * @rettype: 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 * This is a version of typesafe_cb() for callbacks that take other arguments
76 * void _register_callback(void (*fn)(int, void *arg), void *arg);
77 * #define register_callback(fn, arg) \
78 * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
81 #define typesafe_cb_preargs(rettype, fn, arg, ...) \
82 cast_if_type(cast_if_type(cast_if_type((fn), \
83 rettype (*)(__VA_ARGS__, \
85 rettype (*)(__VA_ARGS__, \
87 rettype (*)(__VA_ARGS__, \
88 volatile typeof(arg)), \
89 rettype (*)(__VA_ARGS__, void *)), \
90 rettype (*)(__VA_ARGS__, typeof(arg)), \
91 rettype (*)(__VA_ARGS__, void *))
94 * typesafe_cb_postargs - cast a callback function if it matches the arg
95 * @rettype: the return type of the callback function
96 * @fn: the callback function to cast
97 * @arg: the (pointer) argument to hand to the callback function.
99 * This is a version of typesafe_cb() for callbacks that take other arguments
103 * void _register_callback(void (*fn)(void *arg, int), void *arg);
104 * #define register_callback(fn, arg) \
105 * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
108 #define typesafe_cb_postargs(rettype, fn, arg, ...) \
109 cast_if_type(cast_if_type(cast_if_type((fn), \
110 rettype (*)(const typeof(arg), \
112 rettype (*)(void *, \
114 rettype (*)(volatile typeof(arg), \
116 rettype (*)(void *, __VA_ARGS__)), \
117 rettype (*)(typeof(arg), __VA_ARGS__), \
118 rettype (*)(void *, __VA_ARGS__))
120 #endif /* CCAN_CAST_IF_TYPE_H */