By moving the base fields to a QObjectBase_, QObject can be a type
which also has a 'base' field. This allows writing a generic 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 a typedef and uses a trailing underscore to make
it obvious it is not for normal use and to avoid potential abuse.
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
Message-Id: <
20180419150145.24795-3-marcandre.lureau@redhat.com>
Reviewed-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
#include "qapi/qmp/qobject.h"
struct QBool {
- QObject base;
+ struct QObjectBase_ base;
bool value;
};
} QDictEntry;
struct QDict {
- QObject base;
+ struct QObjectBase_ base;
size_t size;
QLIST_HEAD(,QDictEntry) table[QDICT_BUCKET_MAX];
};
} QListEntry;
struct QList {
- QObject base;
+ struct QObjectBase_ base;
QTAILQ_HEAD(,QListEntry) head;
};
#include "qapi/qmp/qobject.h"
struct QNull {
- QObject base;
+ struct QObjectBase_ base;
};
extern QNull qnull_;
* convert under the hood.
*/
struct QNum {
- QObject base;
+ struct QObjectBase_ base;
QNumKind kind;
union {
int64_t i64;
#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)
+/* this struct must have no other members than base */
+struct QObject {
+ struct QObjectBase_ base;
+};
+
+#define QOBJECT(obj) ({ \
+ typeof(obj) _obj = (obj); \
+ _obj ? container_of(&(_obj)->base, QObject, base) : NULL; \
+})
/* High-level interface for qobject_incref() */
#define QINCREF(obj) \
static inline void qobject_init(QObject *obj, QType type)
{
assert(QTYPE_NONE < type && type < QTYPE__MAX);
- obj->refcnt = 1;
- obj->type = type;
+ obj->base.refcnt = 1;
+ obj->base.type = type;
}
/**
*/
static inline void qobject_incref(QObject *obj)
{
- if (obj)
- obj->refcnt++;
+ if (obj) {
+ obj->base.refcnt++;
+ }
}
/**
*/
static inline void qobject_decref(QObject *obj)
{
- assert(!obj || obj->refcnt);
- if (obj && --obj->refcnt == 0) {
+ assert(!obj || obj->base.refcnt);
+ if (obj && --obj->base.refcnt == 0) {
qobject_destroy(obj);
}
}
*/
static inline QType qobject_type(const QObject *obj)
{
- assert(QTYPE_NONE < obj->type && obj->type < QTYPE__MAX);
- return obj->type;
+ assert(QTYPE_NONE < obj->base.type && obj->base.type < QTYPE__MAX);
+ return obj->base.type;
}
/**
#include "qapi/qmp/qobject.h"
struct QString {
- QObject base;
+ struct QObjectBase_ base;
char *string;
size_t length;
size_t capacity;
void qobject_destroy(QObject *obj)
{
- assert(!obj->refcnt);
- assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX);
- qdestroy[obj->type](obj);
+ assert(!obj->base.refcnt);
+ assert(QTYPE_QNULL < obj->base.type && obj->base.type < QTYPE__MAX);
+ qdestroy[obj->base.type](obj);
}
return true;
}
- if (!x || !y || x->type != y->type) {
+ if (!x || !y || x->base.type != y->base.type) {
return false;
}
- assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX);
+ assert(QTYPE_NONE < x->base.type && x->base.type < QTYPE__MAX);
- return qis_equal[x->type](x, y);
+ return qis_equal[x->base.type](x, y);
}
}
/* Check the references */
- g_assert(qdict_get(dict1, "foo")->refcnt == 1);
- g_assert(qdict_get(dict1, "bar")->refcnt == 1);
+ g_assert(qdict_get(dict1, "foo")->base.refcnt == 1);
+ g_assert(qdict_get(dict1, "bar")->base.refcnt == 1);
if (!overwrite) {
- g_assert(qdict_get(dict2, "foo")->refcnt == 1);
+ g_assert(qdict_get(dict2, "foo")->base.refcnt == 1);
}
/* Clean up */