On 04/17/2015 08:22 AM, Daniel P. Berrange wrote: > It is reasonably common to want to create an object, set a > number of properties, register it in the hierarchy and then > mark it as complete (if a user creatable type). This requires > quite alot of error prone, verbose, boilerplate code to achieve.
s/alot/a lot/ > > The object_new_propv / object_new_proplist constructors will > simplify this task by performing all required steps in one go, > accepting the property names/values as variadic args. > > Usage would be: > > Error *err = NULL; > Object *obj; > obj = object_new_propv(TYPE_MEMORY_BACKEND_FILE, > "hostmem0", > &err, > "share", "yes", > "mem-path", "/dev/shm/somefile", > "prealloc", "yes", > "size": "1048576", s/:/,/ > NULL); > > Note all property values are passed in string form and will > be parsed into their required data types. > > Signed-off-by: Daniel P. Berrange <berra...@redhat.com> > --- > include/qom/object.h | 58 +++++++++++++++++++++++++++++++++++++++++++++++ > qom/object.c | 64 > ++++++++++++++++++++++++++++++++++++++++++++++++++++ > tests/Makefile | 2 +- > 3 files changed, 123 insertions(+), 1 deletion(-) > > diff --git a/include/qom/object.h b/include/qom/object.h > index d2d7748..223b577 100644 > --- a/include/qom/object.h > + * > + * obj = object_new_propv(TYPE_MEMORY_BACKEND_FILE, > + * "hostmem0", > + * &err, > + * "share", "yes", > + * "mem-path", "/dev/shm/somefile", > + * "prealloc", "yes", > + * "size": "1048576", and again > + * NULL); > + * > + * if (!obj) { > + * g_printerr("Cannot create memory backend: %s\n", > + * error_get_pretty(err)); > + * } > + * > + * Returns: The newly allocated, instantiated & initialized object. > + */ > +Object *object_new_propv(const char *typename, > + const char *id, > + Error **errp, > + ...); You probably want to use the gcc __attribute__((__sentinel__)), so that the compiler can ensure that the caller NULL-terminates their list. > +Object *object_new_proplist(const char *typename, > + const char *id, > + Error **errp, > + va_list vargs) > +{ > + Object *obj; > + const char *propname; > + > + obj = object_new(typename); > + > + if (object_class_is_abstract(object_get_class(obj))) { > + error_setg(errp, "object type '%s' is abstract", typename); > + goto error; > + } > + > + propname = va_arg(vargs, char *); > + while (propname != NULL) { > + const char *value = va_arg(vargs, char *); > + > + object_property_parse(obj, value, propname, errp); Is it worth a sanity check of assert(value) prior to calling object_property_parse(), as I have the suspicion that it doesn't handle NULL very well? -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature