]> git.ozlabs.org Git - ccan/blobdiff - ccan/typesafe_cb/typesafe_cb.h
typesafe_cb: more support for typesafe compare functions.
[ccan] / ccan / typesafe_cb / typesafe_cb.h
index cc4ea8adbb9fba319e9ac0d5e45d13ca7dae6456..0eb57d9524b2771e7cf3b43ba089e91fbd8c5992 100644 (file)
@@ -38,7 +38,7 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
 
 /**
  * typesafe_cb - cast a callback function if it matches the arg
- * @rettype: the return type of the callback function
+ * @rtype: the return type of the callback function
  * @fn: the callback function to cast
  * @arg: the (pointer) argument to hand to the callback function.
  *
@@ -54,18 +54,40 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
  *     #define register_callback(fn, arg) \
  *             _register_callback(typesafe_cb(void, (fn), (arg)), (arg))
  */
-#define typesafe_cb(rettype, fn, arg)                                  \
+#define typesafe_cb(rtype, fn, arg)                                    \
        cast_if_type(cast_if_type(cast_if_type((fn),                    \
-                                              rettype (*)(const typeof(arg)), \
-                                              rettype (*)(void *)),    \
-                                 rettype (*)(volatile typeof(arg)),    \
-                                 rettype (*)(void *)),                 \
-                    rettype (*)(typeof(arg)),                          \
-                    rettype (*)(void *))
+                                              rtype (*)(const typeof(*arg)*), \
+                                              rtype (*)(void *)),      \
+                                 rtype (*)(volatile typeof(*arg) *),   \
+                                 rtype (*)(void *)),                   \
+                    rtype (*)(typeof(arg)),                            \
+                    rtype (*)(void *))
+
+/**
+ * typesafe_cb_const - cast a const callback function if it matches the 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.
+ *
+ * If a callback function takes a single argument, this macro does appropriate
+ * casts to a function which takes a single const void * argument if the
+ * callback provided matches the @arg.
+ *
+ * It is assumed that @arg is of pointer type: usually @arg is passed
+ * or assigned to a void * elsewhere anyway.
+ *
+ * Example:
+ *     void _register_callback(void (*fn)(const void *arg), const void *arg);
+ *     #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 *))
 
 /**
  * typesafe_cb_preargs - cast a callback function if it matches the arg
- * @rettype: the return type of the callback function
+ * @rtype: the return type of the callback function
  * @fn: the callback function to cast
  * @arg: the (pointer) argument to hand to the callback function.
  *
@@ -78,21 +100,21 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
  *             _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
  *                                (arg))
  */
-#define typesafe_cb_preargs(rettype, fn, arg, ...)                     \
+#define typesafe_cb_preargs(rtype, fn, arg, ...)                       \
        cast_if_type(cast_if_type(cast_if_type((fn),                    \
-                                              rettype (*)(__VA_ARGS__, \
-                                                          const typeof(arg)), \
-                                              rettype (*)(__VA_ARGS__, \
-                                                          void *)),    \
-                                 rettype (*)(__VA_ARGS__,              \
-                                             volatile typeof(arg)),    \
-                                 rettype (*)(__VA_ARGS__, void *)),    \
-                    rettype (*)(__VA_ARGS__, typeof(arg)),             \
-                    rettype (*)(__VA_ARGS__, void *))
+                                              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 *))
 
 /**
  * typesafe_cb_postargs - cast a callback function if it matches the arg
- * @rettype: the return type of the callback function
+ * @rtype: the return type of the callback function
  * @fn: the callback function to cast
  * @arg: the (pointer) argument to hand to the callback function.
  *
@@ -105,16 +127,40 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
  *             _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
  *                                (arg))
  */
-#define typesafe_cb_postargs(rettype, fn, arg, ...)                    \
+#define typesafe_cb_postargs(rtype, fn, arg, ...)                      \
        cast_if_type(cast_if_type(cast_if_type((fn),                    \
-                                              rettype (*)(const typeof(arg), \
-                                                          __VA_ARGS__), \
-                                              rettype (*)(void *,      \
-                                                          __VA_ARGS__)), \
-                                 rettype (*)(volatile typeof(arg),     \
-                                             __VA_ARGS__),             \
-                                 rettype (*)(void *, __VA_ARGS__)),    \
-                    rettype (*)(typeof(arg), __VA_ARGS__),             \
-                    rettype (*)(void *, __VA_ARGS__))
+                                              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__))
 
+/**
+ * typesafe_cb_cmp - cast a compare function if it matches the arg
+ * @rtype: the return type of the callback function
+ * @fn: the callback function to cast
+ * @arg: the (pointer) argument(s) to hand to the compare function.
+ *
+ * If a callback function takes two matching-type arguments, this macro does
+ * appropriate casts to a function which takes two const void * arguments if
+ * 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.
+ *
+ * Example:
+ *     void _my_qsort(void *base, size_t nmemb, size_t size,
+ *                    int (*cmp)(const void *, const void *));
+ *     #define my_qsort(base, nmemb, cmpfn) \
+ *             _my_qsort((base), (nmemb), sizeof(*(base)), \
+ *                       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 *))
 #endif /* CCAN_CAST_IF_TYPE_H */