Some users of QOM need to be able to iterate over properties defined against an object instance. Currently they are just directly using the QTAIL macros against the object properties data structure.
This is bad because it exposes them to changes in the data structure used to store properties, as well as changes in functionality such as ability to register properties against the class. Providing an explicit object_property_foreach method provides a layer of insulation between the QOM user and the QOM internal implementation. Signed-off-by: Daniel P. Berrange <berra...@redhat.com> --- include/qom/object.h | 23 +++++++++++++++++++++++ qom/object.c | 17 +++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/include/qom/object.h b/include/qom/object.h index be7280c..71503af 100644 --- a/include/qom/object.h +++ b/include/qom/object.h @@ -960,6 +960,29 @@ void object_property_del(Object *obj, const char *name, Error **errp); ObjectProperty *object_property_find(Object *obj, const char *name, Error **errp); +typedef void (*ObjectPropertyIterator)(Object *obj, + ObjectProperty *prop, + Error **errp, + void *opaque); + +/** + * object_property_foreach: + * @obj: the object + * @iter: the iterator callback function + * @errp: returns an error if iterator function fails + * @opaque: opaque data to pass to @iter + * + * Iterates over all properties defined against the object + * instance calling @iter for each property. + * + * It is forbidden to modify the property list from @iter + * whether removing or adding properties. + */ +void object_property_foreach(Object *obj, + ObjectPropertyIterator iter, + Error **errp, + void *opaque); + void object_unparent(Object *obj); /** diff --git a/qom/object.c b/qom/object.c index 4805328..f8b27af 100644 --- a/qom/object.c +++ b/qom/object.c @@ -917,6 +917,23 @@ ObjectProperty *object_property_find(Object *obj, const char *name, return NULL; } +void object_property_foreach(Object *obj, + ObjectPropertyIterator iter, + Error **errp, + void *opaque) +{ + ObjectProperty *prop; + Error *local_err = NULL; + + QTAILQ_FOREACH(prop, &obj->properties, node) { + iter(obj, prop, &local_err, opaque); + if (local_err) { + error_propagate(errp, local_err); + return; + } + } +} + void object_property_del(Object *obj, const char *name, Error **errp) { ObjectProperty *prop = object_property_find(obj, name, errp); -- 2.4.3