qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [PATCH 1/5] qapi: Add QAPI_TO_QOBJECT() convenience macro


From: Eric Blake
Subject: [Qemu-devel] [PATCH 1/5] qapi: Add QAPI_TO_QOBJECT() convenience macro
Date: Mon, 17 Jul 2017 10:56:28 -0500

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 <address@hidden>
---
 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 via email to

[Prev in Thread] Current Thread [Next in Thread]