Commit a4478395 authored by Rusty Russell's avatar Rusty Russell

hashtable: fix traverse typesafety.

parent 175ca4aa
...@@ -73,17 +73,18 @@ bool hashtable_del(struct hashtable *ht, unsigned long hash, const void *p); ...@@ -73,17 +73,18 @@ bool hashtable_del(struct hashtable *ht, unsigned long hash, const void *p);
*/ */
#define hashtable_traverse(ht, type, cb, cbarg) \ #define hashtable_traverse(ht, type, cb, cbarg) \
_hashtable_traverse(ht, cast_if_type(bool (*)(void *, void *), \ _hashtable_traverse(ht, cast_if_type(bool (*)(void *, void *), \
cast_if_any(bool (*)(void *, \ cast_if_any(bool (*)(void *, \
void *), (cb), \ void *), \
bool (*)(const type *, \ (cb), (cb), \
const typeof(*cbarg) *), \ bool (*)(const type *, \
bool (*)(type *, \ const typeof(*cbarg) *), \
const typeof(*cbarg) *), \ bool (*)(type *, \
bool (*)(const type *, \ const typeof(*cbarg) *), \
typeof(*cbarg) *)), \ bool (*)(const type *, \
bool (*)(type *, \ typeof(*cbarg) *)), \
typeof(*cbarg) *)), \ (cb), \
cbarg) bool (*)(type *, typeof(*cbarg) *)), \
(cbarg))
void _hashtable_traverse(struct hashtable *ht, void _hashtable_traverse(struct hashtable *ht,
bool (*cb)(void *p, void *cbarg), void *cbarg); bool (*cb)(void *p, void *cbarg), void *cbarg);
......
#include <ccan/hashtable/hashtable.h>
#include <ccan/hashtable/hashtable.c>
#include <stdlib.h>
struct foo {
int i;
};
struct bar {
int i;
};
static bool fn_bar_bar(
#ifdef FAIL
struct bar *
#else
struct foo *
#endif
foo,
struct bar *bar)
{
return true;
}
int main(void)
{
struct hashtable *ht = NULL;
struct bar *bar = NULL;
hashtable_traverse(ht, struct foo, fn_bar_bar, bar);
return 0;
}
#include <ccan/hashtable/hashtable.h>
#include <ccan/hashtable/hashtable.c>
#include <stdlib.h>
struct foo {
int i;
};
struct bar {
int i;
};
static bool fn_foo_foo(struct foo *foo,
#ifdef FAIL
struct foo *
#else
struct bar *
#endif
bar)
{
return true;
}
int main(void)
{
struct hashtable *ht = NULL;
struct bar *bar = NULL;
hashtable_traverse(ht, struct foo, fn_foo_foo, bar);
return 0;
}
#include <ccan/hashtable/hashtable.h>
#include <ccan/hashtable/hashtable.c>
struct foo {
int i;
};
struct bar {
int i;
};
static bool fn_foo_bar(struct foo *foo, struct bar *bar)
{
return true;
}
static bool fn_const_foo_bar(const struct foo *foo, struct bar *bar)
{
return true;
}
static bool fn_foo_const_bar(struct foo *foo, const struct bar *bar)
{
return true;
}
static bool fn_const_foo_const_bar(const struct foo *foo,
const struct bar *bar)
{
return true;
}
static bool fn_void_void(void *foo, void *bar)
{
return true;
}
int main(void)
{
struct hashtable *ht = NULL;
struct bar *bar = NULL;
hashtable_traverse(ht, struct foo, fn_foo_bar, bar);
hashtable_traverse(ht, struct foo, fn_const_foo_bar, bar);
hashtable_traverse(ht, struct foo, fn_foo_const_bar, bar);
hashtable_traverse(ht, struct foo, fn_const_foo_const_bar, bar);
hashtable_traverse(ht, struct foo, fn_void_void, bar);
return 0;
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment