[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 11/36] qapi: Add QAPI_TO_QOBJECT() convenience macro
From: |
Eric Blake |
Subject: |
[Qemu-devel] [PATCH 11/36] qapi: Add QAPI_TO_QOBJECT() convenience macro |
Date: |
Wed, 30 Nov 2016 13:44:29 -0600 |
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 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
- [Qemu-devel] [PATCH 03/36] qlist: Add convenience helpers for wrapped appends, (continued)
- [Qemu-devel] [PATCH 03/36] qlist: Add convenience helpers for wrapped appends, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 08/36] test-qga: Actually test 0xff sync bytes, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 10/36] qtest: Avoid dynamic JSON in libqtest, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 09/36] qtest: Add a new helper qmp_cmd() and friends, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 06/36] qobject-input-visitor: Avoid dynamic JSON in tests, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 04/36] qmp-event: Avoid dynamic JSON, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 05/36] qmp-dispatch: Avoid dynamic JSON, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 07/36] fdc-test: Avoid deprecated 'change' command, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 22/36] qtest: Avoid dynamic JSON in pc-cpu-test, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 12/36] nbd: Use simpler QAPI_TO_QOBJECT(), Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 11/36] qapi: Add QAPI_TO_QOBJECT() convenience macro,
Eric Blake <=
- [Qemu-devel] [PATCH 19/36] qtest: Change qmp_discard_response() to drop varargs, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 25/36] qtest: Change qmp_fd_send() to drop varags, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 13/36] nfs: Use simpler QAPI_TO_QOBJECT(), Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 16/36] qapi: Promote blockdev-change-medium arguments to QAPI type, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 14/36] qapi: Use simpler QAPI_TO_QOBJECT(), Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 20/36] qtest: Avoid dynamic JSON in device-introspect-test, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 26/36] qtest: Drop unused qtest_qmp_async(), Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 21/36] qtest: Avoid dynamic JSON in tmp105-test, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 28/36] qapi: Factor out JSON string escaping, Eric Blake, 2016/11/30
- [Qemu-devel] [PATCH 24/36] qtest: Drop unused qmp_fdv(), Eric Blake, 2016/11/30