]> git.ozlabs.org Git - ccan/blob - ccan/tcon/_info
talloc: remove const warning in _info example.
[ccan] / ccan / tcon / _info
1 #include "config.h"
2 #include <string.h>
3
4 /**
5  * tcon - routines for creating typesafe generic containers
6  *
7  * This code lets users create a structure with a typecanary; your API
8  * is then a set of macros which check the type canary before calling
9  * the generic routines.
10  *
11  * Example:
12  *      #include <ccan/tcon/tcon.h>
13  *      #include <stdio.h>
14  *
15  *      // A simple container class.  Can only contain one thing though!
16  *      struct container {
17  *              void *contents;
18  *      };
19  *      static inline void container_add_raw(struct container *c, void *p)
20  *      {
21  *              c->contents = p;
22  *      }
23  *      static inline void *container_get_raw(struct container *c)
24  *      {
25  *              return c->contents;
26  *      }
27  *
28  *      // This lets the user define their container type; includes a
29  *      // "type canary" to check types against.
30  *      #define DEFINE_TYPED_CONTAINER_STRUCT(name, type) \
31  *              struct name { struct container raw; TCON(type canary); }
32  *
33  *      // These macros make sure the container type and pointer match.
34  *      #define container_add(c, p) \
35  *              container_add_raw(&tcon_check((c), canary, (p))->raw, (p))
36  *      #define container_get(c) \
37  *              tcon_cast((c), canary, container_get_raw(&(c)->raw))
38  *
39  *      // Now, let's define two different containers.
40  *      DEFINE_TYPED_CONTAINER_STRUCT(int_container, int *);
41  *      DEFINE_TYPED_CONTAINER_STRUCT(string_container, char *);
42  *
43  *      int main(int argc, char *argv[])
44  *      {
45  *              struct int_container ic;
46  *              struct string_container sc;
47  *
48  *              // We would get a warning if we used the wrong types...
49  *              container_add(&ic, &argc);
50  *              container_add(&sc, argv[argc-1]);
51  *
52  *              printf("Last arg is %s of %i arguments\n",
53  *                     container_get(&sc), *container_get(&ic) - 1);
54  *              return 0;
55  *      }
56  *      // Given "foo" outputs "Last arg is foo of 1 arguments"
57  *      // Given "foo bar" outputs "Last arg is bar of 2 arguments"
58  *
59  * License: Public domain
60  *
61  * Author: Rusty Russell <rusty@rustcorp.com.au>
62  */
63 int main(int argc, char *argv[])
64 {
65         /* Expect exactly one argument */
66         if (argc != 2)
67                 return 1;
68
69         if (strcmp(argv[1], "depends") == 0) {
70                 return 0;
71         }
72
73         return 1;
74 }