1 /* Licensed under LGPLv2+ - see LICENSE file for details */
2 #ifndef CCAN_HTABLE_TYPE_H
3 #define CCAN_HTABLE_TYPE_H
4 #include <ccan/htable/htable.h>
8 * HTABLE_DEFINE_TYPE - create a set of htable ops for a type
9 * @type: a type whose pointers will be values in the hash.
10 * @keyof: a function/macro to extract a key from a @type element.
11 * @hashfn: a hash function for a @key
12 * @cmpfn: a comparison function for two keyof()s.
13 * @name: a name for all the functions to define (of form htable_<name>_*)
15 * NULL values may not be placed into the hash table.
17 * The following wrapper functions are defined; each one is a
18 * simplified version of the htable.h equivalent:
20 * // Creating and freeing.
21 * struct htable_@name *htable_@name_new(void);
22 * void htable_@name_free(const struct htable_@name *ht);
24 * // Add, delete and find.
25 * bool htable_@name_add(struct htable_@name *ht, const type *e);
26 * bool htable_@name_del(struct htable_@name *ht, const type *e);
27 * bool htable_@name_delkey(struct htable_@name *ht, const ktype *k);
28 * type *htable_@name_get(const struct htable_@name *ht, const ktype *k);
31 * struct htable_@name_iter;
32 * type *htable_@name_first(const struct htable_@name *ht,
33 * struct htable_@name_iter *i);
34 * type *htable_@name_next(const struct htable_@name *ht,
35 * struct htable_@name_iter *i);
37 #define HTABLE_DEFINE_TYPE(type, keyof, hashfn, cmpfn, name) \
38 struct htable_##name; \
39 struct htable_##name##_iter { struct htable_iter i; }; \
40 static inline size_t htable_##name##_hash(const void *elem, void *priv) \
42 return hashfn(keyof((const type *)elem)); \
44 static inline struct htable_##name *htable_##name##_new(void) \
46 return (struct htable_##name *)htable_new(htable_##name##_hash, \
49 static inline void htable_##name##_free(const struct htable_##name *ht) \
51 htable_free((const struct htable *)ht); \
53 static inline bool htable_##name##_add(struct htable_##name *ht, \
56 return htable_add((struct htable *)ht, hashfn(keyof(elem)), elem); \
58 static inline bool htable_##name##_del(const struct htable_##name *ht, \
61 return htable_del((struct htable *)ht, hashfn(keyof(elem)), elem); \
63 static inline type *htable_##name##_get(const struct htable_##name *ht, \
64 const HTABLE_KTYPE(keyof) k) \
66 /* Typecheck for cmpfn */ \
67 (void)sizeof(cmpfn((const type *)NULL, \
68 keyof((const type *)NULL))); \
69 return (type *)htable_get((const struct htable *)ht, \
71 (bool (*)(const void *, void *))(cmpfn), \
74 static inline bool htable_##name##_delkey(struct htable_##name *ht, \
75 const HTABLE_KTYPE(keyof) k) \
77 type *elem = htable_##name##_get(ht, k); \
79 return htable_##name##_del(ht, elem); \
82 static inline type *htable_##name##_first(const struct htable_##name *ht, \
83 struct htable_##name##_iter *iter) \
85 return htable_first((const struct htable *)ht, &iter->i); \
87 static inline type *htable_##name##_next(const struct htable_##name *ht, \
88 struct htable_##name##_iter *iter) \
90 return htable_next((const struct htable *)ht, &iter->i); \
94 #define HTABLE_KTYPE(keyof) typeof(keyof(NULL))
96 #define HTABLE_KTYPE(keyof) void *
98 #endif /* CCAN_HTABLE_TYPE_H */