Add a shorthand for creating an alias of a child<> property. If you pass a NULL target_name to object_property_add_alias, the function will look up the child property that leads to target_obj, and create an alias for that property.
This can be useful when an object wants to add a link to itself at a well-known location. For example, a real-time clock device might add a link to itself at "/machine/rtc". Such well-known locations can then expose a standard set of properties that can be accessed via the "qom-get" and "qom-set" commands. Signed-off-by: Paolo Bonzini <pbonz...@redhat.com> --- include/qom/object.h | 10 +++++++--- qom/object.c | 16 +++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/qom/object.h b/include/qom/object.h index 9c4a5a4..9cd0ffa 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -1256,11 +1256,15 @@ void object_property_add_uint64_ptr(Object *obj, const char *name, * @obj: the object to add a property to * @name: the name of the property * @target_obj: the object to forward property access to - * @target_name: the name of the property on the forwarded object + * @target_name: the name of the property on the forwarded object, or + * #NULL to make an object alias. * @errp: if an error occurs, a pointer to an area to store the error * - * Add an alias for a property on an object. This function will add a property - * of the same type as the forwarded property. + * Add an alias property on an object. This function will add a property + * of the same type as the forwarded property or, if @target_name is #NULL, + * a link property that always resolves to @target_obj. In fact, the case + * of a #NULL @target_obj actually creates an alias property that targets + * @target_obj's own child property. * * The caller must ensure that <code>@target_obj</code> stays alive as long as * this property exists. In the case of a child object or an alias on the same diff --git a/qom/object.c b/qom/object.c index ddf781e..1e8e6af 100644 --- a/qom/object.c +++ b/qom/object.c @@ -1535,7 +1535,7 @@ void object_property_add_uint64_ptr(Object *obj, const char *name, typedef struct { Object *target_obj; - const char *target_name; + char *target_name; } AliasProperty; static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, @@ -1566,6 +1566,7 @@ static void property_release_alias(Object *obj, const char *name, void *opaque) { AliasProperty *prop = opaque; + g_free(prop->target_name); g_free(prop); } @@ -1576,9 +1577,18 @@ void object_property_add_alias(Object *obj, const char *name, AliasProperty *prop; ObjectProperty *target_prop; gchar *prop_type; + gchar *the_target_name; - target_prop = object_property_find(target_obj, target_name, errp); + if (!target_name) { + the_target_name = object_get_canonical_path_component(target_obj); + target_obj = target_obj->parent; + } else { + the_target_name = g_strdup(target_name); + } + + target_prop = object_property_find(target_obj, the_target_name, errp); if (!target_prop) { + g_free(the_target_name); return; } @@ -1590,7 +1600,7 @@ void object_property_add_alias(Object *obj, const char *name, prop = g_malloc(sizeof(*prop)); prop->target_obj = target_obj; - prop->target_name = target_name; + prop->target_name = the_target_name; object_property_add_full(obj, name, prop_type, property_get_alias, -- 1.8.3.1