From 66d4af6daf8dba02c424592b54daa4d33db3ba10 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 23 Jul 2020 11:05:13 +0200 Subject: [PATCH] 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. --- shared/nm-glib-aux/nm-shared-utils.c | 23 ++++++++++++++++++ shared/nm-glib-aux/nm-shared-utils.h | 35 ++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index f7ae5b88de..e7dbaa8a81 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -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) diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 6aafd09e98..10dd145df2 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -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