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"
23 void visit_complete(Visitor
*v
, void *opaque
)
25 assert(v
->type
!= VISITOR_OUTPUT
|| v
->complete
);
27 v
->complete(v
, opaque
);
31 void visit_free(Visitor
*v
)
38 void visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
39 size_t size
, Error
**errp
)
45 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
47 v
->start_struct(v
, name
, obj
, size
, &err
);
48 if (obj
&& (v
->type
& VISITOR_INPUT
)) {
49 assert(!err
!= !*obj
);
51 error_propagate(errp
, err
);
54 void visit_check_struct(Visitor
*v
, Error
**errp
)
56 if (v
->check_struct
) {
57 v
->check_struct(v
, errp
);
61 void visit_end_struct(Visitor
*v
, void **obj
)
63 v
->end_struct(v
, obj
);
66 void visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
67 size_t size
, Error
**errp
)
71 assert(!list
|| size
>= sizeof(GenericList
));
72 v
->start_list(v
, name
, list
, size
, &err
);
73 if (list
&& (v
->type
& VISITOR_INPUT
)) {
74 assert(!(err
&& *list
));
76 error_propagate(errp
, err
);
79 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
81 assert(tail
&& size
>= sizeof(GenericList
));
82 return v
->next_list(v
, tail
, size
);
85 void visit_end_list(Visitor
*v
, void **obj
)
90 void visit_start_alternate(Visitor
*v
, const char *name
,
91 GenericAlternate
**obj
, size_t size
,
92 bool promote_int
, Error
**errp
)
96 assert(obj
&& size
>= sizeof(GenericAlternate
));
97 assert(!(v
->type
& VISITOR_OUTPUT
) || *obj
);
98 if (v
->start_alternate
) {
99 v
->start_alternate(v
, name
, obj
, size
, promote_int
, &err
);
101 if (v
->type
& VISITOR_INPUT
) {
102 assert(v
->start_alternate
&& !err
!= !*obj
);
104 error_propagate(errp
, err
);
107 void visit_end_alternate(Visitor
*v
, void **obj
)
109 if (v
->end_alternate
) {
110 v
->end_alternate(v
, obj
);
114 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
117 v
->optional(v
, name
, present
);
122 bool visit_is_input(Visitor
*v
)
124 return v
->type
== VISITOR_INPUT
;
127 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
130 v
->type_int64(v
, name
, obj
, errp
);
133 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
134 uint64_t max
, const char *type
, Error
**errp
)
137 uint64_t value
= *obj
;
139 v
->type_uint64(v
, name
, &value
, &err
);
141 error_propagate(errp
, err
);
142 } else if (value
> max
) {
143 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
144 name
? name
: "null", type
);
150 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
153 uint64_t value
= *obj
;
154 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
158 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
161 uint64_t value
= *obj
;
162 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
166 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
169 uint64_t value
= *obj
;
170 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
174 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
178 v
->type_uint64(v
, name
, obj
, errp
);
181 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
182 int64_t min
, int64_t max
, const char *type
,
186 int64_t value
= *obj
;
188 v
->type_int64(v
, name
, &value
, &err
);
190 error_propagate(errp
, err
);
191 } else if (value
< min
|| value
> max
) {
192 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
193 name
? name
: "null", type
);
199 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
201 int64_t value
= *obj
;
202 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
206 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
209 int64_t value
= *obj
;
210 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
214 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
217 int64_t value
= *obj
;
218 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
222 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
226 v
->type_int64(v
, name
, obj
, errp
);
229 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
234 v
->type_size(v
, name
, obj
, errp
);
236 v
->type_uint64(v
, name
, obj
, errp
);
240 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
243 v
->type_bool(v
, name
, obj
, errp
);
246 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
251 /* TODO: Fix callers to not pass NULL when they mean "", so that we
253 assert(!(v->type & VISITOR_OUTPUT) || *obj);
255 v
->type_str(v
, name
, obj
, &err
);
256 if (v
->type
& VISITOR_INPUT
) {
257 assert(!err
!= !*obj
);
259 error_propagate(errp
, err
);
262 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
266 v
->type_number(v
, name
, obj
, errp
);
269 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
274 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
275 v
->type_any(v
, name
, obj
, &err
);
276 if (v
->type
== VISITOR_INPUT
) {
277 assert(!err
!= !*obj
);
279 error_propagate(errp
, err
);
282 void visit_type_null(Visitor
*v
, const char *name
, Error
**errp
)
284 v
->type_null(v
, name
, errp
);
287 static void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
288 const char *const strings
[], Error
**errp
)
294 while (strings
[i
++] != NULL
);
295 if (value
< 0 || value
>= i
- 1) {
296 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
300 enum_str
= (char *)strings
[value
];
301 visit_type_str(v
, name
, &enum_str
, errp
);
304 static void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
305 const char *const strings
[], Error
**errp
)
307 Error
*local_err
= NULL
;
311 visit_type_str(v
, name
, &enum_str
, &local_err
);
313 error_propagate(errp
, local_err
);
317 while (strings
[value
] != NULL
) {
318 if (strcmp(strings
[value
], enum_str
) == 0) {
324 if (strings
[value
] == NULL
) {
325 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
334 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
335 const char *const strings
[], Error
**errp
)
337 assert(obj
&& strings
);
340 input_type_enum(v
, name
, obj
, strings
, errp
);
343 output_type_enum(v
, name
, obj
, strings
, errp
);
346 /* nothing further to do, scalar value was already copied by
347 * g_memdup() during visit_start_*() */
349 case VISITOR_DEALLOC
:
350 /* nothing to deallocate for a scalar */