diff options
author | Dan Williams <dcbw@redhat.com> | 2018-12-03 11:40:43 -0600 |
---|---|---|
committer | Dan Williams <dcbw@redhat.com> | 2018-12-04 17:39:15 +0000 |
commit | 397faef3c5ce9eb5274adc73d31bd8002dd28c21 (patch) | |
tree | cb8dd6ca0bbe8cde6145d789680012fcebe07d16 /libmm-glib/mm-common-helpers.h | |
parent | 9c15579347c0e8adfafbc11aeaccdfebaa8ac626 (diff) |
mm-common-helpers: add mm_autoptr helpers for GRegex and GMatchInfo
Alias them to g_autoptr() on glib >= 2.44, otherwise re-implement.
Diffstat (limited to 'libmm-glib/mm-common-helpers.h')
-rw-r--r-- | libmm-glib/mm-common-helpers.h | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/libmm-glib/mm-common-helpers.h b/libmm-glib/mm-common-helpers.h index 6f8b68ac..fce9ff25 100644 --- a/libmm-glib/mm-common-helpers.h +++ b/libmm-glib/mm-common-helpers.h @@ -174,4 +174,27 @@ gboolean mm_utils_ishexstr (const gchar *hex); gboolean mm_utils_check_for_single_value (guint32 value); +#if GLIB_CHECK_VERSION(2, 44, 0) +#define mm_autoptr g_autoptr +#else + +/* Re-implement for those glib that don't have it */ +#define _MM_AUTOPTR_FUNC_NAME(TypeName) mm_autoptr_cleanup_##TypeName +#define _MM_AUTOPTR_TYPENAME(TypeName) TypeName##_autoptr +#define _MM_CLEANUP(func) __attribute__((cleanup(func))) + +#define _MM_DEFINE_AUTOPTR_CLEANUP_FUNCS(TypeName, cleanup) \ + typedef TypeName *_MM_AUTOPTR_TYPENAME(TypeName); \ + G_GNUC_BEGIN_IGNORE_DEPRECATIONS \ + static G_GNUC_UNUSED inline void _MM_AUTOPTR_FUNC_NAME(TypeName) (TypeName **_ptr) \ + { if (_ptr && *_ptr) (cleanup) (*_ptr); } \ + G_GNUC_END_IGNORE_DEPRECATIONS + +#define mm_autoptr(TypeName) _MM_CLEANUP(_MM_AUTOPTR_FUNC_NAME(TypeName)) _MM_AUTOPTR_TYPENAME(TypeName) + +_MM_DEFINE_AUTOPTR_CLEANUP_FUNCS(GRegex, g_regex_unref) +_MM_DEFINE_AUTOPTR_CLEANUP_FUNCS(GMatchInfo, g_match_info_unref) + +#endif + #endif /* MM_COMMON_HELPERS_H */ |