From 7a956644d4971a523cd9eead52516966360fc309 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 28 Jan 2018 09:08:20 +0100 Subject: [PATCH] shared: add NM_UNCONST_PTR() and NM_UNCONST_PPTR() Add macros that cast away the constness of a pointer, but ensure that the type of the pointer is as expected. Unfortunately, there is no way (AFAIK) to remove the constness of a variable, without explicitly passing @type to the macro. --- shared/nm-utils/nm-macros-internal.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 5837a47bc7..f4b25b34e8 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -389,6 +389,28 @@ NM_G_ERROR_MSG (GError *error) #define NM_CONSTCAST(type, obj, ...) \ NM_CONSTCAST_FULL(type, (obj), (obj), ##__VA_ARGS__) +#if _NM_CC_SUPPORT_GENERIC +#define NM_UNCONST_PTR(type, arg) \ + _Generic ((arg), \ + const type *: ((type *) (arg)), \ + type *: ((type *) (arg))) +#else +#define NM_UNCONST_PTR(type, arg) \ + ((type *) (arg)) +#endif + +#if _NM_CC_SUPPORT_GENERIC +#define NM_UNCONST_PPTR(type, arg) \ + _Generic ((arg), \ + const type * *: ((type **) (arg)), \ + type * *: ((type **) (arg)), \ + const type *const*: ((type **) (arg)), \ + type *const*: ((type **) (arg))) +#else +#define NM_UNCONST_PPTR(type, arg) \ + ((type **) (arg)) +#endif + #define NM_GOBJECT_CAST(type, obj, is_check, ...) \ ({ \ const void *_obj = (obj); \