]> git.ozlabs.org Git - ccan/blob - ccan/container_of/_info
container_of: Make example compile cleanly with -Wunused
[ccan] / ccan / container_of / _info
1 #include "config.h"
2 #include <stdio.h>
3 #include <string.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  *      #include <stdio.h>
18  *      #include <ccan/container_of/container_of.h>
19  *
20  *      struct timer {
21  *              void *members;
22  *      };
23  *
24  *      struct info {
25  *              int my_stuff;
26  *              struct timer timer;
27  *      };
28  *
29  *      static void my_timer_callback(struct timer *timer)
30  *      {
31  *              struct info *info = container_of(timer, struct info, timer);
32  *              printf("my_stuff is %u\n", info->my_stuff);
33  *      }
34  *
35  *      static void register_timer(struct timer *timer)
36  *      {
37  *              (void)timer;
38  *              (void)my_timer_callback;
39  *              //...
40  *      }
41  *
42  *      int main(void)
43  *      {
44  *              struct info info = { .my_stuff = 1 };
45  *
46  *              register_timer(&info.timer);
47  *              // ...
48  *              return 0;
49  *      }
50  *
51  * License: CC0 (Public domain)
52  * Author: Rusty Russell <rusty@rustcorp.com.au>
53  */
54 int main(int argc, char *argv[])
55 {
56         if (argc != 2)
57                 return 1;
58
59         if (strcmp(argv[1], "depends") == 0) {
60                 printf("ccan/check_type\n");
61                 return 0;
62         }
63
64         return 1;
65 }