]> xenbits.xensource.com Git - people/liuw/qemu.git/commitdiff
qapi: Shorter visits of optional fields
authorEric Blake <eblake@redhat.com>
Wed, 2 Dec 2015 05:20:53 +0000 (22:20 -0700)
committerMarkus Armbruster <armbru@redhat.com>
Thu, 17 Dec 2015 07:21:29 +0000 (08:21 +0100)
For less code, reflect the determined boolean value of an optional
visit back to the caller instead of making the caller read the
boolean after the fact.

The resulting generated code has the following diff:

|-    visit_optional(v, &has_fdset_id, "fdset-id");
|-    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;
|         }
|     }

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-Id: <1449033659-25497-10-git-send-email-eblake@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
include/qapi/visitor.h
qapi/qapi-visit-core.c
scripts/qapi.py

index 9be60d428dc55eaadd90f321c1c2de23fc556e6a..a14a16d755e0be2d3ce4eac41903e4b8f463def8 100644 (file)
@@ -41,9 +41,9 @@ void visit_end_list(Visitor *v, Error **errp);
  * Check if an optional member @name of an object needs visiting.
  * For input visitors, set *@present according to whether the
  * corresponding visit_type_*() needs calling; for other visitors,
- * leave *@present unchanged.
+ * leave *@present unchanged.  Return *@present for convenience.
  */
-void visit_optional(Visitor *v, bool *present, const char *name);
+bool visit_optional(Visitor *v, bool *present, const char *name);
 
 /**
  * Determine the qtype of the item @name in the current object visit.
index e07d6f962f8f8bbcbed7067a19aa6a3aa1156313..6d63e40234ad98b178976d3e4fb00f8deb7b7b3d 100644 (file)
@@ -73,11 +73,12 @@ void visit_end_union(Visitor *v, bool data_present, Error **errp)
     }
 }
 
-void visit_optional(Visitor *v, bool *present, const char *name)
+bool visit_optional(Visitor *v, bool *present, const char *name)
 {
     if (v->optional) {
         v->optional(v, present, name);
     }
+    return *present;
 }
 
 void visit_get_next_type(Visitor *v, QType *type, bool promote_int,
index 8bf41db4e2738899e8097744026e53a89c543d2b..58ecdf2a95def92b669182dc0623a3589eeda98e 100644 (file)
@@ -1656,8 +1656,7 @@ def gen_visit_fields(members, prefix='', need_cast=False, skiperr=False):
     for memb in members:
         if memb.optional:
             ret += mcgen('''
-    visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s");
-    if (%(prefix)shas_%(c_name)s) {
+    if (visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s")) {
 ''',
                          prefix=prefix, c_name=c_name(memb.name),
                          name=memb.name, errp=errparg)