diff options
author | Beniamino Galvani <bgalvani@redhat.com> | 2015-09-28 17:05:15 +0200 |
---|---|---|
committer | Beniamino Galvani <bgalvani@redhat.com> | 2015-10-01 09:05:08 +0200 |
commit | bd27c110a3d6dede825efb4517d296c89b01a156 (patch) | |
tree | b37bf5da70f251f8ff1d69a1e03fa5d2c7d046f7 | |
parent | 804ec6fbcd9bae95b8fb17e1eab2b3a25a601b15 (diff) | |
download | NetworkManager-bd27c110a3d6dede825efb4517d296c89b01a156.tar.gz |
glib-compat: add g_hash_table_get_keys_as_array() compat function
-rw-r--r-- | include/nm-glib.h | 39 | ||||
-rw-r--r-- | libnm-core/tests/test-general.c | 36 |
2 files changed, 75 insertions, 0 deletions
diff --git a/include/nm-glib.h b/include/nm-glib.h index 7e42016b94..9d497c4de2 100644 --- a/include/nm-glib.h +++ b/include/nm-glib.h @@ -303,4 +303,43 @@ _g_key_file_save_to_file (GKeyFile *key_file, #endif +#if !GLIB_CHECK_VERSION(2, 40, 0) || defined (NM_GLIB_COMPAT_H_TEST) +static inline gpointer * +_nm_g_hash_table_get_keys_as_array (GHashTable *hash_table, + guint *length) +{ + GHashTableIter iter; + gpointer key, *ret; + guint i = 0; + + g_return_val_if_fail (hash_table, NULL); + + ret = g_new0 (gpointer, g_hash_table_size (hash_table) + 1); + g_hash_table_iter_init (&iter, hash_table); + + while (g_hash_table_iter_next (&iter, &key, NULL)) + ret[i++] = key; + + ret[i] = NULL; + + if (length) + *length = i; + + return ret; +} +#endif +#if !GLIB_CHECK_VERSION(2, 40, 0) +#define g_hash_table_get_keys_as_array(hash_table, length) \ + G_GNUC_EXTENSION ({ \ + _nm_g_hash_table_get_keys_as_array (hash_table, length); \ + }) +#else +#define g_hash_table_get_keys_as_array(hash_table, length) \ + G_GNUC_EXTENSION ({ \ + G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ + (g_hash_table_get_keys_as_array) ((hash_table), (length)); \ + G_GNUC_END_IGNORE_DEPRECATIONS \ + }) +#endif + #endif /* __NM_GLIB_H__ */ diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c index 2253f0c3ba..4eb739e994 100644 --- a/libnm-core/tests/test-general.c +++ b/libnm-core/tests/test-general.c @@ -4534,6 +4534,41 @@ test_g_ptr_array_insert (void) /******************************************************************************/ +static void +test_g_hash_table_get_keys_as_array (void) +{ + GHashTable *table = g_hash_table_new (g_str_hash, g_str_equal); + guint length; + char **keys; + + g_hash_table_insert (table, "one", "1"); + g_hash_table_insert (table, "two", "2"); + g_hash_table_insert (table, "three", "3"); + + keys = (char **) _nm_g_hash_table_get_keys_as_array (table, &length); + g_assert (keys); + g_assert_cmpuint (length, ==, 3); + + g_assert ( !strcmp (keys[0], "one") + || !strcmp (keys[1], "one") + || !strcmp (keys[2], "one")); + + g_assert ( !strcmp (keys[0], "two") + || !strcmp (keys[1], "two") + || !strcmp (keys[2], "two")); + + g_assert ( !strcmp (keys[0], "three") + || !strcmp (keys[1], "three") + || !strcmp (keys[2], "three")); + + g_assert (!keys[3]); + + g_free (keys); + g_hash_table_unref (table); +} + +/******************************************************************************/ + static int _test_find_binary_search_cmp (gconstpointer a, gconstpointer b, gpointer dummy) { @@ -4903,6 +4938,7 @@ int main (int argc, char **argv) g_test_add_func ("/core/general/_nm_utils_ascii_str_to_int64", test_nm_utils_ascii_str_to_int64); g_test_add_func ("/core/general/nm_utils_is_power_of_two", test_nm_utils_is_power_of_two); g_test_add_func ("/core/general/_glib_compat_g_ptr_array_insert", test_g_ptr_array_insert); + g_test_add_func ("/core/general/_glib_compat_g_hash_table_get_keys_as_array", test_g_hash_table_get_keys_as_array); g_test_add_func ("/core/general/_nm_utils_ptrarray_find_binary_search", test_nm_utils_ptrarray_find_binary_search); g_test_add_func ("/core/general/_nm_utils_strstrdictkey", test_nm_utils_strstrdictkey); |