qemu-thread: implement joinable threads for POSIX
[qemu/wangdongxu.git] / qapi / qmp-output-visitor.c
blobf76d0159cd6760cff457ef99d7101402325d3ad2
1 /*
2 * Core Definitions for QAPI/QMP Command Registry
4 * Copyright IBM, Corp. 2011
6 * Authors:
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"
18 #include "qerror.h"
20 typedef struct QStackEntry
22 QObject *value;
23 bool is_list_head;
24 QTAILQ_ENTRY(QStackEntry) node;
25 } QStackEntry;
27 typedef QTAILQ_HEAD(QStack, QStackEntry) QStack;
29 struct QmpOutputVisitor
31 Visitor visitor;
32 QStack stack;
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));
48 e->value = value;
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);
58 QObject *value;
59 QTAILQ_REMOVE(&qov->stack, e, node);
60 value = e->value;
61 g_free(e);
62 return value;
65 static QObject *qmp_output_first(QmpOutputVisitor *qov)
67 QStackEntry *e = QTAILQ_LAST(&qov->stack, QStack);
68 return e->value;
71 static QObject *qmp_output_last(QmpOutputVisitor *qov)
73 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
74 return e->value;
77 static void qmp_output_add_obj(QmpOutputVisitor *qov, const char *name,
78 QObject *value)
80 QObject *cur;
82 if (QTAILQ_EMPTY(&qov->stack)) {
83 qmp_output_push_obj(qov, value);
84 return;
87 cur = qmp_output_last(qov);
89 switch (qobject_type(cur)) {
90 case QTYPE_QDICT:
91 qdict_put_obj(qobject_to_qdict(cur), name, value);
92 break;
93 case QTYPE_QLIST:
94 qlist_append_obj(qobject_to_qlist(cur), value);
95 break;
96 default:
97 qobject_decref(qmp_output_pop(qov));
98 qmp_output_push_obj(qov, value);
99 break;
103 static void qmp_output_start_struct(Visitor *v, void **obj, const char *kind,
104 const char *name, size_t unused,
105 Error **errp)
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);
117 qmp_output_pop(qov);
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,
130 Error **errp)
132 GenericList *list = *listp;
133 QmpOutputVisitor *qov = to_qov(v);
134 QStackEntry *e = QTAILQ_FIRST(&qov->stack);
136 assert(e);
137 if (e->is_list_head) {
138 e->is_list_head = false;
139 return list;
142 return list ? list->next : NULL;
145 static void qmp_output_end_list(Visitor *v, Error **errp)
147 QmpOutputVisitor *qov = to_qov(v);
148 qmp_output_pop(qov);
151 static void qmp_output_type_int(Visitor *v, int64_t *obj, const char *name,
152 Error **errp)
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,
159 Error **errp)
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,
166 Error **errp)
168 QmpOutputVisitor *qov = to_qov(v);
169 if (*obj) {
170 qmp_output_add(qov, name, qstring_from_str(*obj));
171 } else {
172 qmp_output_add(qov, name, qstring_from_str(""));
176 static void qmp_output_type_number(Visitor *v, double *obj, const char *name,
177 Error **errp)
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,
185 Error **errp)
187 int i = 0;
188 int value = *obj;
189 char *enum_str;
191 assert(strings);
192 while (strings[i++] != NULL);
193 if (value < 0 || value >= i - 1) {
194 error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
195 return;
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);
205 if (obj) {
206 qobject_incref(obj);
208 return obj;
211 Visitor *qmp_output_get_visitor(QmpOutputVisitor *v)
213 return &v->visitor;
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);
222 if (e->value) {
223 qobject_decref(e->value);
225 g_free(e);
228 g_free(v);
231 QmpOutputVisitor *qmp_output_visitor_new(void)
233 QmpOutputVisitor *v;
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);
250 return v;