qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Eric Blake <eblake@redhat.com>
To: qemu-devel@nongnu.org
Cc: armbru@redhat.com, Michael Roth <mdroth@linux.vnet.ibm.com>
Subject: [Qemu-devel] [PATCH 1/5] qapi: Add QAPI_TO_QOBJECT() convenience macro
Date: Mon, 17 Jul 2017 10:56:28 -0500	[thread overview]
Message-ID: <20170717155632.12754-2-eblake@redhat.com> (raw)
In-Reply-To: <20170717155632.12754-1-eblake@redhat.com>

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 <eblake@redhat.com>
---
 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 e5a3490812..f6066ce537 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;

@@ -54,4 +56,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 70be84ccb5..0b1f098fa1 100644
--- a/qapi/qobject-output-visitor.c
+++ b/qapi/qobject-output-visitor.c
@@ -251,3 +251,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.13.3

  reply	other threads:[~2017-07-17 15:56 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-17 15:56 [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
2017-07-17 15:56 ` Eric Blake [this message]
2017-07-17 15:56 ` [Qemu-devel] [PATCH 2/5] nbd: Use simpler QAPI_TO_QOBJECT() Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 3/5] nfs: " Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 4/5] qapi: " Eric Blake
2017-07-17 15:56 ` [Qemu-devel] [PATCH 5/5] blockdev: " Eric Blake
2017-07-17 16:02 ` [Qemu-devel] [PATCH 0/5] Easier conversion from qapi to qobject Eric Blake
2017-07-18 15:52   ` 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=20170717155632.12754-2-eblake@redhat.com \
    --to=eblake@redhat.com \
    --cc=armbru@redhat.com \
    --cc=mdroth@linux.vnet.ibm.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).