shared: add compat function nm_g_ptr_array_copy() for older glib

This is not in "nm-glib.h", because it's not a complete replacement.
In glib before 2.62, it's not possible to implement g_ptr_array_copy()
as glib provides it, because the element_free_func is not accessible.

So, instead add our own implemented, which follows glib's version as
much as it can.
This commit is contained in:
Thomas Haller 2020-07-23 11:05:13 +02:00
parent 8346870aa6
commit 66d4af6daf
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
2 changed files with 58 additions and 0 deletions

View file

@ -2188,6 +2188,29 @@ _nm_utils_strv_cleanup (char **strv,
/*****************************************************************************/
GPtrArray *
_nm_g_ptr_array_copy (GPtrArray *array,
GCopyFunc func,
gpointer user_data,
GDestroyNotify element_free_func)
{
GPtrArray *new_array;
guint i;
g_return_val_if_fail (array, NULL);
new_array = g_ptr_array_new_full (array->len, element_free_func);
for (i = 0; i < array->len; i++) {
g_ptr_array_add (new_array,
func
? func (array->pdata[i], user_data)
: array->pdata[i]);
}
return new_array;
}
/*****************************************************************************/
int
_nm_utils_ascii_str_to_bool (const char *str,
int default_value)

View file

@ -1539,6 +1539,41 @@ nm_g_ptr_array_len (const GPtrArray *arr)
return arr ? arr->len : 0u;
}
GPtrArray *_nm_g_ptr_array_copy (GPtrArray *array,
GCopyFunc func,
gpointer user_data,
GDestroyNotify element_free_func);
/**
* nm_g_ptr_array_copy:
* @array: the #GPtrArray to clone.
* @func: the copy function.
* @user_data: the user data for the copy function
* @element_free_func: the free function of the elements. @array MUST have
* the same element_free_func. This argument is only used on older
* glib, that doesn't support g_ptr_array_copy().
*
* This is a replacement for g_ptr_array_copy(), which is not available
* before glib 2.62. Since GPtrArray does not allow to access the internal
* element_free_func, we cannot add a compatibility implementation of g_ptr_array_copy()
* and the user must provide a suitable destroy function.
*
* Note that the @element_free_func MUST correspond to free function set in @array.
*/
#if GLIB_CHECK_VERSION(2,62,0)
#define nm_g_ptr_array_copy(array, func, user_data, element_free_func) \
({ \
_nm_unused GDestroyNotify const _element_free_func = (element_free_func); \
\
G_GNUC_BEGIN_IGNORE_DEPRECATIONS; \
g_ptr_array_copy ((array), (func), (user_data)); \
G_GNUC_END_IGNORE_DEPRECATIONS; \
})
#else
#define nm_g_ptr_array_copy(array, func, user_data, element_free_func) \
_nm_g_ptr_array_copy ((array), (func), (user_data), (element_free_func))
#endif
/*****************************************************************************/
static inline guint