All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Cc: pbonzini@redhat.com, berrange@redhat.com, qemu-devel@nongnu.org,
	ehabkost@redhat.com
Subject: Re: [PATCH v2 04/18] qom: Simplify object_property_get_enum()
Date: Wed, 06 May 2020 09:07:55 +0200	[thread overview]
Message-ID: <87mu6l4k38.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <fb6ea8af-ca83-661b-d708-4648acb3afc5@redhat.com> ("Philippe Mathieu-Daudé"'s message of "Tue, 5 May 2020 17:59:34 +0200")

Philippe Mathieu-Daudé <philmd@redhat.com> writes:

> On 5/5/20 5:29 PM, Markus Armbruster wrote:
>> Reuse object_property_get_str().  Switches from the string to the
>> qobject visitor under the hood.
>>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>>   qom/object.c | 11 ++---------
>>   1 file changed, 2 insertions(+), 9 deletions(-)
>>
>> diff --git a/qom/object.c b/qom/object.c
>> index 3d65658059..b374af302c 100644
>> --- a/qom/object.c
>> +++ b/qom/object.c
>> @@ -1521,8 +1521,6 @@ typedef struct EnumProperty {
>>   int object_property_get_enum(Object *obj, const char *name,
>>                                const char *typename, Error **errp)
>>   {
>> -    Error *err = NULL;
>> -    Visitor *v;
>>       char *str;
>>       int ret;
>>       ObjectProperty *prop = object_property_find(obj, name, errp);
>> @@ -1541,15 +1539,10 @@ int object_property_get_enum(Object *obj, const char *name,
>>         enumprop = prop->opaque;
>>   -    v = string_output_visitor_new(false, &str);
>> -    object_property_get(obj, v, name, &err);
>> -    if (err) {
>> -        error_propagate(errp, err);
>> -        visit_free(v);
>> +    str = object_property_get_str(obj, name, errp);
>> +    if (!str) {
>
> Patch looks good but I'm not confident enough to add a R-b tag :)

Teaching opportunity!

>>           return 0;
>>       }
>> -    visit_complete(v, &str);
>> -    visit_free(v);
>>         ret = qapi_enum_parse(enumprop->lookup, str, -1, errp);
>>       g_free(str);
>>

The core function for getting properties is object_property_get().  To
be used like this:

        v = ... new output visitor of your choice ...
        object_property_get(obj, v, name, &err);
        if (!err) {
            visit_complete(v, &ret);
        }
        visit_free(v);

Delivers the result in @ret and @err.

The type of @ret depends on the visitor.  It typically needs to be
converted to the appropriate C type.

Life's too short to write that much code every time you want to get a
property value.  So we provide two levels of common helpers.

Level 1: the output visitor commonly used is the QObject output
visitor.  Combining object_property_get() with it yields

    QObject *object_property_get_qobject(Object *obj, const char *name,
                                         Error **errp)
    {
        QObject *ret = NULL;
        Error *local_err = NULL;
        Visitor *v;

        v = qobject_output_visitor_new(&ret);
        object_property_get(obj, v, name, &local_err);
        if (!local_err) {
            visit_complete(v, &ret);
        }
        error_propagate(errp, local_err);
        visit_free(v);
        return ret;
    }

The use I showed above becomes

    ret = object_property_get_qobject(obj, name, &err);

Again, result is in @ret and @err.  You commonly need to convert @ret
from QObject to the property's C type, and handle conversion errors.

Still too much code, so we provide convenience functions for common
types.  Here's the one for strings:

    char *object_property_get_str(Object *obj, const char *name,
                                  Error **errp)
    {
        QObject *ret = object_property_get_qobject(obj, name, errp);
        char *retval;

        if (!ret) {
            return NULL;
        }

        retval = g_strdup(qobject_get_try_str(ret));
        if (!retval) {
            error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
        }

        qobject_unref(ret);
        return retval;
    }

Now back to my patch.  Before the patch, object_property_get_enum() is
odd: it uses the string output visitor.  Works, but why do it by hand
when we can simply reuse existing object_property_get_str()?  All we
need is the (checked) conversion from string to enum.

Clearer now?

Bonus: one fewer use of a string visitor.  These need to die, but that's
another story.



  reply	other threads:[~2020-05-06  7:09 UTC|newest]

Thread overview: 38+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-05 15:29 [PATCH v2 00/18] qom: Spring cleaning Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 01/18] qom: Clearer reference counting in object_initialize_childv() Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 02/18] qom: Clean up inconsistent use of gchar * vs. char * Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 03/18] qom: Drop object_property_del_child()'s unused parameter @errp Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 04/18] qom: Simplify object_property_get_enum() Markus Armbruster
2020-05-05 15:59   ` Philippe Mathieu-Daudé
2020-05-06  7:07     ` Markus Armbruster [this message]
2020-05-05 16:36   ` Paolo Bonzini
2020-05-05 15:29 ` [PATCH v2 05/18] qom: Drop convenience method object_property_get_uint16List() Markus Armbruster
2020-05-05 16:42   ` Paolo Bonzini
2020-05-06  7:25     ` Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 06/18] qom: Make all the object_property_add_FOO() return the property Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 07/18] qom: Drop object_property_set_description() parameter @errp Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 08/18] tests/check-qom-proplist: Improve iterator coverage Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 09/18] s390x/cpumodel: Fix UI to CPU features pcc-cmac-{aes, eaes}-256 Markus Armbruster
2020-05-06 11:31   ` [PATCH v2 09/18] s390x/cpumodel: Fix UI to CPU features pcc-cmac-{aes,eaes}-256 Cornelia Huck
2020-05-08 12:15     ` [PATCH v2 09/18] s390x/cpumodel: Fix UI to CPU features pcc-cmac-{aes, eaes}-256 Markus Armbruster
2020-05-06 11:41   ` [PATCH v2 09/18] s390x/cpumodel: Fix UI to CPU features pcc-cmac-{aes,eaes}-256 David Hildenbrand
2020-05-08 12:00     ` [PATCH v2 09/18] s390x/cpumodel: Fix UI to CPU features pcc-cmac-{aes, eaes}-256 Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 10/18] hw/isa/superio: Make the components QOM children Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 11/18] e1000: Don't run e1000_instance_init() twice Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 12/18] hw/arm/bcm2835: Drop futile attempts at QOM-adopting memory Markus Armbruster
2020-05-05 15:29   ` Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 13/18] qdev: Clean up qdev_connect_gpio_out_named() Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 14/18] qom: Drop parameter @errp of object_property_add() & friends Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 15/18] Drop more @errp parameters after previous commit Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 16/18] qdev: Unrealize must not fail Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 17/18] spapr_pci: Drop some dead error handling Markus Armbruster
2020-05-05 15:29 ` [PATCH v2 18/18] qom: Drop @errp parameter of object_property_del() Markus Armbruster
2020-05-05 16:00   ` Philippe Mathieu-Daudé
2020-05-05 18:32 ` [PATCH v2 00/18] qom: Spring cleaning no-reply
2020-05-05 20:21 ` no-reply
2020-05-05 21:35 ` no-reply
2020-05-05 23:25 ` no-reply
2020-05-06  0:58 ` no-reply
2020-05-06  1:31 ` no-reply
2020-05-08 12:35 ` Markus Armbruster
2020-05-15  5:55   ` Markus Armbruster

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=87mu6l4k38.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.