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 void 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 v
->start_struct(v
, name
, obj
, size
, &err
);
50 if (obj
&& (v
->type
& VISITOR_INPUT
)) {
51 assert(!err
!= !*obj
);
53 error_propagate(errp
, err
);
56 void visit_check_struct(Visitor
*v
, Error
**errp
)
58 trace_visit_check_struct(v
);
59 if (v
->check_struct
) {
60 v
->check_struct(v
, errp
);
64 void visit_end_struct(Visitor
*v
, void **obj
)
66 trace_visit_end_struct(v
, obj
);
67 v
->end_struct(v
, obj
);
70 void visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
71 size_t size
, Error
**errp
)
75 assert(!list
|| size
>= sizeof(GenericList
));
76 trace_visit_start_list(v
, name
, list
, size
);
77 v
->start_list(v
, name
, list
, size
, &err
);
78 if (list
&& (v
->type
& VISITOR_INPUT
)) {
79 assert(!(err
&& *list
));
81 error_propagate(errp
, err
);
84 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
86 assert(tail
&& size
>= sizeof(GenericList
));
87 trace_visit_next_list(v
, tail
, size
);
88 return v
->next_list(v
, tail
, size
);
91 void visit_check_list(Visitor
*v
, Error
**errp
)
93 trace_visit_check_list(v
);
95 v
->check_list(v
, errp
);
99 void visit_end_list(Visitor
*v
, void **obj
)
101 trace_visit_end_list(v
, obj
);
105 void visit_start_alternate(Visitor
*v
, const char *name
,
106 GenericAlternate
**obj
, size_t size
,
111 assert(obj
&& size
>= sizeof(GenericAlternate
));
112 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
113 trace_visit_start_alternate(v
, name
, obj
, size
);
114 if (v
->start_alternate
) {
115 v
->start_alternate(v
, name
, obj
, size
, &err
);
117 if (v
->type
& VISITOR_INPUT
) {
118 assert(v
->start_alternate
&& !err
!= !*obj
);
120 error_propagate(errp
, err
);
123 void visit_end_alternate(Visitor
*v
, void **obj
)
125 trace_visit_end_alternate(v
, obj
);
126 if (v
->end_alternate
) {
127 v
->end_alternate(v
, obj
);
131 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
133 trace_visit_optional(v
, name
, present
);
135 v
->optional(v
, name
, present
);
140 bool visit_is_input(Visitor
*v
)
142 return v
->type
== VISITOR_INPUT
;
145 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
148 trace_visit_type_int(v
, name
, obj
);
149 v
->type_int64(v
, name
, obj
, errp
);
152 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
153 uint64_t max
, const char *type
, Error
**errp
)
156 uint64_t value
= *obj
;
158 v
->type_uint64(v
, name
, &value
, &err
);
160 error_propagate(errp
, err
);
161 } else if (value
> max
) {
162 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
163 name
? name
: "null", type
);
169 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
174 trace_visit_type_uint8(v
, name
, obj
);
176 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
180 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
185 trace_visit_type_uint16(v
, name
, obj
);
187 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
191 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
196 trace_visit_type_uint32(v
, name
, obj
);
198 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
202 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
206 trace_visit_type_uint64(v
, name
, obj
);
207 v
->type_uint64(v
, name
, obj
, errp
);
210 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
211 int64_t min
, int64_t max
, const char *type
,
215 int64_t value
= *obj
;
217 v
->type_int64(v
, name
, &value
, &err
);
219 error_propagate(errp
, err
);
220 } else if (value
< min
|| value
> max
) {
221 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
222 name
? name
: "null", type
);
228 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
232 trace_visit_type_int8(v
, name
, obj
);
234 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
238 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
243 trace_visit_type_int16(v
, name
, obj
);
245 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
249 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
254 trace_visit_type_int32(v
, name
, obj
);
256 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
260 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
264 trace_visit_type_int64(v
, name
, obj
);
265 v
->type_int64(v
, name
, obj
, errp
);
268 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
272 trace_visit_type_size(v
, name
, obj
);
274 v
->type_size(v
, name
, obj
, errp
);
276 v
->type_uint64(v
, name
, obj
, errp
);
280 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
283 trace_visit_type_bool(v
, name
, obj
);
284 v
->type_bool(v
, name
, obj
, errp
);
287 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
292 /* TODO: Fix callers to not pass NULL when they mean "", so that we
294 assert(!(v->type & VISITOR_OUTPUT) || *obj);
296 trace_visit_type_str(v
, name
, obj
);
297 v
->type_str(v
, name
, obj
, &err
);
298 if (v
->type
& VISITOR_INPUT
) {
299 assert(!err
!= !*obj
);
301 error_propagate(errp
, err
);
304 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
308 trace_visit_type_number(v
, name
, obj
);
309 v
->type_number(v
, name
, obj
, errp
);
312 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
317 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
318 trace_visit_type_any(v
, name
, obj
);
319 v
->type_any(v
, name
, obj
, &err
);
320 if (v
->type
== VISITOR_INPUT
) {
321 assert(!err
!= !*obj
);
323 error_propagate(errp
, err
);
326 void visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
329 trace_visit_type_null(v
, name
, obj
);
330 v
->type_null(v
, name
, obj
, errp
);
333 static void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
334 const QEnumLookup
*lookup
, Error
**errp
)
340 * TODO why is this an error, not an assertion? If assertion:
341 * delete, and rely on qapi_enum_lookup()
343 if (value
< 0 || value
>= lookup
->size
) {
344 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
348 enum_str
= (char *)qapi_enum_lookup(lookup
, value
);
349 visit_type_str(v
, name
, &enum_str
, errp
);
352 static void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
353 const QEnumLookup
*lookup
, Error
**errp
)
355 Error
*local_err
= NULL
;
359 visit_type_str(v
, name
, &enum_str
, &local_err
);
361 error_propagate(errp
, local_err
);
365 value
= qapi_enum_parse(lookup
, enum_str
, -1, NULL
);
367 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
376 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
377 const QEnumLookup
*lookup
, Error
**errp
)
379 assert(obj
&& lookup
);
380 trace_visit_type_enum(v
, name
, obj
);
383 input_type_enum(v
, name
, obj
, lookup
, errp
);
386 output_type_enum(v
, name
, obj
, lookup
, errp
);
389 /* nothing further to do, scalar value was already copied by
390 * g_memdup() during visit_start_*() */
392 case VISITOR_DEALLOC
:
393 /* nothing to deallocate for a scalar */