1 #include "typesafe_cb/typesafe_cb.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((expr), unsigned long, void *))
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);
62 static void my_callback_preargs_const(int a, int b, const char *p)
66 ok1(strcmp(p, "hello world") == 0);
69 static void my_callback_preargs_volatile(int a, int b, volatile char *p)
73 ok1(strcmp((char *)p, "hello world") == 0);
76 static void my_callback_postargs(char *p, int a, int b)
80 ok1(strcmp(p, "hello world") == 0);
83 static void my_callback_postargs_const(const char *p, int a, int b)
87 ok1(strcmp(p, "hello world") == 0);
90 static void my_callback_postargs_volatile(volatile char *p, int a, int b)
94 ok1(strcmp((char *)p, "hello world") == 0);
97 /* This is simply a compile test; we promised cast_if_type can be in a
98 * static initializer. */
99 struct callback_onearg
101 void (*fn)(void *arg);
105 struct callback_onearg cb_onearg
106 = { typesafe_cb(void, my_callback_onearg, "hello world"), "hello world" };
108 struct callback_preargs
110 void (*fn)(int a, int b, void *arg);
114 struct callback_preargs cb_preargs
115 = { typesafe_cb_preargs(void, my_callback_preargs, "hi", int, int), "hi" };
117 struct callback_postargs
119 void (*fn)(void *arg, int a, int b);
123 struct callback_postargs cb_postargs
124 = { typesafe_cb_postargs(void, my_callback_postargs, "hi", int, int), "hi" };
126 int main(int argc, char *argv[])
129 unsigned long l = (unsigned long)p;
131 plan_tests(2 + 3 + 9 + 9);
135 callback_onearg(my_callback_onearg, "hello world");
136 callback_onearg(my_callback_onearg_const, "hello world");
137 callback_onearg(my_callback_onearg_volatile, "hello world");
139 callback_preargs(my_callback_preargs, "hello world");
140 callback_preargs(my_callback_preargs_const, "hello world");
141 callback_preargs(my_callback_preargs_volatile, "hello world");
143 callback_postargs(my_callback_postargs, "hello world");
144 callback_postargs(my_callback_postargs_const, "hello world");
145 callback_postargs(my_callback_postargs_volatile, "hello world");
147 return exit_status();