From patchwork Wed Oct 26 21:18:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Blake X-Patchwork-Id: 9398343 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 3D506600BA for ; Wed, 26 Oct 2016 21:19:17 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 098FF29E0A for ; Wed, 26 Oct 2016 21:19:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id F295629E0E; Wed, 26 Oct 2016 21:19:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 9235629E0A for ; Wed, 26 Oct 2016 21:19:16 +0000 (UTC) Received: from localhost ([::1]:37441 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzVbP-0003w7-Nr for patchwork-qemu-devel@patchwork.kernel.org; Wed, 26 Oct 2016 17:19:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49844) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bzVb2-0003ut-I2 for qemu-devel@nongnu.org; Wed, 26 Oct 2016 17:18:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bzVb1-0006pg-Ft for qemu-devel@nongnu.org; Wed, 26 Oct 2016 17:18:52 -0400 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]:44334) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1bzVb1-0006pP-Aj for qemu-devel@nongnu.org; Wed, 26 Oct 2016 17:18:51 -0400 Received: from red.redhat.com (unknown [IPv6:2602:30a:c7d0:7100:7657:7b6:d81b:9143]) (Authenticated sender: eric@blake.one) by relay4-d.mail.gandi.net (Postfix) with ESMTPA id 3DCA0172097; Wed, 26 Oct 2016 23:18:49 +0200 (CEST) From: Eric Blake To: qemu-devel@nongnu.org Date: Wed, 26 Oct 2016 16:18:37 -0500 Message-Id: <1477516718-15439-2-git-send-email-eblake@redhat.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1477516358-15039-1-git-send-email-eblake@redhat.com> References: <1477516358-15039-1-git-send-email-eblake@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2001:4b98:c:538::196 Subject: [Qemu-devel] [PATCH v2 2/3] qapi: Further enhance visitor virtual walk doc example X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , armbru@redhat.com, ptoscano@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP Markus pointed out that the example given for virtual walks did not discuss how to do a virtual walk of an alternate type. It turns out that for output, we don't need to visit an alternate (just directly visit the type that we want); and for input, visit_start_alternate() is not currently wired up for alternates (it requires a QAPI type, rather than permitting NULL for a virtual walk). Also, the example was never updated in commit 3b098d5 about where visit_complete() would fit in. Improve the description and example to give more details along these lines. Signed-off-by: Eric Blake --- include/qapi/visitor.h | 41 ++++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h index 9bb6cba..0d9d74c 100644 --- a/include/qapi/visitor.h +++ b/include/qapi/visitor.h @@ -187,19 +187,25 @@ * * It is also possible to use the visitors to do a virtual walk, where * no actual QAPI struct is present. In this situation, decisions - * about what needs to be walked are made by the calling code, and - * structured visits are split between pairs of start and end methods - * (where the end method must be called if the start function - * succeeded, even if an intermediate visit encounters an error). - * Thus, a virtual walk corresponding to '{ "list": [1, 2] }' looks - * like: + * about what needs to be walked are made by the calling code (that + * is, there is no use for QAPI alternate types in a virtual walk, + * because the code can just directly visit the appropriate type + * within the alternate), and structured visits are split between + * pairs of start and end methods (where the end method must be called + * if the start function succeeded, even if an intermediate visit + * encounters an error). Thus, a virtual output walk of an object + * containing a list of alternates between an integer or nested + * object, corresponding to '{ "list": [1, { "value": "2" } ] }', + * would look like: * * * Visitor *v; * Error *err = NULL; - * int value; + * int i = 1; + * const char *s = "2"; + * FOO output; * - * v = FOO_visitor_new(...); + * v = FOO_visitor_new(..., &output); * visit_start_struct(v, NULL, NULL, 0, &err); * if (err) { * goto out; @@ -208,16 +214,21 @@ * if (err) { * goto outobj; * } - * value = 1; - * visit_type_int(v, NULL, &value, &err); + * visit_type_int(v, NULL, &i, &err); * if (err) { * goto outlist; * } - * value = 2; - * visit_type_int(v, NULL, &value, &err); + * visit_type_start(v, NULL, NULL, 0, &err); * if (err) { - * goto outlist; + * goto outnest; + * } + * visit_type_str(v, "value", (char **)&s, &err); + * if (err) { + * goto outnest; * } + * visit_check_struct(v, &err); + * outnest: + * visit_end_struct(v, NULL); * outlist: * visit_end_list(v, NULL); * if (!err) { @@ -225,6 +236,10 @@ * } * outobj: * visit_end_struct(v, NULL); + * if (!err) { + * visit_complete(v, &output); + * ...use output... + * } * out: * error_propagate(errp, err); * visit_free(v);