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
This commit is contained in:
Daniel P. Berrange 2018-02-17 18:38:49 -05:00 committed by Lioncash
parent 5df03a909c
commit 3ba8959dfd
No known key found for this signature in database
GPG key ID: 4E3C3CC1031BA9C7
2 changed files with 76 additions and 0 deletions

View file

@ -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
*
* <example>
* <title>Using object property iterators</title>
* <programlisting>
* 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);
* </programlisting>
* </example>
*
* 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);
/**

View file

@ -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);