From 3ba8959dfdfd34de3a48a02a3a0f513db56a4258 Mon Sep 17 00:00:00 2001 From: "Daniel P. Berrange" Date: Sat, 17 Feb 2018 18:38:49 -0500 Subject: [PATCH] qom: Introduce ObjectPropertyIterator struct for iteration 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. This provides an ObjectPropertyIterator struct which will insulate the callers from the particular data structure used to store properties. It can be used thus ObjectProperty *prop; ObjectPropertyIterator *iter; iter = object_property_iter_init(obj); while ((prop = object_property_iter_next(iter))) { ... do something with prop ... } object_property_iter_free(iter); Backports commit a00c94824126901168bca5b89147f9e334a49e87 from qemu --- qemu/include/qom/object.h | 49 +++++++++++++++++++++++++++++++++++++++ qemu/qom/object.c | 27 +++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/qemu/include/qom/object.h b/qemu/include/qom/object.h index 1acec30d..c9a57eaa 100644 --- a/qemu/include/qom/object.h +++ b/qemu/include/qom/object.h @@ -822,6 +822,55 @@ void object_property_del_child(struct uc_struct *uc, Object *obj, Object *child, ObjectProperty *object_property_find(Object *obj, const char *name, Error **errp); +typedef struct ObjectPropertyIterator ObjectPropertyIterator; + +/** + * object_property_iter_init: + * @obj: the object + * + * Initializes an iterator for traversing all properties + * registered against an object instance. + * + * It is forbidden to modify the property list while iterating, + * whether removing or adding properties. + * + * Typical usage pattern would be + * + * + * Using object property iterators + * + * ObjectProperty *prop; + * ObjectPropertyIterator *iter; + * + * iter = object_property_iter_init(obj); + * while ((prop = object_property_iter_next(iter))) { + * ... do something with prop ... + * } + * object_property_iter_free(iter); + * + * + * + * Returns: the new iterator + */ +ObjectPropertyIterator *object_property_iter_init(Object *obj); + +/** + * object_property_iter_free: + * @iter: the iterator instance + * + * Releases any resources associated with the iterator. + */ +void object_property_iter_free(ObjectPropertyIterator *iter); + +/** + * object_property_iter_next: + * @iter: the iterator instance + * + * Returns: the next property, or %NULL when all properties + * have been traversed. + */ +ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter); + void object_unparent(struct uc_struct *uc, Object *obj); /** diff --git a/qemu/qom/object.c b/qemu/qom/object.c index 66fa6c48..9241db69 100644 --- a/qemu/qom/object.c +++ b/qemu/qom/object.c @@ -65,6 +65,9 @@ struct TypeImpl InterfaceImpl interfaces[MAX_INTERFACES]; }; +struct ObjectPropertyIterator { + ObjectProperty *next; +}; static GHashTable *type_table_get(struct uc_struct *uc) { @@ -799,6 +802,30 @@ ObjectProperty *object_property_find(Object *obj, const char *name, return NULL; } +ObjectPropertyIterator *object_property_iter_init(Object *obj) +{ + ObjectPropertyIterator *ret = g_new0(ObjectPropertyIterator, 1); + ret->next = QTAILQ_FIRST(&obj->properties); + return ret; +} + +void object_property_iter_free(ObjectPropertyIterator *iter) +{ + if (!iter) { + return; + } + g_free(iter); +} + +ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) +{ + ObjectProperty *ret = iter->next; + if (ret) { + iter->next = QTAILQ_NEXT(iter->next, node); + } + return ret; +} + void object_property_del(struct uc_struct *uc, Object *obj, const char *name, Error **errp) { ObjectProperty *prop = object_property_find(obj, name, errp);