unicorn/qemu/qom/container.c
Daniel P. Berrange b97ab59f08
qom: Allow properties to be registered against classes
When there are many instances of a given class, registering
properties against the instance is wasteful of resources. The
majority of objects have a statically defined list of possible
properties, so most of the properties are easily registerable
against the class. Only those properties which are conditionally
registered at runtime need be recorded against the klass.

Registering properties against classes also makes it possible
to provide static introspection of QOM - currently introspection
is only possible after creating an instance of a class, which
severely limits its usefulness.

This impl only supports simple scalar properties. It does not
attempt to allow child object / link object properties against
the class. There are ways to support those too, but it would
make this patch more complicated, so it is left as an exercise
for the future.

There is no equivalent to object_property_del() provided, since
classes must be immutable once they are defined.

Backports commit 16bf7f522a2ff68993f80631ed86254c71eaf5d4 from qemu
2018-02-21 21:00:56 -05:00

50 lines
1.1 KiB
C

/*
* Device Container
*
* Copyright IBM, Corp. 2012
*
* Authors:
* Anthony Liguori <aliguori@us.ibm.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*/
#include "qemu/osdep.h"
#include "qom/object.h"
#include "qemu/module.h"
static const TypeInfo container_info = {
"container",
TYPE_OBJECT,
0,
sizeof(Object),
};
void container_register_types(struct uc_struct *uc)
{
type_register_static(uc, &container_info);
}
Object *container_get(struct uc_struct *uc, Object *root, const char *path)
{
Object *obj, *child;
gchar **parts;
int i;
parts = g_strsplit(path, "/", 0);
assert(parts != NULL && parts[0] != NULL && !parts[0][0]);
obj = root;
for (i = 1; parts[i] != NULL; i++, obj = child) {
child = object_resolve_path_component(uc, obj, parts[i]);
if (!child) {
child = object_new(uc, "container");
object_property_add_child(uc, obj, parts[i], child, NULL);
}
}
g_strfreev(parts);
return obj;
}