]> git.ozlabs.org Git - ccan/blob - ccan/typesafe_cb/_info
typesafe_cb: fix up (and test!) cast_if_any.
[ccan] / ccan / typesafe_cb / _info
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * typesafe_cb - macros for safe callbacks.
7  *
8  * The basis of the typesafe_cb header is cast_if_type(): a
9  * conditional cast macro.   If an expression exactly matches a given
10  * type, it is cast to the target type, otherwise it is left alone.
11  *
12  * This allows us to create functions which take a small number of
13  * specific types, rather than being forced to use a void *.  In
14  * particular, it is useful for creating typesafe callbacks as the
15  * helpers typesafe_cb(), typesafe_cb_preargs() and
16  * typesafe_cb_postargs() demonstrate.
17  * 
18  * The standard way of passing arguments to callback functions in C is
19  * to use a void pointer, which the callback then casts back to the
20  * expected type.  This unfortunately subverts the type checking the
21  * compiler would perform if it were a direct call.  Here's an example:
22  *
23  *      static void my_callback(void *_obj)
24  *      {
25  *              struct obj *obj = _obj;
26  *              ...
27  *      }
28  *      ...
29  *              register_callback(my_callback, &my_obj);
30  *
31  * If we wanted to use the natural type for my_callback (ie. "void
32  * my_callback(struct obj *obj)"), we could make register_callback()
33  * take a void * as its first argument, but this would subvert all
34  * type checking.  We really want register_callback() to accept only
35  * the exactly correct function type to match the argument, or a
36  * function which takes a void *.
37  *
38  * This is where typesafe_cb() comes in: it uses cast_if_type() to
39  * cast the callback function if it matches the argument type:
40  *
41  *      void _register_callback(void (*cb)(void *arg), void *arg);
42  *      #define register_callback(cb, arg)                              \
43  *              _register_callback(typesafe_cb(void, (cb), (arg)), (arg))
44  *
45  * On compilers which don't support the extensions required
46  * cast_if_type() and friend become an unconditional cast, so your
47  * code will compile but you won't get type checking.
48  *
49  * Example:
50  *      #include <ccan/typesafe_cb/typesafe_cb.h>
51  *      #include <stdlib.h>
52  *      #include <stdio.h>
53  *
54  *      // Generic callback infrastructure.
55  *      struct callback {
56  *              struct callback *next;
57  *              int value;
58  *              int (*callback)(int value, void *arg);
59  *              void *arg;
60  *      };
61  *      static struct callback *callbacks;
62  *      
63  *      static void _register_callback(int value, int (*cb)(int, void *),
64  *                                     void *arg)
65  *      {
66  *              struct callback *new = malloc(sizeof(*new));
67  *              new->next = callbacks;
68  *              new->value = value;
69  *              new->callback = cb;
70  *              new->arg = arg;
71  *              callbacks = new;
72  *      }
73  *      #define register_callback(value, cb, arg)                       \
74  *              _register_callback(value,                               \
75  *                                 typesafe_cb_preargs(int, (cb), (arg), int),\
76  *                                 (arg))
77  *      
78  *      static struct callback *find_callback(int value)
79  *      {
80  *              struct callback *i;
81  *      
82  *              for (i = callbacks; i; i = i->next)
83  *                      if (i->value == value)
84  *                              return i;
85  *              return NULL;
86  *      }   
87  *
88  *      // Define several silly callbacks.  Note they don't use void *!
89  *      #define DEF_CALLBACK(name, op)                  \
90  *              static int name(int val, const int *arg)\
91  *              {                                       \
92  *                      printf("%s", #op);              \
93  *                      return val op *arg;             \
94  *              }
95  *      DEF_CALLBACK(multiply, *);
96  *      DEF_CALLBACK(add, +);
97  *      DEF_CALLBACK(divide, /);
98  *      DEF_CALLBACK(sub, -);
99  *      DEF_CALLBACK(or, |);
100  *      DEF_CALLBACK(and, &);
101  *      DEF_CALLBACK(xor, ^);
102  *      DEF_CALLBACK(assign, =);
103  *
104  *      // Silly game to find the longest chain of values.
105  *      int main(int argc, char *argv[])
106  *      {
107  *              int i, run = 1, num = argv[1] ? atoi(argv[1]) : 0;
108  *      
109  *              for (i = 1; i < 1024;) {
110  *                      // Since run is an int, compiler checks "add" does too.
111  *                      register_callback(i++, add, &run);
112  *                      register_callback(i++, divide, &run);
113  *                      register_callback(i++, sub, &run);
114  *                      register_callback(i++, multiply, &run);
115  *                      register_callback(i++, or, &run);
116  *                      register_callback(i++, and, &run);
117  *                      register_callback(i++, xor, &run);
118  *                      register_callback(i++, assign, &run);
119  *              }
120  *      
121  *              printf("%i ", num);
122  *              while (run < 56) {
123  *                      struct callback *cb = find_callback(num % i);
124  *                      if (!cb) {
125  *                              printf("-> STOP\n");
126  *                              return 1;
127  *                      }
128  *                      num = cb->callback(num, cb->arg);
129  *                      printf("->%i ", num);
130  *                      run++;
131  *              }
132  *              printf("-> Winner!\n");
133  *              return 0;
134  *      }
135  *
136  * Licence: LGPL (2 or any later version)
137  */
138 int main(int argc, char *argv[])
139 {
140         if (argc != 2)
141                 return 1;
142
143         if (strcmp(argv[1], "depends") == 0) {
144                 return 0;
145         }
146
147         return 1;
148 }