]> git.ozlabs.org Git - ccan/blob - ccan/container_of/_info.c
Moving grad_fd to string.c
[ccan] / ccan / container_of / _info.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "config.h"
4
5 /**
6  * container_of - routine for upcasting
7  *
8  * It is often convenient to create code where the caller registers a pointer
9  * to a generic structure and a callback.  The callback might know that the
10  * pointer points to within a larger structure, and container_of gives a
11  * convenient and fairly type-safe way of returning to the enclosing structure.
12  *
13  * This idiom is an alternative to providing a void * pointer for every
14  * callback.
15  *
16  * Example:
17  *      struct info
18  *      {
19  *              int my_stuff;
20  *              struct timer timer;
21  *      };
22  *
23  *      static void my_timer_callback(struct timer *timer)
24  *      {
25  *              struct info *info = container_of(timer, struct info, timer);
26  *              printf("my_stuff is %u\n", info->my_stuff);
27  *      }
28  *
29  *      int main()
30  *      {
31  *              struct info info = { .my_stuff = 1 };
32  *
33  *              register_timer(&info.timer);
34  *              ...
35  */
36 int main(int argc, char *argv[])
37 {
38         if (argc != 2)
39                 return 1;
40
41         if (strcmp(argv[1], "depends") == 0) {
42                 printf("ccan/check_type\n");
43                 return 0;
44         }
45
46         return 1;
47 }