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/qerror.h"
19 #include "qapi/visitor.h"
20 #include "qapi/visitor-impl.h"
23 void visit_complete(Visitor
*v
, void *opaque
)
25 assert(v
->type
!= VISITOR_OUTPUT
|| v
->complete
);
26 trace_visit_complete(v
, opaque
);
28 v
->complete(v
, opaque
);
32 void visit_free(Visitor
*v
)
40 void visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
41 size_t size
, Error
**errp
)
45 trace_visit_start_struct(v
, name
, obj
, size
);
48 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
50 v
->start_struct(v
, name
, obj
, size
, &err
);
51 if (obj
&& (v
->type
& VISITOR_INPUT
)) {
52 assert(!err
!= !*obj
);
54 error_propagate(errp
, err
);
57 void visit_check_struct(Visitor
*v
, Error
**errp
)
59 trace_visit_check_struct(v
);
60 if (v
->check_struct
) {
61 v
->check_struct(v
, errp
);
65 void visit_end_struct(Visitor
*v
, void **obj
)
67 trace_visit_end_struct(v
, obj
);
68 v
->end_struct(v
, obj
);
71 void visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
72 size_t size
, Error
**errp
)
76 assert(!list
|| size
>= sizeof(GenericList
));
77 trace_visit_start_list(v
, name
, list
, size
);
78 v
->start_list(v
, name
, list
, size
, &err
);
79 if (list
&& (v
->type
& VISITOR_INPUT
)) {
80 assert(!(err
&& *list
));
82 error_propagate(errp
, err
);
85 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
87 assert(tail
&& size
>= sizeof(GenericList
));
88 trace_visit_next_list(v
, tail
, size
);
89 return v
->next_list(v
, tail
, size
);
92 void visit_check_list(Visitor
*v
, Error
**errp
)
94 trace_visit_check_list(v
);
96 v
->check_list(v
, errp
);
100 void visit_end_list(Visitor
*v
, void **obj
)
102 trace_visit_end_list(v
, obj
);
106 void visit_start_alternate(Visitor
*v
, const char *name
,
107 GenericAlternate
**obj
, size_t size
,
112 assert(obj
&& size
>= sizeof(GenericAlternate
));
113 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
114 trace_visit_start_alternate(v
, name
, obj
, size
);
115 if (v
->start_alternate
) {
116 v
->start_alternate(v
, name
, obj
, size
, &err
);
118 if (v
->type
& VISITOR_INPUT
) {
119 assert(v
->start_alternate
&& !err
!= !*obj
);
121 error_propagate(errp
, err
);
124 void visit_end_alternate(Visitor
*v
, void **obj
)
126 trace_visit_end_alternate(v
, obj
);
127 if (v
->end_alternate
) {
128 v
->end_alternate(v
, obj
);
132 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
134 trace_visit_optional(v
, name
, present
);
136 v
->optional(v
, name
, present
);
141 bool visit_is_input(Visitor
*v
)
143 return v
->type
== VISITOR_INPUT
;
146 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
149 trace_visit_type_int(v
, name
, obj
);
150 v
->type_int64(v
, name
, obj
, errp
);
153 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
154 uint64_t max
, const char *type
, Error
**errp
)
157 uint64_t value
= *obj
;
159 v
->type_uint64(v
, name
, &value
, &err
);
161 error_propagate(errp
, err
);
162 } else if (value
> max
) {
163 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
164 name
? name
: "null", type
);
170 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
175 trace_visit_type_uint8(v
, name
, obj
);
177 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
181 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
186 trace_visit_type_uint16(v
, name
, obj
);
188 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
192 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
197 trace_visit_type_uint32(v
, name
, obj
);
199 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
203 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
207 trace_visit_type_uint64(v
, name
, obj
);
208 v
->type_uint64(v
, name
, obj
, errp
);
211 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
212 int64_t min
, int64_t max
, const char *type
,
216 int64_t value
= *obj
;
218 v
->type_int64(v
, name
, &value
, &err
);
220 error_propagate(errp
, err
);
221 } else if (value
< min
|| value
> max
) {
222 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
223 name
? name
: "null", type
);
229 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
233 trace_visit_type_int8(v
, name
, obj
);
235 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
239 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
244 trace_visit_type_int16(v
, name
, obj
);
246 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
250 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
255 trace_visit_type_int32(v
, name
, obj
);
257 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
261 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
265 trace_visit_type_int64(v
, name
, obj
);
266 v
->type_int64(v
, name
, obj
, errp
);
269 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
273 trace_visit_type_size(v
, name
, obj
);
275 v
->type_size(v
, name
, obj
, errp
);
277 v
->type_uint64(v
, name
, obj
, errp
);
281 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
284 trace_visit_type_bool(v
, name
, obj
);
285 v
->type_bool(v
, name
, obj
, errp
);
288 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
293 /* TODO: Fix callers to not pass NULL when they mean "", so that we
295 assert(!(v->type & VISITOR_OUTPUT) || *obj);
297 trace_visit_type_str(v
, name
, obj
);
298 v
->type_str(v
, name
, obj
, &err
);
299 if (v
->type
& VISITOR_INPUT
) {
300 assert(!err
!= !*obj
);
302 error_propagate(errp
, err
);
305 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
309 trace_visit_type_number(v
, name
, obj
);
310 v
->type_number(v
, name
, obj
, errp
);
313 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
318 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
319 trace_visit_type_any(v
, name
, obj
);
320 v
->type_any(v
, name
, obj
, &err
);
321 if (v
->type
== VISITOR_INPUT
) {
322 assert(!err
!= !*obj
);
324 error_propagate(errp
, err
);
327 void visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
330 trace_visit_type_null(v
, name
, obj
);
331 v
->type_null(v
, name
, obj
, errp
);
334 static void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
335 const QEnumLookup
*lookup
, Error
**errp
)
341 * TODO why is this an error, not an assertion? If assertion:
342 * delete, and rely on qapi_enum_lookup()
344 if (value
< 0 || value
>= lookup
->size
) {
345 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
349 enum_str
= (char *)qapi_enum_lookup(lookup
, value
);
350 visit_type_str(v
, name
, &enum_str
, errp
);
353 static void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
354 const QEnumLookup
*lookup
, Error
**errp
)
356 Error
*local_err
= NULL
;
360 visit_type_str(v
, name
, &enum_str
, &local_err
);
362 error_propagate(errp
, local_err
);
366 value
= qapi_enum_parse(lookup
, enum_str
, -1, NULL
);
368 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
377 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
378 const QEnumLookup
*lookup
, Error
**errp
)
380 assert(obj
&& lookup
);
381 trace_visit_type_enum(v
, name
, obj
);
384 input_type_enum(v
, name
, obj
, lookup
, errp
);
387 output_type_enum(v
, name
, obj
, lookup
, errp
);
390 /* nothing further to do, scalar value was already copied by
391 * g_memdup() during visit_start_*() */
393 case VISITOR_DEALLOC
:
394 /* nothing to deallocate for a scalar */