2015-08-21 07:04:50 +00:00
|
|
|
/*
|
|
|
|
* Output Visitor
|
|
|
|
*
|
|
|
|
* Copyright IBM, Corp. 2011
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Anthony Liguori <aliguori@us.ibm.com>
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
|
|
|
|
* See the COPYING.LIB file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef QMP_OUTPUT_VISITOR_H
|
|
|
|
#define QMP_OUTPUT_VISITOR_H
|
|
|
|
|
|
|
|
#include "qapi/visitor.h"
|
|
|
|
#include "qapi/qmp/qobject.h"
|
|
|
|
|
2018-02-27 12:53:17 +00:00
|
|
|
typedef struct QObjectOutputVisitor QObjectOutputVisitor;
|
2015-08-21 07:04:50 +00:00
|
|
|
|
2018-03-02 17:24:19 +00:00
|
|
|
/**
|
|
|
|
* Create a QObject output visitor for @obj
|
|
|
|
*
|
|
|
|
* A QObject output visitor visit builds a QObject from QAPI Object.
|
|
|
|
* This simultaneously walks the QAPI object and the QObject being
|
|
|
|
* built. The latter walk starts at @obj.
|
|
|
|
*
|
|
|
|
* visit_type_FOO() creates a QObject for QAPI type FOO. It creates a
|
|
|
|
* QDict for struct/union types, a QList for list types, QString for
|
|
|
|
* type 'str' and enumeration types, QInt for integer types, QFloat
|
|
|
|
* for type 'number', QBool for type 'bool'. For type 'any', it
|
|
|
|
* increments the QObject's reference count. For QAPI alternate
|
|
|
|
* types, it creates the QObject for the member that is in use.
|
|
|
|
*
|
|
|
|
* visit_start_struct() ... visit_end_struct() visits a QAPI
|
|
|
|
* struct/union and creates a QDict. Visits in between visit the
|
|
|
|
* members. visit_optional() is true when the struct/union has this
|
|
|
|
* member. visit_check_struct() does nothing.
|
|
|
|
*
|
|
|
|
* visit_start_list() ... visit_end_list() visits a QAPI list and
|
|
|
|
* creates a QList. Visits in between visit list members, one after
|
|
|
|
* the other. visit_next_list() returns NULL when all QAPI list
|
|
|
|
* members have been visited. visit_check_list() does nothing.
|
|
|
|
*
|
|
|
|
* visit_start_alternate() ... visit_end_alternate() visits a QAPI
|
|
|
|
* alternate. The visit in between creates the QObject for the
|
|
|
|
* alternate member that is in use.
|
|
|
|
*
|
|
|
|
* Errors are not expected to happen.
|
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);
Backports commit 3b098d56979d2f7fd707c5be85555d114353a28d from qemu
2018-02-25 06:20:00 +00:00
|
|
|
*
|
2018-03-02 17:24:19 +00:00
|
|
|
* The caller is responsible for freeing the visitor with
|
|
|
|
* visit_free().
|
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);
Backports commit 3b098d56979d2f7fd707c5be85555d114353a28d from qemu
2018-02-25 06:20:00 +00:00
|
|
|
*/
|
2018-02-27 12:53:17 +00:00
|
|
|
Visitor *qobject_output_visitor_new(QObject **result);
|
2015-08-21 07:04:50 +00:00
|
|
|
|
|
|
|
#endif
|