[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v1 RFC 04/34] qom: add object_new_propv / object
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH v1 RFC 04/34] qom: add object_new_propv / object_new_proplist constructors |
Date: |
Fri, 17 Apr 2015 10:11:46 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
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 <address@hidden>
> ---
> 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
- [Qemu-devel] [PATCH v1 RFC 00/34] Generic support for TLS protocol & I/O channels, Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 01/34] ui: remove check for failure of qemu_acl_init(), Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 02/34] qom: document user creatable object types in help text, Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 03/34] qom: create objects in two phases, Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 04/34] qom: add object_new_propv / object_new_proplist constructors, Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 05/34] qom: make enum string tables const-correct, Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 07/34] qom: don't pass string table to object_get_enum method, Daniel P. Berrange, 2015/04/17
- [Qemu-devel] [PATCH v1 RFC 06/34] qom: add a object_property_add_enum helper method, Daniel P. Berrange, 2015/04/17