Skip to content

Commit 56349b4

Browse files
abhishekkumar2718gitster
authored andcommitted
oidmap: rework iterators to return typed pointer
87571c3 (hashmap: use *_entry APIs for iteration, 2019-10-06) modified hashmap_iter_next() to return a hashmap_entry pointer instead of void pointer. However, oidmap_iter_next() is unaware of the struct type containing oidmap_entry and explicilty returns a void pointer. Rework oidmap_iter_* to include struct type and return appropriate pointer. This allows for compile-time type checks. Signed-off-by: Abhishek Kumar <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 8ea7ed2 commit 56349b4

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

oidmap.h

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,21 @@ static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter
7676
hashmap_iter_init(&map->map, &iter->h_iter);
7777
}
7878

79-
static inline void *oidmap_iter_next(struct oidmap_iter *iter)
80-
{
81-
/* TODO: this API could be reworked to do compile-time type checks */
82-
return (void *)hashmap_iter_next(&iter->h_iter);
83-
}
79+
/*
80+
* Returns the next entry, or NULL if there are no more entries.
81+
*
82+
* The entry is of @type (e.g. "struct foo") and has a member of type struct
83+
* oidmap_entry.
84+
*/
85+
#define oidmap_iter_next(iter, type) \
86+
(type *) hashmap_iter_next(&(iter)->h_iter)
8487

85-
static inline void *oidmap_iter_first(struct oidmap *map,
86-
struct oidmap_iter *iter)
87-
{
88-
oidmap_iter_init(map, iter);
89-
/* TODO: this API could be reworked to do compile-time type checks */
90-
return (void *)oidmap_iter_next(iter);
91-
}
88+
/*
89+
* Returns the first entry in @map using @iter, where the entry is of @type
90+
* (e.g. "struct foo") and has a member of type struct oidmap_entry.
91+
*/
92+
#define oidmap_iter_first(map, iter, type) \
93+
({oidmap_iter_init(map, iter); \
94+
oidmap_iter_next(iter, type); })
9295

9396
#endif

t/helper/test-oidmap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ int cmd__oidmap(int argc, const char **argv)
9696

9797
struct oidmap_iter iter;
9898
oidmap_iter_init(&map, &iter);
99-
while ((entry = oidmap_iter_next(&iter)))
99+
while ((entry = oidmap_iter_next(&iter, struct test_entry)))
100100
printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
101101

102102
} else {

0 commit comments

Comments
 (0)