X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftypesafe_cb%2Ftypesafe_cb.h;h=7347b7b2912c20291bfbdd9adfdcbb430334c07c;hp=0eb57d9524b2771e7cf3b43ba089e91fbd8c5992;hb=37e247dbcb57f4b2effd44b37b96dc679ee525d0;hpb=517114490e7153643b4c0a12c59a060568d7ae00 diff --git a/ccan/typesafe_cb/typesafe_cb.h b/ccan/typesafe_cb/typesafe_cb.h index 0eb57d95..7347b7b2 100644 --- a/ccan/typesafe_cb/typesafe_cb.h +++ b/ccan/typesafe_cb/typesafe_cb.h @@ -4,38 +4,67 @@ #if HAVE_TYPEOF && HAVE_BUILTIN_CHOOSE_EXPR && HAVE_BUILTIN_TYPES_COMPATIBLE_P /** - * cast_if_type - only cast an expression if it is of a given type + * cast_if_type - only cast an expression if test matches a given type + * @desttype: the type to cast to * @expr: the expression to cast + * @test: the expression to test * @oktype: the type we allow - * @desttype: the type to cast to * * 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. * * This is merely useful for warnings: if the compiler does not * support the primitives required for cast_if_type(), it becomes an - * unconditional cast, and the @oktype argument is not used. In + * unconditional cast, and the @test and @oktype argument is not used. In * particular, this means that @oktype can be a type which uses * the "typeof": it will not be evaluated if typeof is not supported. * * Example: * // We can take either an unsigned long or a void *. * void _set_some_value(void *val); - * #define set_some_value(expr) \ - * _set_some_value(cast_if_type((expr), unsigned long, void *)) + * #define set_some_value(e) \ + * _set_some_value(cast_if_type(void *, (e), (e), unsigned long)) */ -#define cast_if_type(expr, oktype, desttype) \ -__builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), \ +#define cast_if_type(desttype, expr, test, oktype) \ + __builtin_choose_expr(__builtin_types_compatible_p(typeof(test), oktype), \ (desttype)(expr), (expr)) #else -#define cast_if_type(expr, oktype, desttype) ((desttype)(expr)) +#define cast_if_type(desttype, expr, test, oktype) ((desttype)(expr)) #endif +/** + * cast_if_any - only cast an expression if it is one of the three given types + * @desttype: the type to cast to + * @expr: the expression to cast + * @test: the expression to test + * @ok1: the first type we allow + * @ok2: the second type we allow + * @ok3: the third type we allow + * + * This is a convenient wrapper for multiple cast_if_type() calls. You can + * chain them inside each other (ie. use cast_if_any() for expr) if you need + * more than 3 arguments. + * + * Example: + * // We can take either a long, unsigned long, void * or a const void *. + * void _set_some_value(void *val); + * #define set_some_value(expr) \ + * _set_some_value(cast_if_any(void *, (expr), (expr), \ + * long, unsigned long, const void *)) + */ +#define cast_if_any(desttype, expr, test, ok1, ok2, ok3) \ + cast_if_type(desttype, \ + cast_if_type(desttype, \ + cast_if_type(desttype, (expr), (test), ok1), \ + (test), \ + ok2), \ + (test), \ + ok3) + /** * typesafe_cb - cast a callback function if it matches the arg * @rtype: the return type of the callback function @@ -49,19 +78,56 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):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) \ * _register_callback(typesafe_cb(void, (fn), (arg)), (arg)) */ -#define typesafe_cb(rtype, fn, arg) \ - cast_if_type(cast_if_type(cast_if_type((fn), \ - rtype (*)(const typeof(*arg)*), \ - rtype (*)(void *)), \ - rtype (*)(volatile typeof(*arg) *), \ - rtype (*)(void *)), \ - rtype (*)(typeof(arg)), \ - rtype (*)(void *)) +#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 @@ -81,9 +147,8 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), * #define register_callback(fn, arg) \ * _register_callback(typesafe_cb_const(void, (fn), (arg)), (arg)) */ -#define typesafe_cb_const(rtype, fn, arg) \ - cast_if_type((fn), \ - rtype (*)(const typeof(*arg)*), rtype (*)(const void *)) +#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 @@ -101,16 +166,8 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), * (arg)) */ #define typesafe_cb_preargs(rtype, fn, arg, ...) \ - cast_if_type(cast_if_type(cast_if_type((fn), \ - rtype (*)(__VA_ARGS__, \ - const typeof(*arg) *),\ - rtype (*)(__VA_ARGS__, \ - void *)), \ - rtype (*)(__VA_ARGS__, \ - volatile typeof(*arg) *), \ - rtype (*)(__VA_ARGS__, void *)), \ - rtype (*)(__VA_ARGS__, typeof(arg)), \ - rtype (*)(__VA_ARGS__, void *)) + 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 @@ -124,20 +181,12 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), * Example: * void _register_callback(void (*fn)(void *arg, int), void *arg); * #define register_callback(fn, arg) \ - * _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\ + * _register_callback(typesafe_cb_postargs(void, (fn), (arg), int),\ * (arg)) */ #define typesafe_cb_postargs(rtype, fn, arg, ...) \ - cast_if_type(cast_if_type(cast_if_type((fn), \ - rtype (*)(const typeof(*arg) *, \ - __VA_ARGS__), \ - rtype (*)(void *, \ - __VA_ARGS__)), \ - rtype (*)(volatile typeof(*arg) *, \ - __VA_ARGS__), \ - rtype (*)(void *, __VA_ARGS__)), \ - rtype (*)(typeof(arg), __VA_ARGS__), \ - rtype (*)(void *, __VA_ARGS__)) + 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 @@ -150,7 +199,8 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), * the callback provided takes two a const pointers to @arg. * * It is assumed that @arg is of pointer type: usually @arg is passed - * or assigned to a void * elsewhere anyway. + * or assigned to a void * elsewhere anyway. Note also that the type + * arg points to must be defined. * * Example: * void _my_qsort(void *base, size_t nmemb, size_t size, @@ -160,7 +210,7 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype), * typesafe_cb_cmp(int, (cmpfn), (base)), (arg)) */ #define typesafe_cb_cmp(rtype, cmpfn, arg) \ - cast_if_type((cmpfn), \ - rtype (*)(const typeof(*arg)*, const typeof(*arg)*), \ - rtype (*)(const void *, const void *)) + cast_if_type(rtype (*)(const void *, const void *), (cmpfn), (cmpfn)+0, \ + rtype (*)(const typeof(*arg)*, const typeof(*arg)*)) + #endif /* CCAN_CAST_IF_TYPE_H */