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 "qemu-common.h"
17 #include "qapi/qmp/qobject.h"
18 #include "qapi/qmp/qerror.h"
19 #include "qapi/visitor.h"
20 #include "qapi/visitor-impl.h"
22 void visit_start_struct(Visitor
*v
, const char *name
, void **obj
,
23 size_t size
, Error
**errp
)
25 v
->start_struct(v
, name
, obj
, size
, errp
);
28 void visit_end_struct(Visitor
*v
, Error
**errp
)
30 v
->end_struct(v
, errp
);
33 void visit_start_implicit_struct(Visitor
*v
, void **obj
, size_t size
,
36 if (v
->start_implicit_struct
) {
37 v
->start_implicit_struct(v
, obj
, size
, errp
);
41 void visit_end_implicit_struct(Visitor
*v
)
43 if (v
->end_implicit_struct
) {
44 v
->end_implicit_struct(v
);
48 void visit_start_list(Visitor
*v
, const char *name
, Error
**errp
)
50 v
->start_list(v
, name
, errp
);
53 GenericList
*visit_next_list(Visitor
*v
, GenericList
**list
)
55 return v
->next_list(v
, list
);
58 void visit_end_list(Visitor
*v
)
63 bool visit_start_union(Visitor
*v
, bool data_present
, Error
**errp
)
66 return v
->start_union(v
, data_present
, errp
);
71 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
74 v
->optional(v
, name
, present
);
79 void visit_get_next_type(Visitor
*v
, const char *name
, QType
*type
,
80 bool promote_int
, Error
**errp
)
82 if (v
->get_next_type
) {
83 v
->get_next_type(v
, name
, type
, promote_int
, errp
);
87 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
88 const char *const strings
[], Error
**errp
)
90 v
->type_enum(v
, name
, obj
, strings
, errp
);
93 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
95 v
->type_int64(v
, name
, obj
, errp
);
98 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
99 uint64_t max
, const char *type
, Error
**errp
)
102 uint64_t value
= *obj
;
104 v
->type_uint64(v
, name
, &value
, &err
);
106 error_propagate(errp
, err
);
107 } else if (value
> max
) {
108 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
109 name
? name
: "null", type
);
115 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
118 uint64_t value
= *obj
;
119 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
123 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
126 uint64_t value
= *obj
;
127 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
131 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
134 uint64_t value
= *obj
;
135 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
139 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
142 v
->type_uint64(v
, name
, obj
, errp
);
145 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
146 int64_t min
, int64_t max
, const char *type
,
150 int64_t value
= *obj
;
152 v
->type_int64(v
, name
, &value
, &err
);
154 error_propagate(errp
, err
);
155 } else if (value
< min
|| value
> max
) {
156 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
157 name
? name
: "null", type
);
163 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
165 int64_t value
= *obj
;
166 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
170 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
173 int64_t value
= *obj
;
174 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
178 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
181 int64_t value
= *obj
;
182 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
186 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
189 v
->type_int64(v
, name
, obj
, errp
);
192 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
196 v
->type_size(v
, name
, obj
, errp
);
198 v
->type_uint64(v
, name
, obj
, errp
);
202 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
204 v
->type_bool(v
, name
, obj
, errp
);
207 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
209 v
->type_str(v
, name
, obj
, errp
);
212 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
215 v
->type_number(v
, name
, obj
, errp
);
218 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
220 v
->type_any(v
, name
, obj
, errp
);
223 void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
224 const char *const strings
[], Error
**errp
)
231 while (strings
[i
++] != NULL
);
232 if (value
< 0 || value
>= i
- 1) {
233 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
237 enum_str
= (char *)strings
[value
];
238 visit_type_str(v
, name
, &enum_str
, errp
);
241 void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
242 const char *const strings
[], Error
**errp
)
244 Error
*local_err
= NULL
;
250 visit_type_str(v
, name
, &enum_str
, &local_err
);
252 error_propagate(errp
, local_err
);
256 while (strings
[value
] != NULL
) {
257 if (strcmp(strings
[value
], enum_str
) == 0) {
263 if (strings
[value
] == NULL
) {
264 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);