From: Abhishek Kumar <abhishekkumar8222@gmail.com>
To: git@vger.kernel.org
Subject: [PATCH 2/2] oidmap: rework iterators to return typed pointer
Date: Wed, 8 Apr 2020 09:36:59 +0530 [thread overview]
Message-ID: <20200408040659.14511-2-abhishekkumar8222@gmail.com> (raw)
In-Reply-To: <20200408040659.14511-1-abhishekkumar8222@gmail.com>
87571c3 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 <abhishekkumar8222@gmail.com>
---
oidmap.h | 27 +++++++++++++++------------
t/helper/test-oidmap.c | 2 +-
2 files changed, 16 insertions(+), 13 deletions(-)
diff --git a/oidmap.h b/oidmap.h
index c66a83ab1d..5d6b34a7ce 100644
--- a/oidmap.h
+++ b/oidmap.h
@@ -76,18 +76,21 @@ static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter
hashmap_iter_init(&map->map, &iter->h_iter);
}
-static inline void *oidmap_iter_next(struct oidmap_iter *iter)
-{
- /* TODO: this API could be reworked to do compile-time type checks */
- return (void *)hashmap_iter_next(&iter->h_iter);
-}
+/*
+ * Returns the next entry, or NULL if there are no more entries.
+ *
+ * The entry is of @type (e.g. "struct foo") and has a member of type struct
+ * oidmap_entry.
+ */
+#define oidmap_iter_next(iter, type) \
+ (type *) hashmap_iter_next(&(iter)->h_iter)
-static inline void *oidmap_iter_first(struct oidmap *map,
- struct oidmap_iter *iter)
-{
- oidmap_iter_init(map, iter);
- /* TODO: this API could be reworked to do compile-time type checks */
- return (void *)oidmap_iter_next(iter);
-}
+/*
+ * Returns the first entry in @map using @iter, where the entry is of @type
+ * (e.g. "struct foo") and has a member of type struct oidmap_entry.
+ */
+#define oidmap_iter_first(map, iter, type) \
+ ({oidmap_iter_init(map, iter); \
+ oidmap_iter_next(iter, type); })
#endif
diff --git a/t/helper/test-oidmap.c b/t/helper/test-oidmap.c
index 0acf99931e..a28bf007a8 100644
--- a/t/helper/test-oidmap.c
+++ b/t/helper/test-oidmap.c
@@ -96,7 +96,7 @@ int cmd__oidmap(int argc, const char **argv)
struct oidmap_iter iter;
oidmap_iter_init(&map, &iter);
- while ((entry = oidmap_iter_next(&iter)))
+ while ((entry = oidmap_iter_next(&iter, struct test_entry)))
printf("%s %s\n", oid_to_hex(&entry->entry.oid), entry->name);
} else {
--
2.26.0
next prev parent reply other threads:[~2020-04-08 4:08 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-04-08 4:06 [PATCH 1/2] oidmap: make oidmap_free independent of struct layout Abhishek Kumar
2020-04-08 4:06 ` Abhishek Kumar [this message]
2020-04-08 6:02 ` Junio C Hamano
2020-04-08 7:03 ` [PATCH v2 " Abhishek Kumar
2020-04-08 7:03 ` [PATCH v2 2/2] oidmap: rework iterators to return typed pointer Abhishek Kumar
2020-04-08 22:08 ` Jeff King
2020-04-08 21:54 ` [PATCH 1/2] oidmap: make oidmap_free independent of struct layout Jeff King
2020-04-25 2:59 ` [PATCH v3 1/3] " Abhishek Kumar
2020-04-25 2:59 ` [PATCH v3 2/3] oidmap: rework iterators to return typed pointer Abhishek Kumar
2020-04-25 2:59 ` [PATCH v3 3/3] oidmap: return oidmap_entry pointers Abhishek Kumar
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200408040659.14511-2-abhishekkumar8222@gmail.com \
--to=abhishekkumar8222@gmail.com \
--cc=git@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).