Skip to content

Commit

Permalink
qom: Add "realized" property to Object
Browse files Browse the repository at this point in the history
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 <afaerber@suse.de>
Cc: Anthony Liguori <anthony@codemonkey.ws>
Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Peter Maydell <peter.maydell@linaro.org>
  • Loading branch information
afaerber committed Mar 30, 2012
1 parent d0dbbeb commit bc78ab1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/qemu/object.h
Expand Up @@ -239,6 +239,7 @@ struct ObjectClass
{
/*< private >*/
Type type;
int (*realize)(Object *obj);
};

/**
Expand All @@ -264,6 +265,7 @@ struct Object
QTAILQ_HEAD(, ObjectProperty) properties;
uint32_t ref;
Object *parent;
bool realized;
};

/**
Expand Down
4 changes: 4 additions & 0 deletions qerror.c
Expand Up @@ -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)'",
Expand Down
3 changes: 3 additions & 0 deletions qerror.h
Expand Up @@ -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 } }"

Expand Down
31 changes: 31 additions & 0 deletions qom/object.c
Expand Up @@ -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;
Expand All @@ -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)
Expand Down

0 comments on commit bc78ab1

Please sign in to comment.