X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftypesafe_cb%2Ftypesafe_cb.h;h=7347b7b2912c20291bfbdd9adfdcbb430334c07c;hp=39b0312453f4a93b41c5f079468c83eecb7817ca;hb=2488a379c4c8f9b565cf7f3062015714ec704936;hpb=894dd68a8c899ca7b5028334804fa1638b583908 diff --git a/ccan/typesafe_cb/typesafe_cb.h b/ccan/typesafe_cb/typesafe_cb.h index 39b03124..7347b7b2 100644 --- a/ccan/typesafe_cb/typesafe_cb.h +++ b/ccan/typesafe_cb/typesafe_cb.h @@ -78,6 +78,9 @@ * 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) \ @@ -86,6 +89,46 @@ #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 @@ -123,7 +166,7 @@ * (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))) /** @@ -142,7 +185,7 @@ * (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__)) /** @@ -167,7 +210,7 @@ * typesafe_cb_cmp(int, (cmpfn), (base)), (arg)) */ #define typesafe_cb_cmp(rtype, cmpfn, arg) \ - cast_if_type(rtype (*)(const void *, const void *), (cmpfn), &*(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 */