]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/qlit.c
qlit: use QType instead of int
[mirror_qemu.git] / qobject / qlit.c
CommitLineData
28035bcd
MAL
1/*
2 * QLit literal qobject
3 *
4 * Copyright IBM, Corp. 2009
5 * Copyright (c) 2013, 2015, 2017 Red Hat Inc.
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.ibm.com>
9 * Markus Armbruster <armbru@redhat.com>
10 * Marc-André Lureau <marcandre.lureau@redhat.com>
11 *
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.
14 */
15
16#include "qemu/osdep.h"
17
18#include "qapi/qmp/qlit.h"
6b673957 19#include "qapi/qmp/qbool.h"
47e6b297 20#include "qapi/qmp/qlist.h"
15280c36 21#include "qapi/qmp/qnum.h"
6b673957
MA
22#include "qapi/qmp/qdict.h"
23#include "qapi/qmp/qstring.h"
28035bcd 24
6da8a7a3
MAL
25static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
26{
27 int i;
28
29 for (i = 0; lhs->value.qdict[i].key; i++) {
30 QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key);
31
32 if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
33 return false;
34 }
35 }
36
37 /* Note: the literal qdict must not contain duplicates, this is
38 * considered a programming error and it isn't checked here. */
39 if (qdict_size(qdict) != i) {
40 return false;
41 }
42
43 return true;
44}
45
cbb65405
MAL
46static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
47{
48 QListEntry *e;
49 int i = 0;
50
51 QLIST_FOREACH_ENTRY(qlist, e) {
52 QObject *obj = qlist_entry_obj(e);
53
54 if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) {
55 return false;
56 }
57 i++;
58 }
59
60 return !e && lhs->value.qlist[i].type == QTYPE_NONE;
61}
62
e2346a19 63bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
28035bcd 64{
28035bcd 65 if (!rhs || lhs->type != qobject_type(rhs)) {
d9eba57a 66 return false;
28035bcd
MAL
67 }
68
69 switch (lhs->type) {
6c6084c1
MAL
70 case QTYPE_QBOOL:
71 return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs));
28035bcd 72 case QTYPE_QNUM:
5f4bd809 73 return lhs->value.qnum == qnum_get_int(qobject_to_qnum(rhs));
28035bcd
MAL
74 case QTYPE_QSTRING:
75 return (strcmp(lhs->value.qstr,
76 qstring_get_str(qobject_to_qstring(rhs))) == 0);
6da8a7a3
MAL
77 case QTYPE_QDICT:
78 return qlit_equal_qdict(lhs, qobject_to_qdict(rhs));
cbb65405
MAL
79 case QTYPE_QLIST:
80 return qlit_equal_qlist(lhs, qobject_to_qlist(rhs));
6c6084c1
MAL
81 case QTYPE_QNULL:
82 return true;
28035bcd
MAL
83 default:
84 break;
85 }
86
d9eba57a 87 return false;
28035bcd 88}