2 * Core Definitions for QAPI/QMP Command Registry
4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
14 #include "qmp-output-visitor.h"
15 #include "qemu-queue.h"
16 #include "qemu-common.h"
17 #include "qemu-objects.h"
20 typedef struct QStackEntry
24 QTAILQ_ENTRY(QStackEntry
) node
;
27 typedef QTAILQ_HEAD(QStack
, QStackEntry
) QStack
;
29 struct QmpOutputVisitor
35 #define qmp_output_add(qov, name, value) \
36 qmp_output_add_obj(qov, name, QOBJECT(value))
37 #define qmp_output_push(qov, value) qmp_output_push_obj(qov, QOBJECT(value))
39 static QmpOutputVisitor
*to_qov(Visitor
*v
)
41 return container_of(v
, QmpOutputVisitor
, visitor
);
44 static void qmp_output_push_obj(QmpOutputVisitor
*qov
, QObject
*value
)
46 QStackEntry
*e
= g_malloc0(sizeof(*e
));
49 if (qobject_type(e
->value
) == QTYPE_QLIST
) {
50 e
->is_list_head
= true;
52 QTAILQ_INSERT_HEAD(&qov
->stack
, e
, node
);
55 static QObject
*qmp_output_pop(QmpOutputVisitor
*qov
)
57 QStackEntry
*e
= QTAILQ_FIRST(&qov
->stack
);
59 QTAILQ_REMOVE(&qov
->stack
, e
, node
);
65 static QObject
*qmp_output_first(QmpOutputVisitor
*qov
)
67 QStackEntry
*e
= QTAILQ_LAST(&qov
->stack
, QStack
);
71 static QObject
*qmp_output_last(QmpOutputVisitor
*qov
)
73 QStackEntry
*e
= QTAILQ_FIRST(&qov
->stack
);
77 static void qmp_output_add_obj(QmpOutputVisitor
*qov
, const char *name
,
82 if (QTAILQ_EMPTY(&qov
->stack
)) {
83 qmp_output_push_obj(qov
, value
);
87 cur
= qmp_output_last(qov
);
89 switch (qobject_type(cur
)) {
91 qdict_put_obj(qobject_to_qdict(cur
), name
, value
);
94 qlist_append_obj(qobject_to_qlist(cur
), value
);
97 qobject_decref(qmp_output_pop(qov
));
98 qmp_output_push_obj(qov
, value
);
103 static void qmp_output_start_struct(Visitor
*v
, void **obj
, const char *kind
,
104 const char *name
, size_t unused
,
107 QmpOutputVisitor
*qov
= to_qov(v
);
108 QDict
*dict
= qdict_new();
110 qmp_output_add(qov
, name
, dict
);
111 qmp_output_push(qov
, dict
);
114 static void qmp_output_end_struct(Visitor
*v
, Error
**errp
)
116 QmpOutputVisitor
*qov
= to_qov(v
);
120 static void qmp_output_start_list(Visitor
*v
, const char *name
, Error
**errp
)
122 QmpOutputVisitor
*qov
= to_qov(v
);
123 QList
*list
= qlist_new();
125 qmp_output_add(qov
, name
, list
);
126 qmp_output_push(qov
, list
);
129 static GenericList
*qmp_output_next_list(Visitor
*v
, GenericList
**listp
,
132 GenericList
*list
= *listp
;
133 QmpOutputVisitor
*qov
= to_qov(v
);
134 QStackEntry
*e
= QTAILQ_FIRST(&qov
->stack
);
137 if (e
->is_list_head
) {
138 e
->is_list_head
= false;
142 return list
? list
->next
: NULL
;
145 static void qmp_output_end_list(Visitor
*v
, Error
**errp
)
147 QmpOutputVisitor
*qov
= to_qov(v
);
151 static void qmp_output_type_int(Visitor
*v
, int64_t *obj
, const char *name
,
154 QmpOutputVisitor
*qov
= to_qov(v
);
155 qmp_output_add(qov
, name
, qint_from_int(*obj
));
158 static void qmp_output_type_bool(Visitor
*v
, bool *obj
, const char *name
,
161 QmpOutputVisitor
*qov
= to_qov(v
);
162 qmp_output_add(qov
, name
, qbool_from_int(*obj
));
165 static void qmp_output_type_str(Visitor
*v
, char **obj
, const char *name
,
168 QmpOutputVisitor
*qov
= to_qov(v
);
170 qmp_output_add(qov
, name
, qstring_from_str(*obj
));
172 qmp_output_add(qov
, name
, qstring_from_str(""));
176 static void qmp_output_type_number(Visitor
*v
, double *obj
, const char *name
,
179 QmpOutputVisitor
*qov
= to_qov(v
);
180 qmp_output_add(qov
, name
, qfloat_from_double(*obj
));
183 static void qmp_output_type_enum(Visitor
*v
, int *obj
, const char *strings
[],
184 const char *kind
, const char *name
,
192 while (strings
[i
++] != NULL
);
193 if (value
< 0 || value
>= i
- 1) {
194 error_set(errp
, QERR_INVALID_PARAMETER
, name
? name
: "null");
198 enum_str
= (char *)strings
[value
];
199 qmp_output_type_str(v
, &enum_str
, name
, errp
);
202 QObject
*qmp_output_get_qobject(QmpOutputVisitor
*qov
)
204 QObject
*obj
= qmp_output_first(qov
);
211 Visitor
*qmp_output_get_visitor(QmpOutputVisitor
*v
)
216 void qmp_output_visitor_cleanup(QmpOutputVisitor
*v
)
218 QStackEntry
*e
, *tmp
;
220 QTAILQ_FOREACH_SAFE(e
, &v
->stack
, node
, tmp
) {
221 QTAILQ_REMOVE(&v
->stack
, e
, node
);
223 qobject_decref(e
->value
);
231 QmpOutputVisitor
*qmp_output_visitor_new(void)
235 v
= g_malloc0(sizeof(*v
));
237 v
->visitor
.start_struct
= qmp_output_start_struct
;
238 v
->visitor
.end_struct
= qmp_output_end_struct
;
239 v
->visitor
.start_list
= qmp_output_start_list
;
240 v
->visitor
.next_list
= qmp_output_next_list
;
241 v
->visitor
.end_list
= qmp_output_end_list
;
242 v
->visitor
.type_enum
= qmp_output_type_enum
;
243 v
->visitor
.type_int
= qmp_output_type_int
;
244 v
->visitor
.type_bool
= qmp_output_type_bool
;
245 v
->visitor
.type_str
= qmp_output_type_str
;
246 v
->visitor
.type_number
= qmp_output_type_number
;
248 QTAILQ_INIT(&v
->stack
);