platform: add nm_clear_nmp_object_up_cast(), nmp_object_ref_set_up_cast() helpers

It can be convenient to track a NMPObject in form of their down-cast
pointers. When doing that, it's useful to have clear/ref-set helpers
that operate on such pointers and up-cast first.
This commit is contained in:
Thomas Haller 2021-08-26 10:21:57 +02:00
parent 92ba45727b
commit cc38b36f8c
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -625,6 +625,20 @@ nmp_object_unref(const NMPObject *obj)
_changed; \
})
#define nm_clear_nmp_object_up_cast(ptr) \
({ \
typeof(ptr) _ptr = (ptr); \
typeof(*_ptr) _pptr; \
gboolean _changed = FALSE; \
\
if (_ptr && (_pptr = *_ptr)) { \
*_ptr = NULL; \
nmp_object_unref(NMP_OBJECT_UP_CAST(_pptr)); \
_changed = TRUE; \
} \
_changed; \
})
static inline gboolean
nmp_object_ref_set(const NMPObject **pp, const NMPObject *obj)
{
@ -643,6 +657,25 @@ nmp_object_ref_set(const NMPObject **pp, const NMPObject *obj)
return _changed;
}
static inline gboolean
nmp_object_ref_set_up_cast(gpointer pp, gconstpointer obj)
{
gboolean _changed = FALSE;
const NMPObject *p;
gconstpointer * pp2 = pp;
nm_assert(!pp2 || !*pp2 || NMP_OBJECT_IS_VALID(NMP_OBJECT_UP_CAST(*pp2)));
nm_assert(!obj || NMP_OBJECT_IS_VALID(NMP_OBJECT_UP_CAST(obj)));
if (pp2 && ((p = *pp2) != obj)) {
nmp_object_ref(NMP_OBJECT_UP_CAST(obj));
*pp2 = obj;
nmp_object_unref(NMP_OBJECT_UP_CAST(p));
_changed = TRUE;
}
return _changed;
}
NMPObject *nmp_object_new(NMPObjectType obj_type, gconstpointer plobj);
NMPObject *nmp_object_new_link(int ifindex);