]> git.ozlabs.org Git - ccan/commitdiff
make const and volatile-qualified callbacks actually work.
authorRusty Russell <rusty@rustcorp.com.au>
Sat, 10 Jan 2009 22:51:03 +0000 (09:21 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Sat, 10 Jan 2009 22:51:03 +0000 (09:21 +1030)
ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c [new file with mode: 0644]
ccan/typesafe_cb/test/compile_ok-typesafe_cb-volatile.c [new file with mode: 0644]
ccan/typesafe_cb/typesafe_cb.h

diff --git a/ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c b/ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c
new file mode 100644 (file)
index 0000000..fe3a341
--- /dev/null
@@ -0,0 +1,45 @@
+#include "typesafe_cb/typesafe_cb.h"
+#include <stdlib.h>
+
+/* const args in callbacks should be OK. */
+
+static void _register_callback(void (*cb)(void *arg), void *arg)
+{
+}
+
+#define register_callback(cb, arg)                             \
+       _register_callback(typesafe_cb(void, (cb), (arg)), (arg))
+
+static void _register_callback_pre(void (*cb)(int x, void *arg), void *arg)
+{
+}
+
+#define register_callback_pre(cb, arg)                                 \
+       _register_callback_pre(typesafe_cb_preargs(void, (cb), (arg), int), (arg))
+
+static void _register_callback_post(void (*cb)(void *arg, int x), void *arg)
+{
+}
+
+#define register_callback_post(cb, arg)                                        \
+       _register_callback_post(typesafe_cb_postargs(void, (cb), (arg), int), (arg))
+
+static void my_callback(const char *p)
+{
+}
+
+static void my_callback_pre(int x, const char *p)
+{
+}
+
+static void my_callback_post(const char *p, int x)
+{
+}
+
+int main(int argc, char *argv[])
+{
+       register_callback(my_callback, "hello world");
+       register_callback_pre(my_callback_pre, "hello world");
+       register_callback_post(my_callback_post, "hello world");
+       return 0;
+}
diff --git a/ccan/typesafe_cb/test/compile_ok-typesafe_cb-volatile.c b/ccan/typesafe_cb/test/compile_ok-typesafe_cb-volatile.c
new file mode 100644 (file)
index 0000000..93875c9
--- /dev/null
@@ -0,0 +1,45 @@
+#include "typesafe_cb/typesafe_cb.h"
+#include <stdlib.h>
+
+/* volatile args in callbacks should be OK. */
+
+static void _register_callback(void (*cb)(void *arg), void *arg)
+{
+}
+
+#define register_callback(cb, arg)                             \
+       _register_callback(typesafe_cb(void, (cb), (arg)), (arg))
+
+static void _register_callback_pre(void (*cb)(int x, void *arg), void *arg)
+{
+}
+
+#define register_callback_pre(cb, arg)                                 \
+       _register_callback_pre(typesafe_cb_preargs(void, (cb), (arg), int), (arg))
+
+static void _register_callback_post(void (*cb)(void *arg, int x), void *arg)
+{
+}
+
+#define register_callback_post(cb, arg)                                        \
+       _register_callback_post(typesafe_cb_postargs(void, (cb), (arg), int), (arg))
+
+static void my_callback(volatile char *p)
+{
+}
+
+static void my_callback_pre(int x, volatile char *p)
+{
+}
+
+static void my_callback_post(volatile char *p, int x)
+{
+}
+
+int main(int argc, char *argv[])
+{
+       register_callback(my_callback, "hello world");
+       register_callback_pre(my_callback_pre, "hello world");
+       register_callback_post(my_callback_post, "hello world");
+       return 0;
+}
index cc4ea8adbb9fba319e9ac0d5e45d13ca7dae6456..594e894ad33e1b649f9fa3b184c81b9146b89d02 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
 
 /**
  * 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.
  *
  * @fn: the callback function to cast
  * @arg: the (pointer) argument to hand to the callback function.
  *
@@ -54,18 +54,18 @@ __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 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),                    \
        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_preargs - cast a callback function if it matches the arg
 
 /**
  * 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.
  *
  * @fn: the callback function to cast
  * @arg: the (pointer) argument to hand to the callback function.
  *
@@ -78,21 +78,21 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
  *             _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
  *                                (arg))
  */
  *             _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),                    \
        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
 
 /**
  * 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.
  *
  * @fn: the callback function to cast
  * @arg: the (pointer) argument to hand to the callback function.
  *
@@ -105,16 +105,16 @@ __builtin_choose_expr(__builtin_types_compatible_p(typeof(1?(expr):0), oktype),
  *             _register_callback(typesafe_cb_preargs(void, (fn), (arg), int),\
  *                                (arg))
  */
  *             _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),                    \
        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__))
 
 #endif /* CCAN_CAST_IF_TYPE_H */
 
 #endif /* CCAN_CAST_IF_TYPE_H */