summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRusty Russell <rusty@rustcorp.com.au>2011-12-01 16:41:51 +1030
committerRusty Russell <rusty@rustcorp.com.au>2011-12-01 16:41:51 +1030
commit140cd1699215bdb2a849459644823b88ab7d42cf (patch)
tree277d1cd78aa67d9dd7f241778bbf1c491dd1ebfe
parent932aeb6dcc4a6df2d08755f743659451c9721447 (diff)
strmap: allow const arguments to strset_iterate().
-rw-r--r--ccan/strmap/strmap.c8
-rw-r--r--ccan/strmap/strmap.h4
-rw-r--r--ccan/strmap/test/run-iterate-const.c32
3 files changed, 39 insertions, 5 deletions
diff --git a/ccan/strmap/strmap.c b/ccan/strmap/strmap.c
index 872ca544..7f21d193 100644
--- a/ccan/strmap/strmap.c
+++ b/ccan/strmap/strmap.c
@@ -174,17 +174,19 @@ char *strmap_del_(struct strmap *map, const char *member, void **valuep)
}
static bool iterate(struct strmap n,
- bool (*handle)(const char *, void *, void *), void *data)
+ bool (*handle)(const char *, void *, void *),
+ const void *data)
{
if (n.v)
- return handle(n.u.s, n.v, data);
+ return handle(n.u.s, n.v, (void *)data);
return iterate(n.u.n->child[0], handle, data)
|| iterate(n.u.n->child[1], handle, data);
}
void strmap_iterate_(const struct strmap *map,
- bool (*handle)(const char *, void *, void *), void *data)
+ bool (*handle)(const char *, void *, void *),
+ const void *data)
{
/* Empty map? */
if (!map->u.n)
diff --git a/ccan/strmap/strmap.h b/ccan/strmap/strmap.h
index d6104942..cf77e949 100644
--- a/ccan/strmap/strmap.h
+++ b/ccan/strmap/strmap.h
@@ -189,8 +189,8 @@ void strmap_clear_(struct strmap *map);
__typeof__(arg)), (handle)), \
(arg))
void strmap_iterate_(const struct strmap *map,
- bool (*handle)(const char *, void *, void *), void *data);
-
+ bool (*handle)(const char *, void *, void *),
+ const void *data);
/**
* strmap_prefix - return a submap matching a prefix
diff --git a/ccan/strmap/test/run-iterate-const.c b/ccan/strmap/test/run-iterate-const.c
new file mode 100644
index 00000000..07c814a3
--- /dev/null
+++ b/ccan/strmap/test/run-iterate-const.c
@@ -0,0 +1,32 @@
+#include <ccan/strmap/strmap.h>
+#include <ccan/strmap/strmap.c>
+#include <ccan/tap/tap.h>
+
+static bool found = false;
+
+/* Make sure const args work. */
+static bool find_string(const char *str, char *member, const char *cmp)
+{
+ if (strcmp(member, cmp) == 0)
+ found = true;
+ return false;
+}
+
+int main(void)
+{
+ struct strmap_charp {
+ STRMAP_MEMBERS(char *);
+ } map;
+
+ plan_tests(3);
+
+ strmap_init(&map);
+ ok1(strmap_add(&map, "hello", "hello"));
+ ok1(strmap_add(&map, "world", "world"));
+ strmap_iterate(&map, find_string, (const char *)"hello");
+ ok1(found);
+ strmap_clear(&map);
+
+ /* This exits depending on whether all tests passed */
+ return exit_status();
+}