]> git.proxmox.com Git - mirror_qemu.git/blobdiff - qobject/qlit.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / qobject / qlit.c
index 5917c3584ebf6592c30cfcaf401a829471adac7d..be8332136c27211ccec967d6d8061cd631d612a4 100644 (file)
 #include "qemu/osdep.h"
 
 #include "qapi/qmp/qlit.h"
-#include "qapi/qmp/types.h"
-
-typedef struct QListCompareHelper {
-    int index;
-    LiteralQObject *objs;
-    int result;
-} QListCompareHelper;
-
-static void compare_helper(QObject *obj, void *opaque)
+#include "qapi/qmp/qbool.h"
+#include "qapi/qmp/qlist.h"
+#include "qapi/qmp/qnum.h"
+#include "qapi/qmp/qdict.h"
+#include "qapi/qmp/qstring.h"
+#include "qapi/qmp/qnull.h"
+
+static bool qlit_equal_qdict(const QLitObject *lhs, const QDict *qdict)
 {
-    QListCompareHelper *helper = opaque;
+    int i;
 
-    if (helper->result == 0) {
-        return;
+    for (i = 0; lhs->value.qdict[i].key; i++) {
+        QObject *obj = qdict_get(qdict, lhs->value.qdict[i].key);
+
+        if (!qlit_equal_qobject(&lhs->value.qdict[i].value, obj)) {
+            return false;
+        }
     }
 
-    if (helper->objs[helper->index].type == QTYPE_NONE) {
-        helper->result = 0;
-        return;
+    /* Note: the literal qdict must not contain duplicates, this is
+     * considered a programming error and it isn't checked here. */
+    if (qdict_size(qdict) != i) {
+        return false;
     }
 
-    helper->result =
-        compare_litqobj_to_qobj(&helper->objs[helper->index++], obj);
+    return true;
 }
 
-int compare_litqobj_to_qobj(LiteralQObject *lhs, QObject *rhs)
+static bool qlit_equal_qlist(const QLitObject *lhs, const QList *qlist)
 {
-    int64_t val;
+    QListEntry *e;
+    int i = 0;
 
+    QLIST_FOREACH_ENTRY(qlist, e) {
+        QObject *obj = qlist_entry_obj(e);
+
+        if (!qlit_equal_qobject(&lhs->value.qlist[i], obj)) {
+            return false;
+        }
+        i++;
+    }
+
+    return !e && lhs->value.qlist[i].type == QTYPE_NONE;
+}
+
+bool qlit_equal_qobject(const QLitObject *lhs, const QObject *rhs)
+{
     if (!rhs || lhs->type != qobject_type(rhs)) {
-        return 0;
+        return false;
     }
 
     switch (lhs->type) {
+    case QTYPE_QBOOL:
+        return lhs->value.qbool == qbool_get_bool(qobject_to(QBool, rhs));
     case QTYPE_QNUM:
-        g_assert(qnum_get_try_int(qobject_to_qnum(rhs), &val));
-        return lhs->value.qnum == val;
+        return lhs->value.qnum ==  qnum_get_int(qobject_to(QNum, rhs));
     case QTYPE_QSTRING:
         return (strcmp(lhs->value.qstr,
-                       qstring_get_str(qobject_to_qstring(rhs))) == 0);
-    case QTYPE_QDICT: {
-        int i;
+                       qstring_get_str(qobject_to(QString, rhs))) == 0);
+    case QTYPE_QDICT:
+        return qlit_equal_qdict(lhs, qobject_to(QDict, rhs));
+    case QTYPE_QLIST:
+        return qlit_equal_qlist(lhs, qobject_to(QList, rhs));
+    case QTYPE_QNULL:
+        return true;
+    default:
+        break;
+    }
 
-        for (i = 0; lhs->value.qdict[i].key; i++) {
-            QObject *obj = qdict_get(qobject_to_qdict(rhs),
-                                     lhs->value.qdict[i].key);
+    return false;
+}
 
-            if (!compare_litqobj_to_qobj(&lhs->value.qdict[i].value, obj)) {
-                return 0;
-            }
-        }
+QObject *qobject_from_qlit(const QLitObject *qlit)
+{
+    switch (qlit->type) {
+    case QTYPE_QNULL:
+        return QOBJECT(qnull());
+    case QTYPE_QNUM:
+        return QOBJECT(qnum_from_int(qlit->value.qnum));
+    case QTYPE_QSTRING:
+        return QOBJECT(qstring_from_str(qlit->value.qstr));
+    case QTYPE_QDICT: {
+        QDict *qdict = qdict_new();
+        QLitDictEntry *e;
 
-        return 1;
+        for (e = qlit->value.qdict; e->key; e++) {
+            qdict_put_obj(qdict, e->key, qobject_from_qlit(&e->value));
+        }
+        return QOBJECT(qdict);
     }
     case QTYPE_QLIST: {
-        QListCompareHelper helper;
-
-        helper.index = 0;
-        helper.objs = lhs->value.qlist;
-        helper.result = 1;
+        QList *qlist = qlist_new();
+        QLitObject *e;
 
-        qlist_iter(qobject_to_qlist(rhs), compare_helper, &helper);
-
-        return helper.result;
+        for (e = qlit->value.qlist; e->type != QTYPE_NONE; e++) {
+            qlist_append_obj(qlist, qobject_from_qlit(e));
+        }
+        return QOBJECT(qlist);
     }
+    case QTYPE_QBOOL:
+        return QOBJECT(qbool_from_bool(qlit->value.qbool));
     default:
-        break;
+        assert(0);
     }
 
-    return 0;
+    return NULL;
 }