From 5ab15ac175ca45fce1b63a910f8ae2c488b92cfe Mon Sep 17 00:00:00 2001 From: Jonathan Lebon <jlebon@redhat.com> Date: Tue, 27 Jun 2017 20:07:16 -0700 Subject: [PATCH] macros: add GLNX_HASH_TABLE_FOREACH_V Looking at converting the ostree codebase, iterating over only the values of a hash table (while ignoring the key) is actually a more common pattern than I thought. So let's give it its own macro as well so users don't have to resort to the _KV variant. --- glnx-macros.h | 11 ++++++++++- tests/test-libglnx-macros.c | 8 ++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/glnx-macros.h b/glnx-macros.h index cee068d4f..b5127b74f 100644 --- a/glnx-macros.h +++ b/glnx-macros.h @@ -146,9 +146,10 @@ G_BEGIN_DECLS * All variables are scoped within the loop. You may use the `it` variable as * usual, e.g. to remove an element using g_hash_table_iter_remove(&it). There * are shorter variants for the more common cases where you do not need access - * to the iterator or to values: + * to the iterator or to keys/values: * * GLNX_HASH_TABLE_FOREACH (table, const char*, str) { ... } + * GLNX_HASH_TABLE_FOREACH_V (table, MyData*, data) { ... } * GLNX_HASH_TABLE_FOREACH_KV (table, const char*, str, MyData*, data) { ... } * */ @@ -163,6 +164,14 @@ G_BEGIN_DECLS _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \ _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), kt, k, vt, v) +/* Variant of GLNX_HASH_TABLE_FOREACH_KV which omits unpacking keys. */ +#define GLNX_HASH_TABLE_FOREACH_V(ht, vt, v) \ + _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \ + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_guard_), ht, \ + _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_it_), \ + gpointer, _GLNX_MAKE_ANONYMOUS(_glnx_ht_iter_v_), \ + vt, v) + /* Variant of GLNX_HASH_TABLE_FOREACH_KV which omits unpacking vals. */ #define GLNX_HASH_TABLE_FOREACH(ht, kt, k) \ _GLNX_HASH_TABLE_FOREACH_IMPL_KV( \ diff --git a/tests/test-libglnx-macros.c b/tests/test-libglnx-macros.c index 2a115fca6..ffde8fae2 100644 --- a/tests/test-libglnx-macros.c +++ b/tests/test-libglnx-macros.c @@ -90,6 +90,14 @@ test_hash_table_foreach (void) i++; } g_assert_cmpuint (i, ==, 2); + + i = 0; + GLNX_HASH_TABLE_FOREACH_V (table, const char*, val) + { + g_assert_cmpstr (val, ==, vals[i]); + i++; + } + g_assert_cmpuint (i, ==, 2); } int main (int argc, char **argv) -- GitLab