1 #include <ccan/typesafe_cb/typesafe_cb.h>
3 #include <ccan/tap/tap.h>
7 /* The example usage. */
8 static void _set_some_value(void *val)
13 #define set_some_value(expr) \
14 _set_some_value(cast_if_type(void *, (expr), (expr), unsigned long))
16 static void _callback_onearg(void (*fn)(void *arg), void *arg)
21 static void _callback_preargs(void (*fn)(int a, int b, void *arg), void *arg)
26 static void _callback_postargs(void (*fn)(void *arg, int a, int b), void *arg)
31 #define callback_onearg(cb, arg) \
32 _callback_onearg(typesafe_cb(void, (cb), (arg)), (arg))
34 #define callback_preargs(cb, arg) \
35 _callback_preargs(typesafe_cb_preargs(void, (cb), (arg), int, int), (arg))
37 #define callback_postargs(cb, arg) \
38 _callback_postargs(typesafe_cb_postargs(void, (cb), (arg), int, int), (arg))
40 static void my_callback_onearg(char *p)
42 ok1(strcmp(p, "hello world") == 0);
45 static void my_callback_onearg_const(const char *p)
47 ok1(strcmp(p, "hello world") == 0);
50 static void my_callback_onearg_volatile(volatile char *p)
52 ok1(strcmp((char *)p, "hello world") == 0);
55 static void my_callback_preargs(int a, int b, char *p)
59 ok1(strcmp(p, "hello world") == 0);
63 static void my_callback_preargs_const(int a, int b, const char *p)
67 ok1(strcmp(p, "hello world") == 0);
70 static void my_callback_preargs_volatile(int a, int b, volatile char *p)
74 ok1(strcmp((char *)p, "hello world") == 0);
78 static void my_callback_postargs(char *p, int a, int b)
82 ok1(strcmp(p, "hello world") == 0);
86 static void my_callback_postargs_const(const char *p, int a, int b)
90 ok1(strcmp(p, "hello world") == 0);
93 static void my_callback_postargs_volatile(volatile char *p, int a, int b)
97 ok1(strcmp((char *)p, "hello world") == 0);
101 /* This is simply a compile test; we promised cast_if_type can be in a
102 * static initializer. */
103 struct callback_onearg
105 void (*fn)(void *arg);
109 struct callback_onearg cb_onearg
110 = { typesafe_cb(void, my_callback_onearg, "hello world"), "hello world" };
112 struct callback_preargs
114 void (*fn)(int a, int b, void *arg);
118 struct callback_preargs cb_preargs
119 = { typesafe_cb_preargs(void, my_callback_preargs, "hi", int, int), "hi" };
121 struct callback_postargs
123 void (*fn)(void *arg, int a, int b);
127 struct callback_postargs cb_postargs
128 = { typesafe_cb_postargs(void, my_callback_postargs, "hi", int, int), "hi" };
130 int main(int argc, char *argv[])
133 unsigned long l = (unsigned long)p;
135 plan_tests(2 + 3 + 3 + 3);
139 callback_onearg(my_callback_onearg, "hello world");
140 callback_onearg(my_callback_onearg_const, "hello world");
141 callback_onearg(my_callback_onearg_volatile, "hello world");
143 callback_preargs(my_callback_preargs, "hello world");
145 callback_preargs(my_callback_preargs_const, "hello world");
146 callback_preargs(my_callback_preargs_volatile, "hello world");
149 callback_postargs(my_callback_postargs, "hello world");
151 callback_postargs(my_callback_postargs_const, "hello world");
152 callback_postargs(my_callback_postargs_volatile, "hello world");
155 return exit_status();