glib-aux: add nm_g_variant_maybe_singleton_i() helper

This commit is contained in:
Thomas Haller 2021-10-26 21:17:47 +02:00
parent b450221195
commit f730edfcb1
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 23 additions and 0 deletions

View file

@ -672,6 +672,27 @@ nm_g_variant_singleton_ao(void)
return _variant_singleton_get_array("o");
}
GVariant *
nm_g_variant_maybe_singleton_i(gint32 value)
{
/* Warning: this function always returns a non-floating reference
* that must be consumed (and later unrefed) by the caller.
*
* The instance is either a singleton instance or a newly created
* instance.
*
* The idea of this is that common values (zero) can use the immutable
* singleton/flyweight instance and avoid allocating a new instance in
* the (presumable) common case.
*/
switch (value) {
case 0:
return g_variant_ref(nm_g_variant_singleton_i_0());
default:
return g_variant_take_ref(g_variant_new_int32(value));
}
}
/*****************************************************************************/
GHashTable *

View file

@ -1541,6 +1541,8 @@ GVariant *nm_g_variant_singleton_aLsaLsvII(void);
GVariant *nm_g_variant_singleton_aaLsvI(void);
GVariant *nm_g_variant_singleton_ao(void);
GVariant *nm_g_variant_maybe_singleton_i(gint32 v);
static inline void
nm_g_variant_unref_floating(GVariant *var)
{