Received: from eggs.gnu.org ([2001:4830:134:3::10]:35465) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cCAoi-0003p5-GQ for qemu-devel@nongnu.org; Wed, 30 Nov 2016 14:45:25 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cCAoh-0001sm-Gg for qemu-devel@nongnu.org; Wed, 30 Nov 2016 14:45:20 -0500 Received: from mx1.redhat.com ([209.132.183.28]:44462) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1cCAoh-0001sQ-BA for qemu-devel@nongnu.org; Wed, 30 Nov 2016 14:45:19 -0500 From: Eric Blake Date: Wed, 30 Nov 2016 13:44:29 -0600 Message-Id: <1480535094-23831-12-git-send-email-eblake@redhat.com> In-Reply-To: <1480535094-23831-1-git-send-email-eblake@redhat.com> References: <1480535094-23831-1-git-send-email-eblake@redhat.com> Subject: [Qemu-devel] [PATCH 11/36] qapi: Add QAPI_TO_QOBJECT() convenience macro List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: armbru@redhat.com, pbonzini@redhat.com, Michael Roth We have several callers that want to convert a QAPI C type into a QObject; right now all of them have to copy the same boilerplate of creating a visitor. A convenience macro makes this paradigm easier to type. Signed-off-by: Eric Blake --- include/qapi/qobject-output-visitor.h | 19 +++++++++++++++++++ qapi/qobject-output-visitor.c | 16 ++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/include/qapi/qobject-output-visitor.h b/include/qapi/qobject-output-visitor.h index 8241877..8ed5271 100644 --- a/include/qapi/qobject-output-visitor.h +++ b/include/qapi/qobject-output-visitor.h @@ -16,6 +16,8 @@ #include "qapi/visitor.h" #include "qapi/qmp/qobject.h" +#include "qapi/error.h" +#include "qapi-visit.h" typedef struct QObjectOutputVisitor QObjectOutputVisitor; @@ -27,4 +29,21 @@ typedef struct QObjectOutputVisitor QObjectOutputVisitor; */ Visitor *qobject_output_visitor_new(QObject **result); +QObject *qapi_to_qobject(const void *src, + void (*visit_type)(Visitor *, const char *, + void **, Error **), + Error **errp); + +/* + * Create a QObject from a QAPI object @src of the given @type. + * + * Not usable on QAPI scalars (integers, strings, enums), nor on a + * QAPI object that references the 'any' type. @src must not be NULL. + */ +#define QAPI_TO_QOBJECT(type, src, err) \ + (qapi_to_qobject(1 ? src : (type *)NULL, \ + (void (*)(Visitor *, const char *, void**, \ + Error **))visit_type_ ## type, \ + err)) + #endif diff --git a/qapi/qobject-output-visitor.c b/qapi/qobject-output-visitor.c index 8711270..6d7783c 100644 --- a/qapi/qobject-output-visitor.c +++ b/qapi/qobject-output-visitor.c @@ -252,3 +252,19 @@ Visitor *qobject_output_visitor_new(QObject **result) return &v->visitor; } + +QObject *qapi_to_qobject(const void *src, + void (*visit_type)(Visitor *, const char *, + void **, Error **), + Error **errp) +{ + Visitor *v; + void *s = (void *) src; /* cast away const */ + QObject *dst = NULL; + + v = qobject_output_visitor_new(&dst); + visit_type(v, NULL, &s, &error_abort); + visit_complete(v, &dst); + visit_free(v); + return dst; +} -- 2.7.4