aboutsummaryrefslogtreecommitdiff
path: root/include/qapi
diff options
context:
space:
mode:
authorEric Blake <eblake@redhat.com>2016-06-09 10:48:43 -0600
committerMarkus Armbruster <armbru@redhat.com>2016-07-06 10:52:04 +0200
commit3b098d56979d2f7fd707c5be85555d114353a28d (patch)
tree85f234e99b363f4417c0b4a09971ad63935031d9 /include/qapi
parent23d1705f425ca847f11d92e4363cd7f67f369fd8 (diff)
downloadqemu-3b098d56979d2f7fd707c5be85555d114353a28d.zip
qemu-3b098d56979d2f7fd707c5be85555d114353a28d.tar.gz
qemu-3b098d56979d2f7fd707c5be85555d114353a28d.tar.bz2
qapi: Add new visit_complete() function
Making each output visitor provide its own output collection function was the only remaining reason for exposing visitor sub-types to the rest of the code base. Add a polymorphic visit_complete() function which is a no-op for input visitors, and which populates an opaque pointer for output visitors. For maximum type-safety, also add a parameter to the output visitor constructors with a type-correct version of the output pointer, and assert that the two uses match. This approach was considered superior to either passing the output parameter only during construction (action at a distance during visit_free() feels awkward) or only during visit_complete() (defeating type safety makes it easier to use incorrectly). Most callers were function-local, and therefore a mechanical conversion; the testsuite was a bit trickier, but the previous cleanup patch minimized the churn here. The visit_complete() function may be called at most once; doing so lets us use transfer semantics rather than duplication or ref-count semantics to get the just-built output back to the caller, even though it means our behavior is not idempotent. Generated code is simplified as follows for events: |@@ -26,7 +26,7 @@ void qapi_event_send_acpi_device_ost(ACP | QDict *qmp; | Error *err = NULL; | QMPEventFuncEmit emit; |- QmpOutputVisitor *qov; |+ QObject *obj; | Visitor *v; | q_obj_ACPI_DEVICE_OST_arg param = { | info |@@ -39,8 +39,7 @@ void qapi_event_send_acpi_device_ost(ACP | | qmp = qmp_event_build_dict("ACPI_DEVICE_OST"); | |- qov = qmp_output_visitor_new(); |- v = qmp_output_get_visitor(qov); |+ v = qmp_output_visitor_new(&obj); | | visit_start_struct(v, "ACPI_DEVICE_OST", NULL, 0, &err); | if (err) { |@@ -55,7 +54,8 @@ void qapi_event_send_acpi_device_ost(ACP | goto out; | } | |- qdict_put_obj(qmp, "data", qmp_output_get_qobject(qov)); |+ visit_complete(v, &obj); |+ qdict_put_obj(qmp, "data", obj); | emit(QAPI_EVENT_ACPI_DEVICE_OST, qmp, &err); and for commands: | { | Error *err = NULL; |- QmpOutputVisitor *qov = qmp_output_visitor_new(); | Visitor *v; | |- v = qmp_output_get_visitor(qov); |+ v = qmp_output_visitor_new(ret_out); | visit_type_AddfdInfo(v, "unused", &ret_in, &err); |- if (err) { |- goto out; |+ if (!err) { |+ visit_complete(v, ret_out); | } |- *ret_out = qmp_output_get_qobject(qov); |- |-out: | error_propagate(errp, err); Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <1465490926-28625-13-git-send-email-eblake@redhat.com> Reviewed-by: Markus Armbruster <armbru@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
Diffstat (limited to 'include/qapi')
-rw-r--r--include/qapi/qmp-output-visitor.h11
-rw-r--r--include/qapi/string-output-visitor.h13
-rw-r--r--include/qapi/visitor-impl.h3
-rw-r--r--include/qapi/visitor.h50
4 files changed, 49 insertions, 28 deletions
diff --git a/include/qapi/qmp-output-visitor.h b/include/qapi/qmp-output-visitor.h
index 29c9a2e..040fdda 100644
--- a/include/qapi/qmp-output-visitor.h
+++ b/include/qapi/qmp-output-visitor.h
@@ -19,9 +19,12 @@
typedef struct QmpOutputVisitor QmpOutputVisitor;
-QmpOutputVisitor *qmp_output_visitor_new(void);
-
-QObject *qmp_output_get_qobject(QmpOutputVisitor *v);
-Visitor *qmp_output_get_visitor(QmpOutputVisitor *v);
+/*
+ * Create a new QMP output visitor.
+ *
+ * If everything else succeeds, pass @result to visit_complete() to
+ * collect the result of the visit.
+ */
+Visitor *qmp_output_visitor_new(QObject **result);
#endif
diff --git a/include/qapi/string-output-visitor.h b/include/qapi/string-output-visitor.h
index 03e377e..268dfe9 100644
--- a/include/qapi/string-output-visitor.h
+++ b/include/qapi/string-output-visitor.h
@@ -18,13 +18,18 @@
typedef struct StringOutputVisitor StringOutputVisitor;
/*
+ * Create a new string output visitor.
+ *
+ * Using @human creates output that is a bit easier for humans to read
+ * (for example, showing integer values in both decimal and hex).
+ *
+ * If everything else succeeds, pass @result to visit_complete() to
+ * collect the result of the visit.
+ *
* The string output visitor does not implement support for visiting
* QAPI structs, alternates, null, or arbitrary QTypes. It also
* requires a non-null list argument to visit_start_list().
*/
-StringOutputVisitor *string_output_visitor_new(bool human);
-
-char *string_output_get_string(StringOutputVisitor *v);
-Visitor *string_output_get_visitor(StringOutputVisitor *v);
+Visitor *string_output_visitor_new(bool human, char **result);
#endif
diff --git a/include/qapi/visitor-impl.h b/include/qapi/visitor-impl.h
index 525b068..16e0b86 100644
--- a/include/qapi/visitor-impl.h
+++ b/include/qapi/visitor-impl.h
@@ -105,6 +105,9 @@ struct Visitor
/* Must be set */
VisitorType type;
+ /* Must be set for output visitors, optional otherwise. */
+ void (*complete)(Visitor *v, void *opaque);
+
/* Must be set */
void (*free)(Visitor *v);
};
diff --git a/include/qapi/visitor.h b/include/qapi/visitor.h
index 2ded852..00a60ea 100644
--- a/include/qapi/visitor.h
+++ b/include/qapi/visitor.h
@@ -39,21 +39,15 @@
*
* All of the visitors are created via:
*
- * Type *subtype_visitor_new(parameters...);
- *
- * where Type is either directly 'Visitor *', or is a subtype that can
- * be trivially upcast to Visitor * via another function:
- *
- * Visitor *subtype_get_visitor(SubtypeVisitor *);
+ * Visitor *subtype_visitor_new(parameters...);
*
* A visitor should be used for exactly one top-level visit_type_FOO()
- * or virtual walk, then passed to visit_free() to clean up resources.
+ * or virtual walk; if that is successful, the caller can optionally
+ * call visit_complete() (for now, useful only for output visits, but
+ * safe to call on all visits). Then, regardless of success or
+ * failure, the user should call visit_free() to clean up resources.
* It is okay to free the visitor without completing the visit, if
- * some other error is detected in the meantime. Output visitors
- * provide an additional function, for collecting the final results of
- * a successful visit: string_output_get_string() and
- * qmp_output_get_qobject(); this collection function should not be
- * called if any errors were reported during the visit.
+ * some other error is detected in the meantime.
*
* All QAPI types have a corresponding function with a signature
* roughly compatible with this:
@@ -123,14 +117,14 @@
* Error *err = NULL;
* Visitor *v;
*
- * v = ...obtain input visitor...
+ * v = FOO_visitor_new(...);
* visit_type_Foo(v, NULL, &f, &err);
* if (err) {
* ...handle error...
* } else {
* ...use f...
* }
- * ...clean up v...
+ * visit_free(v);
* qapi_free_Foo(f);
* </example>
*
@@ -140,7 +134,7 @@
* Error *err = NULL;
* Visitor *v;
*
- * v = ...obtain input visitor...
+ * v = FOO_visitor_new(...);
* visit_type_FooList(v, NULL, &l, &err);
* if (err) {
* ...handle error...
@@ -149,7 +143,7 @@
* ...use l->value...
* }
* }
- * ...clean up v...
+ * visit_free(v);
* qapi_free_FooList(l);
* </example>
*
@@ -159,13 +153,17 @@
* Foo *f = ...obtain populated object...
* Error *err = NULL;
* Visitor *v;
+ * Type *result;
*
- * v = ...obtain output visitor...
+ * v = FOO_visitor_new(..., &result);
* visit_type_Foo(v, NULL, &f, &err);
* if (err) {
* ...handle error...
+ * } else {
+ * visit_complete(v, &result);
+ * ...use result...
* }
- * ...clean up v...
+ * visit_free(v);
* </example>
*
* When visiting a real QAPI struct, this file provides several
@@ -191,7 +189,7 @@
* Error *err = NULL;
* int value;
*
- * v = ...obtain visitor...
+ * v = FOO_visitor_new(...);
* visit_start_struct(v, NULL, NULL, 0, &err);
* if (err) {
* goto out;
@@ -219,7 +217,7 @@
* visit_end_struct(v, NULL);
* out:
* error_propagate(errp, err);
- * ...clean up v...
+ * visit_free(v);
* </example>
*/
@@ -243,6 +241,18 @@ typedef struct GenericAlternate {
/*** Visitor cleanup ***/
/*
+ * Complete the visit, collecting any output.
+ *
+ * May only be called only once after a successful top-level
+ * visit_type_FOO() or visit_end_ITEM(), and marks the end of the
+ * visit. The @opaque pointer should match the output parameter
+ * passed to the subtype_visitor_new() used to create an output
+ * visitor, or NULL for any other visitor. Needed for output
+ * visitors, but may also be called with other visitors.
+ */
+void visit_complete(Visitor *v, void *opaque);
+
+/*
* Free @v and any resources it has tied up.
*
* May be called whether or not the visit has been successfully