[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Qemu-devel] [PATCH v11 25/28] qapi: Simplify visits of optional fie
From: |
Eric Blake |
Subject: |
Re: [Qemu-devel] [PATCH v11 25/28] qapi: Simplify visits of optional fields |
Date: |
Thu, 12 Nov 2015 08:30:42 -0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 |
On 11/12/2015 08:11 AM, Markus Armbruster wrote:
> Eric Blake <address@hidden> writes:
>
>> None of the visitor callbacks would set an error when testing
>> if an optional field was present; make this part of the interface
>> contract by eliminating the errp argument. Then, for less code,
>> reflect the determined boolean value back to the caller instead
>> of making the caller read the boolean after the fact.
>>
>> The resulting generated code has a nice diff:
>>
>> |- visit_optional(v, &has_fdset_id, "fdset-id", &err);
>> |- if (err) {
>> |- goto out;
>> |- }
>> |- if (has_fdset_id) {
>> |+ if (visit_optional(v, &has_fdset_id, "fdset-id")) {
>> | visit_type_int(v, &fdset_id, "fdset-id", &err);
>> | if (err) {
>> | goto out;
>> | }
>> | }
>
> Any particular reason not to do
>
> has_fdset_id = visit_optional(v, "fdset-id");
> if (has_fdset_id) {
We can't. Output visitors do not implement visit_optional() callbacks,
but must rely on the incoming value of has_fdset_id. Which means
assigning to has_fdset_id without an incoming value will do the wrong
thing. Or worded differently, &has_fdset_id is modified as an output
parameter by input visitors, and read unchanged as an input parameter by
output visitors.
>> +++ b/qapi/qapi-visit-core.c
>> @@ -73,12 +73,12 @@ void visit_end_union(Visitor *v, bool data_present,
>> Error **errp)
>> }
>> }
>>
>> -void visit_optional(Visitor *v, bool *present, const char *name,
>> - Error **errp)
>> +bool visit_optional(Visitor *v, bool *present, const char *name)
>> {
>> if (v->optional) {
>> - v->optional(v, present, name, errp);
>> + v->optional(v, present, name);
>> }
>> + return *present;
>> }
>
> Slightly ugly: struct Visitor method optional returns void, but the
> wrapper returns bool.
I could make all the callbacks (all 3 of them: opts-visitor,
qmp-input-visitor, string-input-visitor) return bool, but didn't see the
point in the churn, especially since the contract of the return value is
easy to do in the wrapper.
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature
- [Qemu-devel] [PATCH v11 16/28] qapi: Detect collisions in C member names, (continued)
- [Qemu-devel] [PATCH v11 16/28] qapi: Detect collisions in C member names, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 20/28] qapi: Forbid case-insensitive clashes, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 17/28] cpu: Convert CpuInfo into flat union, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 24/28] qapi: Add positive tests to qapi-schema-test, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 26/28] qapi: Move duplicate member checks to schema check(), Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 25/28] qapi: Simplify visits of optional fields, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 22/28] qapi: Simplify visiting of alternate types, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 28/28] qapi: Detect base class loops, Eric Blake, 2015/11/11
- [Qemu-devel] [PATCH v11 23/28] qapi: Fix alternates that accept 'number' but not 'int', Eric Blake, 2015/11/11