On 04/17/2018 08:35 AM, Marc-André Lureau wrote: > By moving the base fields to a QObjectBase_, QObject can be a type > which also has a 'base' field. This allows to write a generic
either "allows us to write" or "allows writing" > QOBJECT() macro that will work with any QObject type, including > QObject itself. The container_of() macro ensures that the object to > cast has a QObjectBase_ base field, giving some type safety > guarantees. QObject must have no members but QObjectBase_ base, or > else QOBJECT() breaks. > > QObjectBase_ is not typedef and use a trailing underscore to make it s/not typedef and use/not a typedef and uses/ > obvious it is not for normal use and to avoid potential abuse. > > Signed-off-by: Marc-André Lureau <marcandre.lur...@redhat.com> > --- > +++ b/include/qapi/qmp/qobject.h > @@ -34,13 +34,20 @@ > > #include "qapi/qapi-builtin-types.h" > > -struct QObject { > +/* Not for use outside include/qapi/qmp/ */ > +struct QObjectBase_ { > QType type; > size_t refcnt; > }; > > -/* Get the 'base' part of an object */ > -#define QOBJECT(obj) (&(obj)->base) > +struct QObject { > + struct QObjectBase_ base; I'd add a comment here that this struct must have no other members than base. > +}; > + > +#define QOBJECT(x) ({ \ > + typeof(x) __x = (x); \ > + __x ? container_of(&(__x)->base, QObject, base) : NULL; \ > +}) > With those changes, Reviewed-by: Eric Blake <ebl...@redhat.com> -- Eric Blake, Principal Software Engineer Red Hat, Inc. +1-919-301-3266 Virtualization: qemu.org | libvirt.org
signature.asc
Description: OpenPGP digital signature