On 10/08/2015 08:09 AM, Daniel P. Berrange wrote: > 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);
Do we want the iterator to be able to return a value, and possibly allow a non-zero value to abort iteration? [1] > + > +/** > + * 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. Probably should mention that there is an early exit if error gets set [2] > + * > + * 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); [1] if we allow the iterator to return a non-zero value to abort iteration (particularly if it wants to abort iteration without setting an error, just to save CPU cycles), should this foreach() function return that value? Of course, a caller can always use opaque to achieve the same purpose, but it gets more verbose (the caller has to reserve space in their opaque for tracking whether an interesting exit value is needed, as well as having an early check on whether the value was already set in a previous visit) and burns more CPU cycles (the iterator runs to completion, even though the later callbacks are doing nothing). > +++ 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; [2] there's the early exit if an error is set. The code looks fine, but I think we need a documentation improvement for issue [2], and we may want a design change for issue [1] if a non-void return would be useful to any client later in the series. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature