2 * Core Definitions for QAPI Visitor Classes
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
8 * Anthony Liguori <aliguori@us.ibm.com>
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu-common.h"
18 #include "qapi/qmp/qobject.h"
19 #include "qapi/qmp/qerror.h"
20 #include "qapi/visitor.h"
21 #include "qapi/visitor-impl.h"
24 void visit_complete(Visitor
*v
, void *opaque
)
26 assert(v
->type
!= VISITOR_OUTPUT
|| v
->complete
);
27 trace_visit_complete(v
, opaque
);
29 v
->complete(v
, opaque
);
33 void visit_free(Visitor
*v
)
41 void visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
42 size_t size
, Error
**errp
)
46 trace_visit_start_struct(v
, name
, obj
, size
);
49 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
51 v
->start_struct(v
, name
, obj
, size
, &err
);
52 if (obj
&& (v
->type
& VISITOR_INPUT
)) {
53 assert(!err
!= !*obj
);
55 error_propagate(errp
, err
);
58 void visit_check_struct(Visitor
*v
, Error
**errp
)
60 trace_visit_check_struct(v
);
61 if (v
->check_struct
) {
62 v
->check_struct(v
, errp
);
66 void visit_end_struct(Visitor
*v
, void **obj
)
68 trace_visit_end_struct(v
, obj
);
69 v
->end_struct(v
, obj
);
72 void visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
73 size_t size
, Error
**errp
)
77 assert(!list
|| size
>= sizeof(GenericList
));
78 trace_visit_start_list(v
, name
, list
, size
);
79 v
->start_list(v
, name
, list
, size
, &err
);
80 if (list
&& (v
->type
& VISITOR_INPUT
)) {
81 assert(!(err
&& *list
));
83 error_propagate(errp
, err
);
86 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
88 assert(tail
&& size
>= sizeof(GenericList
));
89 trace_visit_next_list(v
, tail
, size
);
90 return v
->next_list(v
, tail
, size
);
93 void visit_end_list(Visitor
*v
, void **obj
)
95 trace_visit_end_list(v
, obj
);
99 void visit_start_alternate(Visitor
*v
, const char *name
,
100 GenericAlternate
**obj
, size_t size
,
101 bool promote_int
, Error
**errp
)
105 assert(obj
&& size
>= sizeof(GenericAlternate
));
106 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
107 trace_visit_start_alternate(v
, name
, obj
, size
, promote_int
);
108 if (v
->start_alternate
) {
109 v
->start_alternate(v
, name
, obj
, size
, promote_int
, &err
);
111 if (v
->type
& VISITOR_INPUT
) {
112 assert(v
->start_alternate
&& !err
!= !*obj
);
114 error_propagate(errp
, err
);
117 void visit_end_alternate(Visitor
*v
, void **obj
)
119 trace_visit_end_alternate(v
, obj
);
120 if (v
->end_alternate
) {
121 v
->end_alternate(v
, obj
);
125 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
127 trace_visit_optional(v
, name
, present
);
129 v
->optional(v
, name
, present
);
134 bool visit_is_input(Visitor
*v
)
136 return v
->type
== VISITOR_INPUT
;
139 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
142 trace_visit_type_int(v
, name
, obj
);
143 v
->type_int64(v
, name
, obj
, errp
);
146 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
147 uint64_t max
, const char *type
, Error
**errp
)
150 uint64_t value
= *obj
;
152 v
->type_uint64(v
, name
, &value
, &err
);
154 error_propagate(errp
, err
);
155 } else if (value
> max
) {
156 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
157 name
? name
: "null", type
);
163 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
168 trace_visit_type_uint8(v
, name
, obj
);
170 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
174 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
179 trace_visit_type_uint16(v
, name
, obj
);
181 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
185 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
190 trace_visit_type_uint32(v
, name
, obj
);
192 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
196 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
200 trace_visit_type_uint64(v
, name
, obj
);
201 v
->type_uint64(v
, name
, obj
, errp
);
204 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
205 int64_t min
, int64_t max
, const char *type
,
209 int64_t value
= *obj
;
211 v
->type_int64(v
, name
, &value
, &err
);
213 error_propagate(errp
, err
);
214 } else if (value
< min
|| value
> max
) {
215 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
216 name
? name
: "null", type
);
222 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
226 trace_visit_type_int8(v
, name
, obj
);
228 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
232 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
237 trace_visit_type_int16(v
, name
, obj
);
239 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
243 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
248 trace_visit_type_int32(v
, name
, obj
);
250 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
254 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
258 trace_visit_type_int64(v
, name
, obj
);
259 v
->type_int64(v
, name
, obj
, errp
);
262 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
266 trace_visit_type_size(v
, name
, obj
);
268 v
->type_size(v
, name
, obj
, errp
);
270 v
->type_uint64(v
, name
, obj
, errp
);
274 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
277 trace_visit_type_bool(v
, name
, obj
);
278 v
->type_bool(v
, name
, obj
, errp
);
281 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
286 /* TODO: Fix callers to not pass NULL when they mean "", so that we
288 assert(!(v->type & VISITOR_OUTPUT) || *obj);
290 trace_visit_type_str(v
, name
, obj
);
291 v
->type_str(v
, name
, obj
, &err
);
292 if (v
->type
& VISITOR_INPUT
) {
293 assert(!err
!= !*obj
);
295 error_propagate(errp
, err
);
298 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
302 trace_visit_type_number(v
, name
, obj
);
303 v
->type_number(v
, name
, obj
, errp
);
306 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
311 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
312 trace_visit_type_any(v
, name
, obj
);
313 v
->type_any(v
, name
, obj
, &err
);
314 if (v
->type
== VISITOR_INPUT
) {
315 assert(!err
!= !*obj
);
317 error_propagate(errp
, err
);
320 void visit_type_null(Visitor
*v
, const char *name
, Error
**errp
)
322 trace_visit_type_null(v
, name
);
323 v
->type_null(v
, name
, errp
);
326 static void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
327 const char *const strings
[], Error
**errp
)
333 while (strings
[i
++] != NULL
);
334 if (value
< 0 || value
>= i
- 1) {
335 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
339 enum_str
= (char *)strings
[value
];
340 visit_type_str(v
, name
, &enum_str
, errp
);
343 static void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
344 const char *const strings
[], Error
**errp
)
346 Error
*local_err
= NULL
;
350 visit_type_str(v
, name
, &enum_str
, &local_err
);
352 error_propagate(errp
, local_err
);
356 while (strings
[value
] != NULL
) {
357 if (strcmp(strings
[value
], enum_str
) == 0) {
363 if (strings
[value
] == NULL
) {
364 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
373 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
374 const char *const strings
[], Error
**errp
)
376 assert(obj
&& strings
);
377 trace_visit_type_enum(v
, name
, obj
);
380 input_type_enum(v
, name
, obj
, strings
, errp
);
383 output_type_enum(v
, name
, obj
, strings
, errp
);
386 /* nothing further to do, scalar value was already copied by
387 * g_memdup() during visit_start_*() */
389 case VISITOR_DEALLOC
:
390 /* nothing to deallocate for a scalar */