4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
12 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
13 * See the COPYING.LIB file in the top-level directory.
16 #include "qemu/osdep.h"
18 #include "qapi/qmp/qlit.h"
19 #include "qapi/qmp/qbool.h"
20 #include "qapi/qmp/qlist.h"
21 #include "qapi/qmp/qnum.h"
22 #include "qapi/qmp/qdict.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp/qnull.h"
26 static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
30 for (i = 0; lhs->value.qdict[i].key; i++) {
31 QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key);
33 if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
38 /* Note: the literal qdict must not contain duplicates, this is
39 * considered a programming error and it isn't checked here. */
40 if (qdict_size(qdict) != i) {
47 static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
52 QLIST_FOREACH_ENTRY(qlist, e) {
53 QObject *obj = qlist_entry_obj(e);
55 if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) {
61 return !e && lhs->value.qlist[i].type == QTYPE_NONE;
64 bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
66 if (!rhs || lhs->type != qobject_type(rhs)) {
72 return lhs->value.qbool == qbool_get_bool(qobject_to(QBool, rhs));
74 return lhs->value.qnum == qnum_get_int(qobject_to(QNum, rhs));
76 return (strcmp(lhs->value.qstr,
77 qstring_get_str(qobject_to(QString, rhs))) == 0);
79 return qlit_equal_qdict(lhs, qobject_to(QDict, rhs));
81 return qlit_equal_qlist(lhs, qobject_to(QList, rhs));
91 QObject *qobject_from_qlit(const QLitObject *qlit)
95 return QOBJECT(qnull());
97 return QOBJECT(qnum_from_int(qlit->value.qnum));
99 return QOBJECT(qstring_from_str(qlit->value.qstr));
101 QDict *qdict = qdict_new();
104 for (e = qlit->value.qdict; e->key; e++) {
105 qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
107 return QOBJECT(qdict);
110 QList *qlist = qlist_new();
113 for (e = qlit->value.qlist; e->type != QTYPE_NONE; e++) {
114 qlist_append_obj(qlist, qobject_from_qlit(e));
116 return QOBJECT(qlist);
119 return QOBJECT(qbool_from_bool(qlit->value.qbool));