4 #include <ccan/tcon/tcon.h>
5 #include <ccan/typesafe_cb/typesafe_cb.h>
10 * struct strmap - representation of a string map
12 * It's exposed here to allow you to embed it and so we can inline the
24 * STRMAP - declare a type-specific strmap
25 * @type: type for this map's values, or void * for any pointer.
27 * You use this to create your own typed strmap for a particular type.
28 * You can use an integer type, *but* remember you can't use "0" as a
32 * STRMAP(int *) int_strmap;
33 * strmap_init(&int_strmap);
35 #define STRMAP(type) \
36 TCON_WRAP(struct strmap, type canary)
39 * strmap_init - initialize a string map (empty)
40 * @map: the typed strmap to initialize.
42 * For completeness; if you've arranged for it to be NULL already you don't
50 #define strmap_init(map) strmap_init_(tcon_unwrap(map))
52 static inline void strmap_init_(struct strmap *map)
58 * strmap_empty - is this string map empty?
59 * @map: the typed strmap to check.
62 * if (!strmap_empty(&map))
65 #define strmap_empty(map) strmap_empty_(tcon_unwrap(map))
67 static inline bool strmap_empty_(const struct strmap *map)
69 return map->u.n == NULL;
73 * strmap_get - get a value from a string map
74 * @map: the typed strmap to search.
75 * @member: the string to search for.
77 * Returns the value, or NULL if it isn't in the map (and sets errno = ENOENT).
80 * int *val = strmap_get(&map, "hello");
82 * printf("hello => %i\n", *val);
84 #define strmap_get(map, member) \
85 tcon_cast((map), canary, strmap_get_(tcon_unwrap(map), (member)))
86 void *strmap_get_(const struct strmap *map, const char *member);
89 * strmap_add - place a member in the string map.
90 * @map: the typed strmap to add to.
91 * @member: the string to place in the map.
92 * @v: the (non-NULL) value.
94 * This returns false if we run out of memory (errno = ENOMEM), or
95 * (more normally) if that string already appears in the map (EEXIST).
97 * Note that the pointer is placed in the map, the string is not copied. If
98 * you want a copy in the map, use strdup(). Similarly for the value.
101 * val = malloc(sizeof *val);
103 * if (!strmap_add(&map, "goodbye", val))
104 * printf("goodbye was already in the map\n");
106 #define strmap_add(map, member, value) \
107 strmap_add_(tcon_unwrap(tcon_check((map), canary, (value))), \
108 (member), (void *)(value))
110 bool strmap_add_(struct strmap *map, const char *member, const void *value);
113 * strmap_del - remove a member from the string map.
114 * @map: the typed strmap to delete from.
115 * @member: the string to remove from the map.
116 * @valuep: the value (if non-NULL)
118 * This returns the string which was passed to strmap_map(), or NULL if
119 * it was not in the map (and sets errno = ENOENT).
121 * This means that if you allocated a string (eg. using strdup()), you
122 * can free it here. Similarly, the value is returned in @valuep if
123 * @valuep is not NULL.
126 * if (!strmap_del(&map, "goodbye", NULL))
127 * printf("goodbye was not in the map?\n");
129 #define strmap_del(map, member, valuep) \
130 strmap_del_(tcon_unwrap(tcon_check_ptr((map), canary, valuep)), \
131 (member), (void **)valuep)
132 char *strmap_del_(struct strmap *map, const char *member, void **valuep);
135 * strmap_clear - remove every member from the map.
136 * @map: the typed strmap to clear.
138 * The map will be empty after this.
141 * strmap_clear(&map);
143 #define strmap_clear(map) strmap_clear_(tcon_unwrap(map))
145 void strmap_clear_(struct strmap *map);
148 * strmap_iterate - ordered iteration over a map
149 * @map: the typed strmap to iterate through.
150 * @handle: the function to call.
151 * @arg: the argument for the function (types should match).
153 * @handle's prototype should be:
154 * bool @handle(const char *member, type value, typeof(arg) arg)
156 * If @handle returns false, the iteration will stop.
157 * You should not alter the map within the @handle function!
160 * typedef STRMAP(int *) strmap_intp;
161 * static bool dump_some(const char *member, int *value, int *num)
163 * // Only dump out num nodes.
166 * printf("%s=>%i\n", member, *value);
170 * static void dump_map(const strmap_intp *map)
173 * strmap_iterate(map, dump_some, &max);
175 * printf("... (truncated to 100 entries)\n");
178 #define strmap_iterate(map, handle, arg) \
179 strmap_iterate_(tcon_unwrap(map), \
180 typesafe_cb_cast(bool (*)(const char *, \
182 bool (*)(const char *, \
183 tcon_type((map), canary), \
184 __typeof__(arg)), (handle)), \
186 void strmap_iterate_(const struct strmap *map,
187 bool (*handle)(const char *, void *, void *),
191 * strmap_prefix - return a submap matching a prefix
193 * @prefix: the prefix.
195 * This returns a pointer into @map, so don't alter @map while using
196 * the return value. You can use strmap_iterate(), strmap_get() or
197 * strmap_empty() on the returned pointer.
200 * static void dump_prefix(const strmap_intp *map,
201 * const char *prefix)
204 * printf("Nodes with prefix %s:\n", prefix);
205 * strmap_iterate(strmap_prefix(map, prefix), dump_some, &max);
207 * printf("... (truncated to 100 entries)\n");
211 #define strmap_prefix(map, prefix) \
212 ((const __typeof__(map))strmap_prefix_(tcon_unwrap(map), (prefix)))
214 #define strmap_prefix(map, prefix) \
215 ((const void *)strmap_prefix_(tcon_unwrap(map), (prefix)))
218 const struct strmap *strmap_prefix_(const struct strmap *map,
221 #endif /* CCAN_STRMAP_H */