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_check_list(Visitor
*v
, Error
**errp
)
95 trace_visit_check_list(v
);
97 v
->check_list(v
, errp
);
101 void visit_end_list(Visitor
*v
, void **obj
)
103 trace_visit_end_list(v
, obj
);
107 void visit_start_alternate(Visitor
*v
, const char *name
,
108 GenericAlternate
**obj
, size_t size
,
113 assert(obj
&& size
>= sizeof(GenericAlternate
));
114 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
115 trace_visit_start_alternate(v
, name
, obj
, size
);
116 if (v
->start_alternate
) {
117 v
->start_alternate(v
, name
, obj
, size
, &err
);
119 if (v
->type
& VISITOR_INPUT
) {
120 assert(v
->start_alternate
&& !err
!= !*obj
);
122 error_propagate(errp
, err
);
125 void visit_end_alternate(Visitor
*v
, void **obj
)
127 trace_visit_end_alternate(v
, obj
);
128 if (v
->end_alternate
) {
129 v
->end_alternate(v
, obj
);
133 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
135 trace_visit_optional(v
, name
, present
);
137 v
->optional(v
, name
, present
);
142 bool visit_is_input(Visitor
*v
)
144 return v
->type
== VISITOR_INPUT
;
147 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
150 trace_visit_type_int(v
, name
, obj
);
151 v
->type_int64(v
, name
, obj
, errp
);
154 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
155 uint64_t max
, const char *type
, Error
**errp
)
158 uint64_t value
= *obj
;
160 v
->type_uint64(v
, name
, &value
, &err
);
162 error_propagate(errp
, err
);
163 } else if (value
> max
) {
164 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
165 name
? name
: "null", type
);
171 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
176 trace_visit_type_uint8(v
, name
, obj
);
178 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
182 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
187 trace_visit_type_uint16(v
, name
, obj
);
189 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
193 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
198 trace_visit_type_uint32(v
, name
, obj
);
200 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
204 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
208 trace_visit_type_uint64(v
, name
, obj
);
209 v
->type_uint64(v
, name
, obj
, errp
);
212 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
213 int64_t min
, int64_t max
, const char *type
,
217 int64_t value
= *obj
;
219 v
->type_int64(v
, name
, &value
, &err
);
221 error_propagate(errp
, err
);
222 } else if (value
< min
|| value
> max
) {
223 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
224 name
? name
: "null", type
);
230 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
234 trace_visit_type_int8(v
, name
, obj
);
236 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
240 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
245 trace_visit_type_int16(v
, name
, obj
);
247 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
251 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
256 trace_visit_type_int32(v
, name
, obj
);
258 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
262 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
266 trace_visit_type_int64(v
, name
, obj
);
267 v
->type_int64(v
, name
, obj
, errp
);
270 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
274 trace_visit_type_size(v
, name
, obj
);
276 v
->type_size(v
, name
, obj
, errp
);
278 v
->type_uint64(v
, name
, obj
, errp
);
282 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
285 trace_visit_type_bool(v
, name
, obj
);
286 v
->type_bool(v
, name
, obj
, errp
);
289 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
294 /* TODO: Fix callers to not pass NULL when they mean "", so that we
296 assert(!(v->type & VISITOR_OUTPUT) || *obj);
298 trace_visit_type_str(v
, name
, obj
);
299 v
->type_str(v
, name
, obj
, &err
);
300 if (v
->type
& VISITOR_INPUT
) {
301 assert(!err
!= !*obj
);
303 error_propagate(errp
, err
);
306 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
310 trace_visit_type_number(v
, name
, obj
);
311 v
->type_number(v
, name
, obj
, errp
);
314 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
319 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
320 trace_visit_type_any(v
, name
, obj
);
321 v
->type_any(v
, name
, obj
, &err
);
322 if (v
->type
== VISITOR_INPUT
) {
323 assert(!err
!= !*obj
);
325 error_propagate(errp
, err
);
328 void visit_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
331 trace_visit_type_null(v
, name
, obj
);
332 v
->type_null(v
, name
, obj
, errp
);
335 static void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
336 const char *const strings
[], Error
**errp
)
342 while (strings
[i
++] != NULL
);
343 if (value
< 0 || value
>= i
- 1) {
344 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
348 enum_str
= (char *)strings
[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 char *const strings
[], Error
**errp
)
355 Error
*local_err
= NULL
;
359 visit_type_str(v
, name
, &enum_str
, &local_err
);
361 error_propagate(errp
, local_err
);
365 while (strings
[value
] != NULL
) {
366 if (strcmp(strings
[value
], enum_str
) == 0) {
372 if (strings
[value
] == NULL
) {
373 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
382 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
383 const char *const strings
[], Error
**errp
)
385 assert(obj
&& strings
);
386 trace_visit_type_enum(v
, name
, obj
);
389 input_type_enum(v
, name
, obj
, strings
, errp
);
392 output_type_enum(v
, name
, obj
, strings
, errp
);
395 /* nothing further to do, scalar value was already copied by
396 * g_memdup() during visit_start_*() */
398 case VISITOR_DEALLOC
:
399 /* nothing to deallocate for a scalar */