typesafe_cb: expose _exact and _def variants.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 15 Jun 2010 10:02:55 +0000 (19:32 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 15 Jun 2010 10:02:55 +0000 (19:32 +0930)
We can't allow NULL with the new variant (needed by talloc's set_destructor
for example), so document that and expose all three variants for different
uses.

ccan/typesafe_cb/test/compile_fail-typesafe_cb_exact.c [new file with mode: 0644]
ccan/typesafe_cb/test/compile_ok-typesafe_cb-NULL.c [new file with mode: 0644]
ccan/typesafe_cb/test/compile_ok-typesafe_cb-const.c
ccan/typesafe_cb/test/compile_ok-typesafe_cb_def-const.c [new file with mode: 0644]
ccan/typesafe_cb/typesafe_cb.h

diff --git a/ccan/typesafe_cb/test/compile_fail-typesafe_cb_exact.c b/ccan/typesafe_cb/test/compile_fail-typesafe_cb_exact.c
new file mode 100644 (file)
index 0000000..0f61d5d
--- /dev/null
@@ -0,0 +1,33 @@
+#include <ccan/typesafe_cb/typesafe_cb.h>
+#include <stdlib.h>
+
+static void _register_callback(void (*cb)(void *arg), const void *arg)
+{
+}
+
+#define register_callback(cb, arg)                             \
+       _register_callback(typesafe_cb_exact(void, (cb), (arg)), (arg))
+
+static void my_callback(const char *p)
+{
+}
+
+int main(int argc, char *argv[])
+{
+#ifdef FAIL
+       char *p;
+#if !HAVE_TYPEOF||!HAVE_BUILTIN_CHOOSE_EXPR||!HAVE_BUILTIN_TYPES_COMPATIBLE_P
+#error "Unfortunately we don't fail if cast_if_type is a noop."
+#endif
+#else
+       const char *p;
+#endif
+       p = NULL;
+
+       /* This should work always. */
+       register_callback(my_callback, (const char *)"hello world");
+
+       /* This will fail with FAIL defined */
+       register_callback(my_callback, p);
+       return 0;
+}
diff --git a/ccan/typesafe_cb/test/compile_ok-typesafe_cb-NULL.c b/ccan/typesafe_cb/test/compile_ok-typesafe_cb-NULL.c
new file mode 100644 (file)
index 0000000..e3dceb2
--- /dev/null
@@ -0,0 +1,21 @@
+#include <ccan/typesafe_cb/typesafe_cb.h>
+#include <stdlib.h>
+
+/* NULL args for callback function should be OK for _exact and _def. */
+
+static void _register_callback(void (*cb)(void *arg), void *arg)
+{
+}
+
+#define register_callback_def(cb, arg)                         \
+       _register_callback(typesafe_cb_def(void, (cb), (arg)), (arg))
+
+#define register_callback_exact(cb, arg)                               \
+       _register_callback(typesafe_cb_exact(void, (cb), (arg)), (arg))
+
+int main(int argc, char *argv[])
+{
+       register_callback_def(NULL, "hello world");
+       register_callback_exact(NULL, "hello world");
+       return 0;
+}
index 5017928a39e70aebd476e74e7dac0b45323bde6d..1e4a77b25399c44bcfc4e92abba5992611bcb1ee 100644 (file)
@@ -10,6 +10,9 @@ static void _register_callback(void (*cb)(void *arg), void *arg)
 #define register_callback(cb, arg)                             \
        _register_callback(typesafe_cb(void, (cb), (arg)), (arg))
 
+#define register_callback_def(cb, arg)                         \
+       _register_callback(typesafe_cb_def(void, (cb), (arg)), (arg))
+
 static void _register_callback_pre(void (*cb)(int x, void *arg), void *arg)
 {
 }
@@ -39,6 +42,7 @@ static void my_callback_post(/*const*/ char *p, int x)
 int main(int argc, char *argv[])
 {
        register_callback(my_callback, "hello world");
+       register_callback_def(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_def-const.c b/ccan/typesafe_cb/test/compile_ok-typesafe_cb_def-const.c
new file mode 100644 (file)
index 0000000..5017928
--- /dev/null
@@ -0,0 +1,45 @@
+#include <ccan/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;
+}
index 39b0312453f4a93b41c5f079468c83eecb7817ca..e48741a9d15962650e267a61e1a87ed7cc4ef21c 100644 (file)
@@ -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) \
 #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),    \
+                   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), rtype (*)(typeof(arg)))
+
 /**
  * typesafe_cb_const - cast a const callback function if it matches the arg
  * @rtype: the return type of the callback function