aboutsummaryrefslogtreecommitdiff
path: root/tests/test-visitor-serialization.c
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 /tests/test-visitor-serialization.c
parent23d1705f425ca847f11d92e4363cd7f67f369fd8 (diff)
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 'tests/test-visitor-serialization.c')
-rw-r--r--tests/test-visitor-serialization.c22
1 files changed, 12 insertions, 10 deletions
diff --git a/tests/test-visitor-serialization.c b/tests/test-visitor-serialization.c
index 377ee3e788..dba4670762 100644
--- a/tests/test-visitor-serialization.c
+++ b/tests/test-visitor-serialization.c
@@ -1012,7 +1012,8 @@ static PrimitiveType pt_values[] = {
/* visitor-specific op implementations */
typedef struct QmpSerializeData {
- QmpOutputVisitor *qov;
+ Visitor *qov;
+ QObject *obj;
Visitor *qiv;
} QmpSerializeData;
@@ -1021,8 +1022,8 @@ static void qmp_serialize(void *native_in, void **datap,
{
QmpSerializeData *d = g_malloc0(sizeof(*d));
- d->qov = qmp_output_visitor_new();
- visit(qmp_output_get_visitor(d->qov), &native_in, errp);
+ d->qov = qmp_output_visitor_new(&d->obj);
+ visit(d->qov, &native_in, errp);
*datap = d;
}
@@ -1033,7 +1034,8 @@ static void qmp_deserialize(void **native_out, void *datap,
QString *output_json;
QObject *obj_orig, *obj;
- obj_orig = qmp_output_get_qobject(d->qov);
+ visit_complete(d->qov, &d->obj);
+ obj_orig = d->obj;
output_json = qobject_to_json(obj_orig);
obj = qobject_from_json(qstring_get_str(output_json));
@@ -1047,7 +1049,7 @@ static void qmp_deserialize(void **native_out, void *datap,
static void qmp_cleanup(void *datap)
{
QmpSerializeData *d = datap;
- visit_free(qmp_output_get_visitor(d->qov));
+ visit_free(d->qov);
visit_free(d->qiv);
g_free(d);
@@ -1055,7 +1057,7 @@ static void qmp_cleanup(void *datap)
typedef struct StringSerializeData {
char *string;
- StringOutputVisitor *sov;
+ Visitor *sov;
Visitor *siv;
} StringSerializeData;
@@ -1064,8 +1066,8 @@ static void string_serialize(void *native_in, void **datap,
{
StringSerializeData *d = g_malloc0(sizeof(*d));
- d->sov = string_output_visitor_new(false);
- visit(string_output_get_visitor(d->sov), &native_in, errp);
+ d->sov = string_output_visitor_new(false, &d->string);
+ visit(d->sov, &native_in, errp);
*datap = d;
}
@@ -1074,7 +1076,7 @@ static void string_deserialize(void **native_out, void *datap,
{
StringSerializeData *d = datap;
- d->string = string_output_get_string(d->sov);
+ visit_complete(d->sov, &d->string);
d->siv = string_input_visitor_new(d->string);
visit(d->siv, native_out, errp);
}
@@ -1083,7 +1085,7 @@ static void string_cleanup(void *datap)
{
StringSerializeData *d = datap;
- visit_free(string_output_get_visitor(d->sov));
+ visit_free(d->sov);
visit_free(d->siv);
g_free(d->string);
g_free(d);