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_list(Visitor
*v
, const char *name
, Error
**errp
)
35 v
->start_list(v
, name
, errp
);
38 GenericList
*visit_next_list(Visitor
*v
, GenericList
**list
, size_t size
)
40 assert(list
&& size
>= sizeof(GenericList
));
41 return v
->next_list(v
, list
, size
);
44 void visit_end_list(Visitor
*v
)
49 void visit_start_alternate(Visitor
*v
, const char *name
,
50 GenericAlternate
**obj
, size_t size
,
51 bool promote_int
, Error
**errp
)
53 assert(obj
&& size
>= sizeof(GenericAlternate
));
54 if (v
->start_alternate
) {
55 v
->start_alternate(v
, name
, obj
, size
, promote_int
, errp
);
59 void visit_end_alternate(Visitor
*v
)
61 if (v
->end_alternate
) {
66 bool visit_optional(Visitor
*v
, const char *name
, bool *present
)
69 v
->optional(v
, name
, present
);
74 void visit_type_enum(Visitor
*v
, const char *name
, int *obj
,
75 const char *const strings
[], Error
**errp
)
77 v
->type_enum(v
, name
, obj
, strings
, errp
);
80 void visit_type_int(Visitor
*v
, const char *name
, int64_t *obj
, Error
**errp
)
82 v
->type_int64(v
, name
, obj
, errp
);
85 static void visit_type_uintN(Visitor
*v
, uint64_t *obj
, const char *name
,
86 uint64_t max
, const char *type
, Error
**errp
)
89 uint64_t value
= *obj
;
91 v
->type_uint64(v
, name
, &value
, &err
);
93 error_propagate(errp
, err
);
94 } else if (value
> max
) {
95 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
96 name
? name
: "null", type
);
102 void visit_type_uint8(Visitor
*v
, const char *name
, uint8_t *obj
,
105 uint64_t value
= *obj
;
106 visit_type_uintN(v
, &value
, name
, UINT8_MAX
, "uint8_t", errp
);
110 void visit_type_uint16(Visitor
*v
, const char *name
, uint16_t *obj
,
113 uint64_t value
= *obj
;
114 visit_type_uintN(v
, &value
, name
, UINT16_MAX
, "uint16_t", errp
);
118 void visit_type_uint32(Visitor
*v
, const char *name
, uint32_t *obj
,
121 uint64_t value
= *obj
;
122 visit_type_uintN(v
, &value
, name
, UINT32_MAX
, "uint32_t", errp
);
126 void visit_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
129 v
->type_uint64(v
, name
, obj
, errp
);
132 static void visit_type_intN(Visitor
*v
, int64_t *obj
, const char *name
,
133 int64_t min
, int64_t max
, const char *type
,
137 int64_t value
= *obj
;
139 v
->type_int64(v
, name
, &value
, &err
);
141 error_propagate(errp
, err
);
142 } else if (value
< min
|| value
> max
) {
143 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
144 name
? name
: "null", type
);
150 void visit_type_int8(Visitor
*v
, const char *name
, int8_t *obj
, Error
**errp
)
152 int64_t value
= *obj
;
153 visit_type_intN(v
, &value
, name
, INT8_MIN
, INT8_MAX
, "int8_t", errp
);
157 void visit_type_int16(Visitor
*v
, const char *name
, int16_t *obj
,
160 int64_t value
= *obj
;
161 visit_type_intN(v
, &value
, name
, INT16_MIN
, INT16_MAX
, "int16_t", errp
);
165 void visit_type_int32(Visitor
*v
, const char *name
, int32_t *obj
,
168 int64_t value
= *obj
;
169 visit_type_intN(v
, &value
, name
, INT32_MIN
, INT32_MAX
, "int32_t", errp
);
173 void visit_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
176 v
->type_int64(v
, name
, obj
, errp
);
179 void visit_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
183 v
->type_size(v
, name
, obj
, errp
);
185 v
->type_uint64(v
, name
, obj
, errp
);
189 void visit_type_bool(Visitor
*v
, const char *name
, bool *obj
, Error
**errp
)
191 v
->type_bool(v
, name
, obj
, errp
);
194 void visit_type_str(Visitor
*v
, const char *name
, char **obj
, Error
**errp
)
196 v
->type_str(v
, name
, obj
, errp
);
199 void visit_type_number(Visitor
*v
, const char *name
, double *obj
,
202 v
->type_number(v
, name
, obj
, errp
);
205 void visit_type_any(Visitor
*v
, const char *name
, QObject
**obj
, Error
**errp
)
207 v
->type_any(v
, name
, obj
, errp
);
210 void output_type_enum(Visitor
*v
, const char *name
, int *obj
,
211 const char *const strings
[], Error
**errp
)
218 while (strings
[i
++] != NULL
);
219 if (value
< 0 || value
>= i
- 1) {
220 error_setg(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
224 enum_str
= (char *)strings
[value
];
225 visit_type_str(v
, name
, &enum_str
, errp
);
228 void input_type_enum(Visitor
*v
, const char *name
, int *obj
,
229 const char *const strings
[], Error
**errp
)
231 Error
*local_err
= NULL
;
237 visit_type_str(v
, name
, &enum_str
, &local_err
);
239 error_propagate(errp
, local_err
);
243 while (strings
[value
] != NULL
) {
244 if (strcmp(strings
[value
], enum_str
) == 0) {
250 if (strings
[value
] == NULL
) {
251 error_setg(errp
, QERR_INVALID_PARAMETER
, enum_str
);