On 03/21/2018 08:40 AM, Marc-André Lureau wrote:
Simplify casting to QObject by not requiring the definition of the
type and assuming base QObject is at offset 0. Add a static check for
this.
Use the QEMU_GENERIC macro for keeping type safety check and error
when given an unknown type.
Signed-off-by: Marc-André Lureau <[email protected]>
---
include/qapi/qmp/qobject.h | 36 ++++++++++++++++++++++++++++++++++--
qobject/qobject.c | 9 +++++++++
2 files changed, 43 insertions(+), 2 deletions(-)
+++ b/qobject/qobject.c
@@ -16,6 +16,15 @@
#include "qapi/qmp/qlist.h"
#include "qapi/qmp/qstring.h"
+QEMU_BUILD_BUG_MSG(
+ offsetof(QNull, base) != 0 ||
+ offsetof(QNum, base) != 0 ||
+ offsetof(QString, base) != 0 ||
+ offsetof(QDict, base) != 0 ||
+ offsetof(QList, base) != 0 ||
+ offsetof(QBool, base) != 0,
+ "base qobject must be at offset 0");
Interesting. If we do this, we should also simplify qobject_to() from
its current:
#define qobject_to(type, obj) ({ \
QObject *_tmp = qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)); \
_tmp ? container_of(_tmp, type, base) : (type *)NULL; })
to the simpler:
#define qobject_to(type, obj) \
((type *)qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)))
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org