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" 19 #include "qapi/qmp/qbool.h" 20 #include "qapi/qmp/qnum.h" 21 #include "qapi/qmp/qdict.h" 22 #include "qapi/qmp/qstring.h" 23 24 static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict) 25 { 26 int i; 27 28 for (i = 0; lhs->value.qdict[i].key; i++) { 29 QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key); 30 31 if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) { 32 return false; 33 } 34 } 35 36 /* Note: the literal qdict must not contain duplicates, this is 37 * considered a programming error and it isn't checked here. */ 38 if (qdict_size(qdict) != i) { 39 return false; 40 } 41 42 return true; 43 } 44 45 static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist) 46 { 47 QListEntry *e; 48 int i = 0; 49 50 QLIST_FOREACH_ENTRY(qlist, e) { 51 QObject *obj = qlist_entry_obj(e); 52 53 if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) { 54 return false; 55 } 56 i++; 57 } 58 59 return !e && lhs->value.qlist[i].type == QTYPE_NONE; 60 } 61 62 bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs) 63 { 64 if (!rhs || lhs->type != qobject_type(rhs)) { 65 return false; 66 } 67 68 switch (lhs->type) { 69 case QTYPE_QBOOL: 70 return lhs->value.qbool == qbool_get_bool(qobject_to_qbool(rhs)); 71 case QTYPE_QNUM: 72 return lhs->value.qnum == qnum_get_int(qobject_to_qnum(rhs)); 73 case QTYPE_QSTRING: 74 return (strcmp(lhs->value.qstr, 75 qstring_get_str(qobject_to_qstring(rhs))) == 0); 76 case QTYPE_QDICT: 77 return qlit_equal_qdict(lhs, qobject_to_qdict(rhs)); 78 case QTYPE_QLIST: 79 return qlit_equal_qlist(lhs, qobject_to_qlist(rhs)); 80 case QTYPE_QNULL: 81 return true; 82 default: 83 break; 84 } 85 86 return false; 87 } 88