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 "qapi/qmp/qerror.h"
18 #include "qapi/visitor.h"
19 #include "qapi/visitor-impl.h"
22 void visit_complete(Visitor
*v
, void *opaque
)
24 assert(v
->type
!= VISITOR_OUTPUT
|| v
->complete
);
25 trace_visit_complete(v
, opaque
);
27 v
->complete(v
, opaque
);
31 void visit_free(Visitor
*v
)
39 bool visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
40 size_t size
, Error
**errp
)
44 trace_visit_start_struct(v
, name
, obj
, size
);
47 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
49 ok
= v
->start_struct(v
, name
, obj
, size
, errp
);
50 if (obj
&& (v
->type
& VISITOR_INPUT
)) {
56 bool visit_check_struct(Visitor
*v
, Error
**errp
)
58 trace_visit_check_struct(v
);
59 return v
->check_struct
? v
->check_struct(v
, errp
) : true;
62 void visit_end_struct(Visitor
*v
, void **obj
)
64 trace_visit_end_struct(v
, obj
);
65 v
->end_struct(v
, obj
);
68 bool visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
69 size_t size
, Error
**errp
)
73 assert(!list
|| size
>= sizeof(GenericList
));
74 trace_visit_start_list(v
, name
, list
, size
);
75 ok
= v
->start_list(v
, name
, list
, size
, errp
);
76 if (list
&& (v
->type
& VISITOR_INPUT
)) {
82 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
84 assert(tail
&& size
>= sizeof(GenericList
));
85 trace_visit_next_list(v
, tail
, size
);
86 return v
->next_list(v
, tail
, size
);
89 bool visit_check_list(Visitor
*v
, Error
**errp
)
91 trace_visit_check_list(v
);
92 return v
->check_list
? v
->check_list(v
, errp
) : true;
95 void visit_end_list(Visitor
*v
, void **obj
)
97 trace_visit_end_list(v
, obj
);
101 bool visit_start_alternate(Visitor
*v
, const char *name
,
102 GenericAlternate
**obj
, size_t size
,
107 assert(obj
&& size
>= sizeof(GenericAlternate
));
108 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
109 trace_visit_start_alternate(v
, name
, obj
, size
);
110 if (!v
->start_alternate
) {
111 assert(!(v
->type
& VISITOR_INPUT
));
114 ok
= v
->start_alternate(v
, name
, obj
, size
, errp
);
115 if (v
->type
& VISITOR_INPUT
) {
121 void visit_end_alternate(Visitor
*v
, void **obj
)
123 trace_visit_end_alternate(v
, obj
);
124 if (v
->end_alternate
) {
125 v
->end_alternate(v
, obj
);
129 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
131 trace_visit_optional(v
, name
, present
);
133 v
->optional(v
, name
, present
);
138 bool visit_deprecated_accept(Visitor
*v
, const char *name
, Error
**errp
)
140 trace_visit_deprecated_accept(v
, name
);
141 if (v
->deprecated_accept
) {
142 return v
->deprecated_accept(v
, name
, errp
);
147 bool visit_deprecated(Visitor
*v
, const char *name
)
149 trace_visit_deprecated(v
, name
);
151 return v
->deprecated(v
, name
);
156 bool visit_is_input(Visitor
*v
)
158 return v
->type
== VISITOR_INPUT
;
161 bool visit_is_dealloc(Visitor
*v
)
163 return v
->type
== VISITOR_DEALLOC
;
166 bool visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
169 trace_visit_type_int(v
, name
, obj
);
170 return v
->type_int64(v
, name
, obj
, errp
);
173 static bool visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
174 uint64_t max
, const char *type
, Error
**errp
)
176 uint64_t value
= *obj
;
178 assert(v
->type
== VISITOR_INPUT
|| value
<= max
);
180 if (!v
->type_uint64(v
, name
, &value
, errp
)) {
184 assert(v
->type
== VISITOR_INPUT
);
185 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
186 name
? name
: "null", type
);
193 bool visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
199 trace_visit_type_uint8(v
, name
, obj
);
201 ok
= visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
206 bool visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
212 trace_visit_type_uint16(v
, name
, obj
);
214 ok
= visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
219 bool visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
225 trace_visit_type_uint32(v
, name
, obj
);
227 ok
= visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
232 bool visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
236 trace_visit_type_uint64(v
, name
, obj
);
237 return v
->type_uint64(v
, name
, obj
, errp
);
240 static bool visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
241 int64_t min
, int64_t max
, const char *type
,
244 int64_t value
= *obj
;
246 assert(v
->type
== VISITOR_INPUT
|| (value
>= min
&& value
<= max
));
248 if (!v
->type_int64(v
, name
, &value
, errp
)) {
251 if (value
< min
|| value
> max
) {
252 assert(v
->type
== VISITOR_INPUT
);
253 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
254 name
? name
: "null", type
);
261 bool visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
266 trace_visit_type_int8(v
, name
, obj
);
268 ok
= visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
273 bool visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
279 trace_visit_type_int16(v
, name
, obj
);
281 ok
= visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t",
287 bool visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
293 trace_visit_type_int32(v
, name
, obj
);
295 ok
= visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t",
301 bool visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
305 trace_visit_type_int64(v
, name
, obj
);
306 return v
->type_int64(v
, name
, obj
, errp
);
309 bool visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
313 trace_visit_type_size(v
, name
, obj
);
315 return v
->type_size(v
, name
, obj
, errp
);
317 return v
->type_uint64(v
, name
, obj
, errp
);
320 bool visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
323 trace_visit_type_bool(v
, name
, obj
);
324 return v
->type_bool(v
, name
, obj
, errp
);
327 bool visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
332 /* TODO: Fix callers to not pass NULL when they mean "", so that we
334 assert(!(v->type & VISITOR_OUTPUT) || *obj);
336 trace_visit_type_str(v
, name
, obj
);
337 ok
= v
->type_str(v
, name
, obj
, errp
);
338 if (v
->type
& VISITOR_INPUT
) {
344 bool visit_type_number(Visitor
*v
, const char *name
, double *obj
,
348 trace_visit_type_number(v
, name
, obj
);
349 return v
->type_number(v
, name
, obj
, errp
);
352 bool visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
357 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
358 trace_visit_type_any(v
, name
, obj
);
359 ok
= v
->type_any(v
, name
, obj
, errp
);
360 if (v
->type
== VISITOR_INPUT
) {
366 bool visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
369 trace_visit_type_null(v
, name
, obj
);
370 return v
->type_null(v
, name
, obj
, errp
);
373 static bool output_type_enum(Visitor
*v
, const char *name
, int *obj
,
374 const QEnumLookup
*lookup
, Error
**errp
)
379 enum_str
= (char *)qapi_enum_lookup(lookup
, value
);
380 return visit_type_str(v
, name
, &enum_str
, errp
);
383 static bool input_type_enum(Visitor
*v
, const char *name
, int *obj
,
384 const QEnumLookup
*lookup
, Error
**errp
)
389 if (!visit_type_str(v
, name
, &enum_str
, errp
)) {
393 value
= qapi_enum_parse(lookup
, enum_str
, -1, NULL
);
395 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
405 bool visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
406 const QEnumLookup
*lookup
, Error
**errp
)
408 assert(obj
&& lookup
);
409 trace_visit_type_enum(v
, name
, obj
);
412 return input_type_enum(v
, name
, obj
, lookup
, errp
);
414 return output_type_enum(v
, name
, obj
, lookup
, errp
);
416 /* nothing further to do, scalar value was already copied by
417 * g_memdup() during visit_start_*() */
419 case VISITOR_DEALLOC
:
420 /* nothing to deallocate for a scalar */