X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftypesafe_cb%2Ftypesafe_cb.h;h=7347b7b2912c20291bfbdd9adfdcbb430334c07c;hp=f5e416de2de26c0e65605c5ed923ce0177cf4c33;hb=a39bf3aca1b60365efaf8b1eeb2b2c58b09ffab6;hpb=67a035869c6f3512ba943ae197b407f182a4506d diff --git a/ccan/typesafe_cb/typesafe_cb.h b/ccan/typesafe_cb/typesafe_cb.h index f5e416de..7347b7b2 100644 --- a/ccan/typesafe_cb/typesafe_cb.h +++ b/ccan/typesafe_cb/typesafe_cb.h @@ -12,9 +12,8 @@ * * This macro is used to create functions which allow multiple types. * The result of this macro is used somewhere that a @desttype type is - * expected: if @expr was of type @oktype, it will be cast to - * @desttype type. As a result, if @expr is any type other than - * @oktype or @desttype, a compiler warning will be issued. + * expected: if @test is exactly of type @oktype, then @expr will be + * cast to @desttype type, otherwise left alone. * * This macro can be used in static initializers. * @@ -31,7 +30,7 @@ * _set_some_value(cast_if_type(void *, (e), (e), unsigned long)) */ #define cast_if_type(desttype, expr, test, oktype) \ -__builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), \ + __builtin_choose_expr(__builtin_types_compatible_p(typeof(test), oktype), \ (desttype)(expr), (expr)) #else #define cast_if_type(desttype, expr, test, oktype) ((desttype)(expr)) @@ -61,7 +60,9 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), cast_if_type(desttype, \ cast_if_type(desttype, \ cast_if_type(desttype, (expr), (test), ok1), \ + (test), \ ok2), \ + (test), \ ok3) /** @@ -77,6 +78,9 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), * It is assumed that @arg is of pointer type: usually @arg is passed * or assigned to a void * elsewhere anyway. * + * This will not work with a NULL @fn argument: see typesafe_cb_def or + * typesafe_cb_exact. + * * Example: * void _register_callback(void (*fn)(void *arg), void *arg); * #define register_callback(fn, arg) \ @@ -85,6 +89,46 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), #define typesafe_cb(rtype, fn, arg) \ cast_if_type(rtype (*)(void *), (fn), (fn)(arg), rtype) +/** + * typesafe_cb_def - cast a callback fn if it matches arg (of defined type) + * @rtype: the return type of the callback function + * @fn: the callback function to cast + * @arg: the (pointer) argument to hand to the callback function. + * + * This is typesafe_cb(), except the type must be defined (eg. if it's + * struct foo *, the definition of struct foo must be visible). For many + * applications, this is reasonable. + * + * This variant can accept @fn equal to NULL. + * + * Example: + * void _register_callback(void (*fn)(void *arg), void *arg); + * #define register_callback(fn, arg) \ + * _register_callback(typesafe_cb_def(void, (fn), (arg)), (arg)) + */ +#define typesafe_cb_def(rtype, fn, arg) \ + cast_if_any(rtype (*)(void *), (fn), (fn)+0, \ + rtype (*)(typeof(*arg)*), \ + rtype (*)(const typeof(*arg)*), \ + rtype (*)(volatile typeof(*arg)*)) + +/** + * typesafe_cb_exact - cast a callback fn if it exactly matches arg + * @rtype: the return type of the callback function + * @fn: the callback function to cast + * @arg: the (pointer) argument to hand to the callback function. + * + * This is typesafe_cb(), except the @fn can be NULL, or must exactly match + * the @arg type (no const or volatile). + * + * Example: + * void _register_callback(void (*fn)(void *arg), void *arg); + * #define register_callback(fn, arg) \ + * _register_callback(typesafe_cb_exact(void, (fn), (arg)), (arg)) + */ +#define typesafe_cb_exact(rtype, fn, arg) \ + cast_if_type(rtype (*)(void *), (fn), (fn)+0, rtype (*)(typeof(arg))) + /** * typesafe_cb_const - cast a const callback function if it matches the arg * @rtype: the return type of the callback function @@ -103,10 +147,8 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), * #define register_callback(fn, arg) \ * _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg)) */ -#define typesafe_cb_const(rtype, fn, arg) \ - sizeof((fn)((const void *)0)), \ - cast_if_type(rtype (*)(const void *), \ - (fn), (fn)(arg), rtype (*)(typeof(arg))) +#define typesafe_cb_const(rtype, fn, arg) \ + cast_if_type(rtype (*)(const void *), (fn), (fn)(arg), rtype) /** * typesafe_cb_preargs - cast a callback function if it matches the arg @@ -124,8 +166,9 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), * (arg)) */ #define typesafe_cb_preargs(rtype, fn, arg, ...) \ - cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), (fn), \ + cast_if_type(rtype (*)(__VA_ARGS__, void *), (fn), (fn)+0, \ rtype (*)(__VA_ARGS__, typeof(arg))) + /** * typesafe_cb_postargs - cast a callback function if it matches the arg * @rtype: the return type of the callback function @@ -142,8 +185,9 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), * (arg)) */ #define typesafe_cb_postargs(rtype, fn, arg, ...) \ - cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn), \ + cast_if_type(rtype (*)(void *, __VA_ARGS__), (fn), (fn)+0, \ rtype (*)(typeof(arg), __VA_ARGS__)) + /** * typesafe_cb_cmp - cast a compare function if it matches the arg * @rtype: the return type of the callback function @@ -166,7 +210,7 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(test):0), oktype), * typesafe_cb_cmp(int, (cmpfn), (base)), (arg)) */ #define typesafe_cb_cmp(rtype, cmpfn, arg) \ - cast_if_type(rtype (*)(const void *, const void *), (cmpfn), \ + cast_if_type(rtype (*)(const void *, const void *), (cmpfn), (cmpfn)+0, \ rtype (*)(const typeof(*arg)*, const typeof(*arg)*)) #endif /* CCAN_CAST_IF_TYPE_H */