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_is_input(Visitor
*v
)
140 return v
->type
== VISITOR_INPUT
;
143 bool visit_is_dealloc(Visitor
*v
)
145 return v
->type
== VISITOR_DEALLOC
;
148 bool visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
151 trace_visit_type_int(v
, name
, obj
);
152 return v
->type_int64(v
, name
, obj
, errp
);
155 static bool visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
156 uint64_t max
, const char *type
, Error
**errp
)
158 uint64_t value
= *obj
;
160 assert(v
->type
== VISITOR_INPUT
|| value
<= max
);
162 if (!v
->type_uint64(v
, name
, &value
, errp
)) {
166 assert(v
->type
== VISITOR_INPUT
);
167 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
168 name
? name
: "null", type
);
175 bool visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
181 trace_visit_type_uint8(v
, name
, obj
);
183 ok
= visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
188 bool visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
194 trace_visit_type_uint16(v
, name
, obj
);
196 ok
= visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
201 bool visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
207 trace_visit_type_uint32(v
, name
, obj
);
209 ok
= visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
214 bool visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
218 trace_visit_type_uint64(v
, name
, obj
);
219 return v
->type_uint64(v
, name
, obj
, errp
);
222 static bool visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
223 int64_t min
, int64_t max
, const char *type
,
226 int64_t value
= *obj
;
228 assert(v
->type
== VISITOR_INPUT
|| (value
>= min
&& value
<= max
));
230 if (!v
->type_int64(v
, name
, &value
, errp
)) {
233 if (value
< min
|| value
> max
) {
234 assert(v
->type
== VISITOR_INPUT
);
235 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
236 name
? name
: "null", type
);
243 bool visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
248 trace_visit_type_int8(v
, name
, obj
);
250 ok
= visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
255 bool visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
261 trace_visit_type_int16(v
, name
, obj
);
263 ok
= visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t",
269 bool visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
275 trace_visit_type_int32(v
, name
, obj
);
277 ok
= visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t",
283 bool visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
287 trace_visit_type_int64(v
, name
, obj
);
288 return v
->type_int64(v
, name
, obj
, errp
);
291 bool visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
295 trace_visit_type_size(v
, name
, obj
);
297 return v
->type_size(v
, name
, obj
, errp
);
299 return v
->type_uint64(v
, name
, obj
, errp
);
302 bool visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
305 trace_visit_type_bool(v
, name
, obj
);
306 return v
->type_bool(v
, name
, obj
, errp
);
309 bool visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
314 /* TODO: Fix callers to not pass NULL when they mean "", so that we
316 assert(!(v->type & VISITOR_OUTPUT) || *obj);
318 trace_visit_type_str(v
, name
, obj
);
319 ok
= v
->type_str(v
, name
, obj
, errp
);
320 if (v
->type
& VISITOR_INPUT
) {
326 bool visit_type_number(Visitor
*v
, const char *name
, double *obj
,
330 trace_visit_type_number(v
, name
, obj
);
331 return v
->type_number(v
, name
, obj
, errp
);
334 bool visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
339 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
340 trace_visit_type_any(v
, name
, obj
);
341 ok
= v
->type_any(v
, name
, obj
, errp
);
342 if (v
->type
== VISITOR_INPUT
) {
348 bool visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
351 trace_visit_type_null(v
, name
, obj
);
352 return v
->type_null(v
, name
, obj
, errp
);
355 static bool output_type_enum(Visitor
*v
, const char *name
, int *obj
,
356 const QEnumLookup
*lookup
, Error
**errp
)
361 enum_str
= (char *)qapi_enum_lookup(lookup
, value
);
362 return visit_type_str(v
, name
, &enum_str
, errp
);
365 static bool input_type_enum(Visitor
*v
, const char *name
, int *obj
,
366 const QEnumLookup
*lookup
, Error
**errp
)
371 if (!visit_type_str(v
, name
, &enum_str
, errp
)) {
375 value
= qapi_enum_parse(lookup
, enum_str
, -1, NULL
);
377 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
387 bool visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
388 const QEnumLookup
*lookup
, Error
**errp
)
390 assert(obj
&& lookup
);
391 trace_visit_type_enum(v
, name
, obj
);
394 return input_type_enum(v
, name
, obj
, lookup
, errp
);
396 return output_type_enum(v
, name
, obj
, lookup
, errp
);
398 /* nothing further to do, scalar value was already copied by
399 * g_memdup() during visit_start_*() */
401 case VISITOR_DEALLOC
:
402 /* nothing to deallocate for a scalar */