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.
This commit is contained in:
Thomas Haller 2017-03-10 10:49:28 +01:00
parent 3ce22c7319
commit 7d88bd24f3

View file

@ -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