From 7d88bd24f3018d337cf9dc07f341de755a9c095e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 10 Mar 2017 10:49:28 +0100 Subject: [PATCH] shared: add _NM_GET_PRIVATE_VOID() macro _NM_GET_PRIVATE() macro is used to implement a standard private-getter, but it requires that "self" is a pointer of either "const type *" or "type *". That is great in most cases, but sometimes we have predominatly self pointers of different type, so it would require a lot of casts. Add a different form _NM_GET_PRIVATE_VOID() where self pointer can be any non-const pointer and returns a non-const private pointer after casting. --- shared/nm-utils/nm-macros-internal.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 2548665458..89e4fb591b 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -469,6 +469,9 @@ _notify (obj_type *obj, _PropertyEnums prop) \ /*****************************************************************************/ +/* these are implemented as a macro, because they accept self + * as both (type*) and (const type*), and return a const + * private pointer accordingly. */ #define __NM_GET_PRIVATE(self, type, is_check, result_cmd) \ ({ \ /* preserve the const-ness of self. Unfortunately, that @@ -476,7 +479,7 @@ _notify (obj_type *obj, _PropertyEnums prop) \ typeof (self) _self = (self); \ \ /* Get compiler error if variable is of wrong type */ \ - _nm_unused const type *_self2 = (_self); \ + _nm_unused const type *const _self2 = (_self); \ \ nm_assert (is_check (_self)); \ ( result_cmd ); \ @@ -485,6 +488,21 @@ _notify (obj_type *obj, _PropertyEnums prop) \ #define _NM_GET_PRIVATE(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check, &_self->_priv) #define _NM_GET_PRIVATE_PTR(self, type, is_check) __NM_GET_PRIVATE(self, type, is_check, _self->_priv) +#define __NM_GET_PRIVATE_VOID(self, type, is_check, result_cmd) \ + ({ \ + /* (self) can be any non-const pointer. It will be cast to "type *". + * We don't explicitly cast but assign first to (void *) which + * will fail if @self is pointing to const. */ \ + void *const _self1 = (self); \ + type *const _self = _self1; \ + \ + nm_assert (is_check (_self)); \ + ( result_cmd ); \ + }) + +#define _NM_GET_PRIVATE_VOID(self, type, is_check) __NM_GET_PRIVATE_VOID(self, type, is_check, &_self->_priv) +#define _NM_GET_PRIVATE_PTR_VOID(self, type, is_check) __NM_GET_PRIVATE_VOID(self, type, is_check, _self->_priv) + /*****************************************************************************/ static inline gpointer