From bc78ab1c0e4ba375bc5942447644323281184a31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20F=C3=A4rber?= Date: Sat, 24 Mar 2012 01:19:16 +0100 Subject: [PATCH] qom: Add "realized" property to Object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Object::realized property can only be set once and, on setting it, invokes the ObjectClass::realize callback. Introduce QERR_OBJECT_REALIZE_FAILED for error handling. Signed-off-by: Andreas Färber Cc: Anthony Liguori Cc: Paolo Bonzini Cc: Peter Maydell --- include/qemu/object.h | 2 ++ qerror.c | 4 ++++ qerror.h | 3 +++ qom/object.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+) diff --git a/include/qemu/object.h b/include/qemu/object.h index e8fc1268b3..742b5b653e 100644 --- a/include/qemu/object.h +++ b/include/qemu/object.h @@ -239,6 +239,7 @@ struct ObjectClass { /*< private >*/ Type type; + int (*realize)(Object *obj); }; /** @@ -264,6 +265,7 @@ struct Object QTAILQ_HEAD(, ObjectProperty) properties; uint32_t ref; Object *parent; + bool realized; }; /** diff --git a/qerror.c b/qerror.c index 41c729a01f..79c91da273 100644 --- a/qerror.c +++ b/qerror.c @@ -216,6 +216,10 @@ static const QErrorStringTable qerror_table[] = { .error_fmt = QERR_NOT_SUPPORTED, .desc = "Not supported", }, + { + .error_fmt = QERR_OBJECT_REALIZE_FAILED, + .desc = "Object of type '%(type)' could not be realized", + }, { .error_fmt = QERR_OPEN_FILE_FAILED, .desc = "Could not open '%(filename)'", diff --git a/qerror.h b/qerror.h index e16f9c27e1..eb754852d5 100644 --- a/qerror.h +++ b/qerror.h @@ -184,6 +184,9 @@ QError *qobject_to_qerror(const QObject *obj); #define QERR_NOT_SUPPORTED \ "{ 'class': 'NotSupported', 'data': {} }" +#define QERR_OBJECT_REALIZE_FAILED \ + "{ 'class': 'ObjectRealizeFailed', 'data': { 'type': %s } }" + #define QERR_OPEN_FILE_FAILED \ "{ 'class': 'OpenFileFailed', 'data': { 'filename': %s } }" diff --git a/qom/object.c b/qom/object.c index 9cd9506eb5..388cf864a6 100644 --- a/qom/object.c +++ b/qom/object.c @@ -273,6 +273,34 @@ static void object_init_with_type(Object *obj, TypeImpl *ti) } } +static void object_get_realized(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + visit_type_bool(v, &obj->realized, name, errp); +} + +static void object_set_realized(Object *obj, Visitor *v, void *opaque, + const char *name, Error **errp) +{ + bool value; + + if (obj->realized) { + error_set(errp, QERR_PERMISSION_DENIED); + return; + } + + visit_type_bool(v, &value, name, errp); + if (error_is_set(errp) || !value) { + return; + } + + if (obj->class->realize != NULL && obj->class->realize(obj) != 0) { + error_set(errp, QERR_OBJECT_REALIZE_FAILED, object_get_typename(obj)); + return; + } + obj->realized = true; +} + void object_initialize_with_type(void *data, TypeImpl *type) { Object *obj = data; @@ -287,6 +315,9 @@ void object_initialize_with_type(void *data, TypeImpl *type) obj->class = type->class; QTAILQ_INIT(&obj->properties); object_init_with_type(obj, type); + object_property_add(obj, "realized", "boolean", + object_get_realized, object_set_realized, + NULL, NULL, NULL); } void object_initialize(void *data, const char *typename)