glib-compat: add nm_g_hash_table_replace() compat function

The newer version of g_hash_table_replace() is useful, because it
saves an additional hash table lookup.

(cherry picked from commit 2bea70e750)
This commit is contained in:
Thomas Haller 2015-04-30 15:15:34 +02:00
parent fa7acaef83
commit a87231abbc

View file

@ -163,4 +163,21 @@ q_n##_quark (void) \
}
#endif
static inline gboolean
nm_g_hash_table_replace (GHashTable *hash, gpointer key, gpointer value)
{
/* glib 2.40 added a return value indicating whether the key already existed
* (910191597a6c2e5d5d460e9ce9efb4f47d9cc63c). */
#if GLIB_CHECK_VERSION(2, 40, 0)
return g_hash_table_replace (hash, key, value);
#else
gboolean contained = g_hash_table_contains (hash, key);
g_hash_table_replace (hash, key, value);
return !contained;
#endif
}
#endif /* __NM_GLIB_COMPAT_H__ */