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_start_struct(Visitor
*v
, const char *name
, void **obj
,
24 size_t size
, Error
**errp
)
30 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
32 v
->start_struct(v
, name
, obj
, size
, &err
);
33 if (obj
&& v
->type
== VISITOR_INPUT
) {
34 assert(!err
!= !*obj
);
36 error_propagate(errp
, err
);
39 void visit_check_struct(Visitor
*v
, Error
**errp
)
41 if (v
->check_struct
) {
42 v
->check_struct(v
, errp
);
46 void visit_end_struct(Visitor
*v
)
51 void visit_start_list(Visitor
*v
, const char *name
, GenericList
**list
,
52 size_t size
, Error
**errp
)
56 assert(!list
|| size
>= sizeof(GenericList
));
57 v
->start_list(v
, name
, list
, size
, &err
);
58 if (list
&& v
->type
== VISITOR_INPUT
) {
59 assert(!(err
&& *list
));
61 error_propagate(errp
, err
);
64 GenericList
*visit_next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
66 assert(tail
&& size
>= sizeof(GenericList
));
67 return v
->next_list(v
, tail
, size
);
70 void visit_end_list(Visitor
*v
)
75 void visit_start_alternate(Visitor
*v
, const char *name
,
76 GenericAlternate
**obj
, size_t size
,
77 bool promote_int
, Error
**errp
)
81 assert(obj
&& size
>= sizeof(GenericAlternate
));
82 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
83 if (v
->start_alternate
) {
84 v
->start_alternate(v
, name
, obj
, size
, promote_int
, &err
);
86 if (v
->type
== VISITOR_INPUT
) {
87 assert(v
->start_alternate
&& !err
!= !*obj
);
89 error_propagate(errp
, err
);
92 void visit_end_alternate(Visitor
*v
)
94 if (v
->end_alternate
) {
99 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
102 v
->optional(v
, name
, present
);
107 bool visit_is_input(Visitor
*v
)
109 return v
->type
== VISITOR_INPUT
;
112 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
115 v
->type_int64(v
, name
, obj
, errp
);
118 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
119 uint64_t max
, const char *type
, Error
**errp
)
122 uint64_t value
= *obj
;
124 v
->type_uint64(v
, name
, &value
, &err
);
126 error_propagate(errp
, err
);
127 } else if (value
> max
) {
128 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
129 name
? name
: "null", type
);
135 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
138 uint64_t value
= *obj
;
139 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
143 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
146 uint64_t value
= *obj
;
147 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
151 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
154 uint64_t value
= *obj
;
155 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
159 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
163 v
->type_uint64(v
, name
, obj
, errp
);
166 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
167 int64_t min
, int64_t max
, const char *type
,
171 int64_t value
= *obj
;
173 v
->type_int64(v
, name
, &value
, &err
);
175 error_propagate(errp
, err
);
176 } else if (value
< min
|| value
> max
) {
177 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
178 name
? name
: "null", type
);
184 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
186 int64_t value
= *obj
;
187 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
191 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
194 int64_t value
= *obj
;
195 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
199 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
202 int64_t value
= *obj
;
203 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
207 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
211 v
->type_int64(v
, name
, obj
, errp
);
214 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
219 v
->type_size(v
, name
, obj
, errp
);
221 v
->type_uint64(v
, name
, obj
, errp
);
225 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
228 v
->type_bool(v
, name
, obj
, errp
);
231 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
236 /* TODO: Fix callers to not pass NULL when they mean "", so that we
238 assert(v->type != VISITOR_OUTPUT || *obj);
240 v
->type_str(v
, name
, obj
, &err
);
241 if (v
->type
== VISITOR_INPUT
) {
242 assert(!err
!= !*obj
);
244 error_propagate(errp
, err
);
247 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
251 v
->type_number(v
, name
, obj
, errp
);
254 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
259 assert(v
->type
!= VISITOR_OUTPUT
|| *obj
);
260 v
->type_any(v
, name
, obj
, &err
);
261 if (v
->type
== VISITOR_INPUT
) {
262 assert(!err
!= !*obj
);
264 error_propagate(errp
, err
);
267 void visit_type_null(Visitor
*v
, const char *name
, Error
**errp
)
269 v
->type_null(v
, name
, errp
);
272 static void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
273 const char *const strings
[], Error
**errp
)
279 while (strings
[i
++] != NULL
);
280 if (value
< 0 || value
>= i
- 1) {
281 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
285 enum_str
= (char *)strings
[value
];
286 visit_type_str(v
, name
, &enum_str
, errp
);
289 static void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
290 const char *const strings
[], Error
**errp
)
292 Error
*local_err
= NULL
;
296 visit_type_str(v
, name
, &enum_str
, &local_err
);
298 error_propagate(errp
, local_err
);
302 while (strings
[value
] != NULL
) {
303 if (strcmp(strings
[value
], enum_str
) == 0) {
309 if (strings
[value
] == NULL
) {
310 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);
319 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
320 const char *const strings
[], Error
**errp
)
322 assert(obj
&& strings
);
323 if (v
->type
== VISITOR_INPUT
) {
324 input_type_enum(v
, name
, obj
, strings
, errp
);
325 } else if (v
->type
== VISITOR_OUTPUT
) {
326 output_type_enum(v
, name
, obj
, strings
, errp
);