Norman James | 6a58a27 | 2015-10-07 14:34:16 -0500 | [diff] [blame] | 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 | * #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 register_timer(struct timer *timer) |
| 30 | * { |
| 31 | * //... |
| 32 | * } |
| 33 | * |
| 34 | * static void my_timer_callback(struct timer *timer) |
| 35 | * { |
| 36 | * struct info *info = container_of(timer, struct info, timer); |
| 37 | * printf("my_stuff is %u\n", info->my_stuff); |
| 38 | * } |
| 39 | * |
| 40 | * int main(void) |
| 41 | * { |
| 42 | * struct info info = { .my_stuff = 1 }; |
| 43 | * |
| 44 | * register_timer(&info.timer); |
| 45 | * // ... |
| 46 | * return 0; |
| 47 | * } |
| 48 | * |
| 49 | * License: CC0 (Public domain) |
| 50 | * Author: Rusty Russell <rusty@rustcorp.com.au> |
| 51 | */ |
| 52 | int main(int argc, char *argv[]) |
| 53 | { |
| 54 | if (argc != 2) |
| 55 | return 1; |
| 56 | |
| 57 | if (strcmp(argv[1], "depends") == 0) { |
| 58 | printf("ccan/check_type\n"); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | return 1; |
| 63 | } |