qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Daniel P. Berrange" <berrange@redhat.com>
To: qemu-devel@nongnu.org
Cc: "Paolo Bonzini" <pbonzini@redhat.com>,
	"Andreas Färber" <afaerber@suse.de>
Subject: Re: [Qemu-devel] [PATCH v2 6/7] qom: add a object_property_add_enum helper method
Date: Fri, 1 May 2015 11:28:49 +0100	[thread overview]
Message-ID: <20150501102849.GB11559@redhat.com> (raw)
In-Reply-To: <1430408798-20914-7-git-send-email-berrange@redhat.com>

On Thu, Apr 30, 2015 at 04:46:37PM +0100, 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, "MyDevType",
>                             mydev_prop_get_devtype,
>                             mydev_prop_set_devtype,
>                             NULL);
> 
> 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       | 19 ++++++++++++
>  qom/object.c               | 58 ++++++++++++++++++++++++++++++++++++
>  tests/check-qom-proplist.c | 74 ++++++++++++++++++++++++++++++++++++++++++++++
>  3 files changed, 151 insertions(+)


> diff --git a/tests/check-qom-proplist.c b/tests/check-qom-proplist.c
> index 9a02e4d..15f4933 100644
> --- a/tests/check-qom-proplist.c
> +++ b/tests/check-qom-proplist.c
> +static void test_dummy_badenum(void)
> +{
> +    Error *err = NULL;
> +    DummyObject *dobj = DUMMY_OBJECT(
> +        object_new_propv(TYPE_DUMMY,
> +                         "/objects",
> +                         "dummy0",
> +                         &err,
> +                         "bv", "yes",
> +                         "sv", "Hiss hiss hiss",
> +                         "av", "yeti",
> +                         NULL));

Opps, this won't compile as I forgot to update the 2nd arg. Will
send a v3

Regards,
Daniel
-- 
|: http://berrange.com      -o-    http://www.flickr.com/photos/dberrange/ :|
|: http://libvirt.org              -o-             http://virt-manager.org :|
|: http://autobuild.org       -o-         http://search.cpan.org/~danberr/ :|
|: http://entangle-photo.org       -o-       http://live.gnome.org/gtk-vnc :|

  reply	other threads:[~2015-05-01 10:28 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-30 15:46 [Qemu-devel] [PATCH v2 0/7] qom: misc fixes & enhancements to support TLS work Daniel P. Berrange
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 1/7] qom: fix typename of 'policy' enum property in hostmem obj Daniel P. Berrange
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 2/7] qom: document user creatable object types in help text Daniel P. Berrange
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 3/7] qom: create objects in two phases Daniel P. Berrange
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 4/7] qom: add object_new_propv / object_new_proplist constructors Daniel P. Berrange
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 5/7] qom: make enum string tables const-correct Daniel P. Berrange
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 6/7] qom: add a object_property_add_enum helper method Daniel P. Berrange
2015-05-01 10:28   ` Daniel P. Berrange [this message]
2015-04-30 15:46 ` [Qemu-devel] [PATCH v2 7/7] qom: don't pass string table to object_get_enum method Daniel P. Berrange

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=20150501102849.GB11559@redhat.com \
    --to=berrange@redhat.com \
    --cc=afaerber@suse.de \
    --cc=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).