From: Paolo Bonzini <pbonzini@redhat.com>
To: "Daniel P. Berrange" <berrange@redhat.com>, qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>, Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [Qemu-devel] [PATCH v1 RFC 06/34] qom: add a object_property_add_enum helper method
Date: Fri, 17 Apr 2015 17:01:24 +0200 [thread overview]
Message-ID: <55312044.90604@redhat.com> (raw)
In-Reply-To: <55311F3B.8060305@redhat.com>
On 17/04/2015 16:56, Paolo Bonzini wrote:
>
>
> On 17/04/2015 16:22, Daniel P. Berrange wrote:
>> A QOM property can be parsed as enum using the visit_type_enum()
>> helper method, but this forces callers to use the more complex
>> generic object_property_add() method when registering it. It
>> also requires that users of that object have access to the
>> string map when they want to read the property value.
>>
>> This patch introduces a specialized object_property_add_enum()
>> method which simplifies the use of enum properties, so the
>> setters/getters directly get passed the int value.
>>
>> typedef enum {
>> MYDEV_TYPE_FROG,
>> MYDEV_TYPE_ALLIGATOR,
>> MYDEV_TYPE_PLATYPUS,
>>
>> MYDEV_TYPE_LAST
>> } MyDevType;
>>
>> Then provide a table of enum <-> string mappings
>>
>> static const char *const mydevtypemap[MYDEV_TYPE_LAST + 1] = {
>> [MYDEV_TYPE_FROG] = "frog",
>> [MYDEV_TYPE_ALLIGATOR] = "alligator",
>> [MYDEV_TYPE_PLATYPUS] = "platypus",
>> [MYDEV_TYPE_LAST] = NULL,
>> };
>>
>> Assuming an object struct of
>>
>> typedef struct {
>> Object parent;
>> MyDevType devtype;
>> ...other fields...
>> } MyDev;
>>
>> The property can then be registered as follows:
>>
>> static int mydev_prop_get_devtype(Object *obj,
>> Error **errp G_GNUC_UNUSED)
>> {
>> MyDev *dev = MYDEV(obj);
>>
>> return dev->devtype;
>> }
>>
>> static void mydev_prop_set_devtype(Object *obj,
>> int value,
>> Error **errp G_GNUC_UNUSED)
>> {
>> MyDev *dev = MYDEV(obj);
>>
>> dev->endpoint = value;
>> }
>>
>> object_property_add_enum(obj, "devtype",
>> mydevtypemap,
>> mydev_prop_get_devtype,
>> mydev_prop_set_devtype,
>> NULL);
On second thought (after seeing patch 7), please add a property type
argument here. We lose introspection of enum property types otherwise.
It's possible to use macros so that 'MyEnum' gets translated to two
arguments '"MyEnum", MyEnum_lookup', but I don't think that's worth the
obfuscation.
Paolo
>> Note there is no need to check the range of 'value' in
>> the setter, because the string->enum conversion code will
>> have already done that and reported an error as required.
>>
>> Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
>> ---
>> include/qom/object.h | 17 ++++++++++++++++
>> qom/object.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>> 2 files changed, 74 insertions(+)
>>
>> diff --git a/include/qom/object.h b/include/qom/object.h
>> index dfdba2f..3462821 100644
>> --- a/include/qom/object.h
>> +++ b/include/qom/object.h
>> @@ -1262,6 +1262,23 @@ void object_property_add_bool(Object *obj, const char *name,
>> Error **errp);
>>
>> /**
>> + * object_property_add_enum:
>> + * @obj: the object to add a property to
>> + * @name: the name of the property
>> + * @get: the getter or NULL if the property is write-only.
>> + * @set: the setter or NULL if the property is read-only
>> + * @errp: if an error occurs, a pointer to an area to store the error
>> + *
>> + * Add a enum property using getters/setters. This function will add a
>> + * property of type 'enum'.
>> + */
>> +void object_property_add_enum(Object *obj, const char *name,
>> + const char * const *strings,
>> + int (*get)(Object *, Error **),
>> + void (*set)(Object *, int, Error **),
>> + Error **errp);
>> +
>> +/**
>> * object_property_add_tm:
>> * @obj: the object to add a property to
>> * @name: the name of the property
>> diff --git a/qom/object.c b/qom/object.c
>> index 2534398..543cc57 100644
>> --- a/qom/object.c
>> +++ b/qom/object.c
>> @@ -1607,6 +1607,63 @@ void object_property_add_bool(Object *obj, const char *name,
>> }
>> }
>>
>> +typedef struct EnumProperty {
>> + const char * const *strings;
>> + int (*get)(Object *, Error **);
>> + void (*set)(Object *, int, Error **);
>> +} EnumProperty;
>> +
>> +static void property_get_enum(Object *obj, Visitor *v, void *opaque,
>> + const char *name, Error **errp)
>> +{
>> + EnumProperty *prop = opaque;
>> + int value;
>> +
>> + value = prop->get(obj, errp);
>> + visit_type_enum(v, &value, prop->strings, NULL, name, errp);
>> +}
>> +
>> +static void property_set_enum(Object *obj, Visitor *v, void *opaque,
>> + const char *name, Error **errp)
>> +{
>> + EnumProperty *prop = opaque;
>> + int value;
>> +
>> + visit_type_enum(v, &value, prop->strings, NULL, name, errp);
>> + prop->set(obj, value, errp);
>> +}
>> +
>> +static void property_release_enum(Object *obj, const char *name,
>> + void *opaque)
>> +{
>> + EnumProperty *prop = opaque;
>> + g_free(prop);
>> +}
>> +
>> +void object_property_add_enum(Object *obj, const char *name,
>> + const char * const *strings,
>> + int (*get)(Object *, Error **),
>> + void (*set)(Object *, int, Error **),
>> + Error **errp)
>> +{
>> + Error *local_err = NULL;
>> + EnumProperty *prop = g_malloc0(sizeof(*prop));
>> +
>> + prop->strings = strings;
>> + prop->get = get;
>> + prop->set = set;
>> +
>> + object_property_add(obj, name, "enum",
>> + get ? property_get_enum : NULL,
>> + set ? property_set_enum : NULL,
>> + property_release_enum,
>> + prop, &local_err);
>> + if (local_err) {
>> + error_propagate(errp, local_err);
>> + g_free(prop);
>> + }
>> +}
>> +
>> typedef struct TMProperty {
>> void (*get)(Object *, struct tm *, Error **);
>> } TMProperty;
>>
>
> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>
>
>
next prev parent reply other threads:[~2015-04-17 15:01 UTC|newest]
Thread overview: 71+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-04-17 14:22 [Qemu-devel] [PATCH v1 RFC 00/34] Generic support for TLS protocol & I/O channels Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 01/34] ui: remove check for failure of qemu_acl_init() Daniel P. Berrange
2015-04-17 15:56 ` Eric Blake
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 02/34] qom: document user creatable object types in help text Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 03/34] qom: create objects in two phases Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 04/34] qom: add object_new_propv / object_new_proplist constructors Daniel P. Berrange
2015-04-17 14:55 ` Paolo Bonzini
2015-04-17 15:16 ` Daniel P. Berrange
2015-04-17 16:11 ` Eric Blake
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 05/34] qom: make enum string tables const-correct Daniel P. Berrange
2015-04-17 14:56 ` Paolo Bonzini
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 06/34] qom: add a object_property_add_enum helper method Daniel P. Berrange
2015-04-17 14:56 ` Paolo Bonzini
2015-04-17 15:01 ` Paolo Bonzini [this message]
2015-04-17 15:11 ` Daniel P. Berrange
2015-04-17 15:19 ` Paolo Bonzini
2015-04-17 15:22 ` Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 07/34] qom: don't pass string table to object_get_enum method Daniel P. Berrange
2015-04-17 15:05 ` Paolo Bonzini
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 08/34] crypto: introduce new module for computing hash digests Daniel P. Berrange
2015-05-13 17:04 ` Daniel P. Berrange
2015-05-13 17:12 ` Paolo Bonzini
2015-05-13 17:21 ` Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 09/34] crypto: move built-in AES implementation into crypto/ Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 10/34] crypto: move built-in D3DES " Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 11/34] crypto: introduce generic cipher API & built-in implementation Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 12/34] crypto: add a gcrypt cipher implementation Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 13/34] crypto: add a nettle " Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 14/34] crypto: introduce new module for handling TLS credentials Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 15/34] crypto: add sanity checking of " Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 16/34] crypto: introduce new module for handling TLS sessions Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 17/34] block: convert quorum blockdrv to use crypto APIs Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 18/34] ui: convert VNC websockets " Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 19/34] block: convert qcow/qcow2 to use generic cipher API Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 20/34] ui: convert VNC " Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 21/34] io: add abstract QIOChannel classes Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 22/34] io: add helper module for creating watches on UNIX FDs Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 23/34] io: add QIOChannelSocket class Daniel P. Berrange
2015-04-17 15:28 ` Paolo Bonzini
2015-04-17 15:52 ` Daniel P. Berrange
2015-04-17 16:00 ` Paolo Bonzini
2015-04-20 7:18 ` Gerd Hoffmann
2015-04-23 12:31 ` Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 24/34] io: add QIOChannelFile class Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 25/34] io: add QIOTask class for async operations Daniel P. Berrange
2015-04-17 15:16 ` Paolo Bonzini
2015-04-17 15:49 ` Daniel P. Berrange
2015-04-17 15:57 ` Paolo Bonzini
2015-04-17 16:11 ` Daniel P. Berrange
2015-04-17 17:06 ` Paolo Bonzini
2015-04-17 17:38 ` Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 26/34] io: add QIOChannelTLS class Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 27/34] io: pull Buffer code out of VNC module Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 28/34] io: add QIOChannelWebsock class Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 29/34] ui: convert VNC server to use QEMUIOChannelSocket classes Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 30/34] ui: convert VNC server to use QIOChannelTLS Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 31/34] ui: convert VNC server to use QIOChannelWebsock Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 32/34] char: convert from GIOChannel to QIOChannel Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 33/34] char: don't assume telnet initialization will not block Daniel P. Berrange
2015-04-17 14:22 ` [Qemu-devel] [PATCH v1 RFC 34/34] char: introduce support for TLS encrypted TCP chardev backend Daniel P. Berrange
2015-04-17 18:27 ` Eric Blake
2015-04-23 12:32 ` Daniel P. Berrange
2015-05-04 20:07 ` Kashyap Chamarthy
2015-05-05 13:49 ` Daniel P. Berrange
2015-05-05 13:53 ` Paolo Bonzini
2015-05-05 13:56 ` Daniel P. Berrange
2015-05-05 14:54 ` Kashyap Chamarthy
2015-05-06 8:34 ` Kashyap Chamarthy
2015-05-06 10:18 ` Daniel P. Berrange
2015-05-06 11:38 ` Kashyap Chamarthy
2015-04-23 12:28 ` [Qemu-devel] [PATCH v1 RFC 00/34] Generic support for TLS protocol & I/O channels Stefan Hajnoczi
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=55312044.90604@redhat.com \
--to=pbonzini@redhat.com \
--cc=berrange@redhat.com \
--cc=kraxel@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).