s3:net_idmap_delete do not lock two records at the same time
[Samba/gebeck_regimport.git] / ctdb / include / ctdb_typesafe_cb.h
blobb1f2c5f5b5e95f11f91a080da485f726575ecf93
1 #ifndef CCAN_CAST_IF_TYPE_H
2 #define CCAN_CAST_IF_TYPE_H
4 #if (__GNUC__ >= 3)
5 #define HAVE_TYPEOF 1
6 #define HAVE_BUILTIN_CHOOSE_EXPR 1
7 #define HAVE_BUILTIN_TYPES_COMPATIBLE_P 1
8 #endif
10 #if HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P
11 /**
12 * cast_if_type - only cast an expression if test matches a given type
13 * @desttype: the type to cast to
14 * @expr: the expression to cast
15 * @test: the expression to test
16 * @oktype: the type we allow
18 * This macro is used to create functions which allow multiple types.
19 * The result of this macro is used somewhere that a @desttype type is
20 * expected: if @expr was of type @oktype, it will be cast to
21 * @desttype type. As a result, if @expr is any type other than
22 * @oktype or @desttype, a compiler warning will be issued.
24 * This macro can be used in static initializers.
26 * This is merely useful for warnings: if the compiler does not
27 * support the primitives required for cast_if_type(), it becomes an
28 * unconditional cast, and the @test and @oktype argument is not used. In
29 * particular, this means that @oktype can be a type which uses
30 * the "typeof": it will not be evaluated if typeof is not supported.
32 * Example:
33 * // We can take either an unsigned long or a void *.
34 * void _set_some_value(void *val);
35 * #define set_some_value(e) \
36 * _set_some_value(cast_if_type(void *, (e), (e), unsigned long))
38 #define cast_if_type(desttype, expr, test, oktype) \
39 __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), \
40 (desttype)(expr), (expr))
41 #else
42 #define cast_if_type(desttype, expr, test, oktype) ((desttype)(expr))
43 #endif
45 /**
46 * cast_if_any - only cast an expression if it is one of the three given types
47 * @desttype: the type to cast to
48 * @expr: the expression to cast
49 * @test: the expression to test
50 * @ok1: the first type we allow
51 * @ok2: the second type we allow
52 * @ok3: the third type we allow
54 * This is a convenient wrapper for multiple cast_if_type() calls. You can
55 * chain them inside each other (ie. use cast_if_any() for expr) if you need
56 * more than 3 arguments.
58 * Example:
59 * // We can take either a long, unsigned long, void * or a const void *.
60 * void _set_some_value(void *val);
61 * #define set_some_value(expr) \
62 * _set_some_value(cast_if_any(void *, (expr), (expr), \
63 * long, unsigned long, const void *))
65 #define cast_if_any(desttype, expr, test, ok1, ok2, ok3) \
66 cast_if_type(desttype, \
67 cast_if_type(desttype, \
68 cast_if_type(desttype, (expr), (test), ok1), \
69 ok2), \
70 ok3)
72 /**
73 * typesafe_cb - cast a callback function if it matches the arg
74 * @rtype: the return type of the callback function
75 * @fn: the callback function to cast
76 * @arg: the (pointer) argument to hand to the callback function.
78 * If a callback function takes a single argument, this macro does
79 * appropriate casts to a function which takes a single void * argument if the
80 * callback provided matches the @arg (or a const or volatile version).
82 * It is assumed that @arg is of pointer type: usually @arg is passed
83 * or assigned to a void * elsewhere anyway.
85 * Example:
86 * void _register_callback(void (*fn)(void *arg), void *arg);
87 * #define register_callback(fn, arg) \
88 * _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
90 #define typesafe_cb(rtype, fn, arg) \
91 cast_if_type(rtype (*)(void *), (fn), (fn)(arg), rtype)
93 /**
94 * typesafe_cb_const - cast a const callback function if it matches the arg
95 * @rtype: 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 * If a callback function takes a single argument, this macro does appropriate
100 * casts to a function which takes a single const void * argument if the
101 * callback provided matches the @arg.
103 * It is assumed that @arg is of pointer type: usually @arg is passed
104 * or assigned to a void * elsewhere anyway.
106 * Example:
107 * void _register_callback(void (*fn)(const void *arg), const void *arg);
108 * #define register_callback(fn, arg) \
109 * _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg))
111 #define typesafe_cb_const(rtype, fn, arg) \
112 sizeof((fn)((const void *)0)), \
113 cast_if_type(rtype (*)(const void *), \
114 (fn), (fn)(arg), rtype (*)(typeof(arg)))
117 * typesafe_cb_preargs - cast a callback function if it matches the arg
118 * @rtype: the return type of the callback function
119 * @fn: the callback function to cast
120 * @arg: the (pointer) argument to hand to the callback function.
122 * This is a version of typesafe_cb() for callbacks that take other arguments
123 * before the @arg.
125 * Example:
126 * void _register_callback(void (*fn)(int, void *arg), void *arg);
127 * #define register_callback(fn, arg) \
128 * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
129 * (arg))
131 #define typesafe_cb_preargs(rtype, fn, arg, ...) \
132 cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), (fn), \
133 rtype (*)(__VA_ARGS__, typeof(arg)))
135 * typesafe_cb_postargs - cast a callback function if it matches the arg
136 * @rtype: the return type of the callback function
137 * @fn: the callback function to cast
138 * @arg: the (pointer) argument to hand to the callback function.
140 * This is a version of typesafe_cb() for callbacks that take other arguments
141 * after the @arg.
143 * Example:
144 * void _register_callback(void (*fn)(void *arg, int), void *arg);
145 * #define register_callback(fn, arg) \
146 * _register_callback(typesafe_cb_postargs(void, (fn), (arg), int),\
147 * (arg))
149 #define typesafe_cb_postargs(rtype, fn, arg, ...) \
150 cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn), \
151 rtype (*)(typeof(arg), __VA_ARGS__))
153 * typesafe_cb_cmp - cast a compare function if it matches the arg
154 * @rtype: the return type of the callback function
155 * @fn: the callback function to cast
156 * @arg: the (pointer) argument(s) to hand to the compare function.
158 * If a callback function takes two matching-type arguments, this macro does
159 * appropriate casts to a function which takes two const void * arguments if
160 * the callback provided takes two a const pointers to @arg.
162 * It is assumed that @arg is of pointer type: usually @arg is passed
163 * or assigned to a void * elsewhere anyway. Note also that the type
164 * arg points to must be defined.
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))
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)*))
177 #endif /* CCAN_CAST_IF_TYPE_H */