155e1819cSEric Blake /* 255e1819cSEric Blake * QObject 355e1819cSEric Blake * 455e1819cSEric Blake * Copyright (C) 2015 Red Hat, Inc. 555e1819cSEric Blake * 655e1819cSEric Blake * This work is licensed under the terms of the GNU LGPL, version 2.1 755e1819cSEric Blake * or later. See the COPYING.LIB file in the top-level directory. 855e1819cSEric Blake */ 955e1819cSEric Blake 10f2ad72b3SPeter Maydell #include "qemu/osdep.h" 1155e1819cSEric Blake #include "qemu-common.h" 126b673957SMarkus Armbruster #include "qapi/qmp/qbool.h" 1315280c36SMarkus Armbruster #include "qapi/qmp/qnull.h" 1415280c36SMarkus Armbruster #include "qapi/qmp/qnum.h" 156b673957SMarkus Armbruster #include "qapi/qmp/qdict.h" 16*47e6b297SMarkus Armbruster #include "qapi/qmp/qlist.h" 176b673957SMarkus Armbruster #include "qapi/qmp/qstring.h" 1855e1819cSEric Blake 197264f5c5SEric Blake static void (*qdestroy[QTYPE__MAX])(QObject *) = { 2055e1819cSEric Blake [QTYPE_NONE] = NULL, /* No such object exists */ 2155e1819cSEric Blake [QTYPE_QNULL] = NULL, /* qnull_ is indestructible */ 2201b2ffceSMarc-André Lureau [QTYPE_QNUM] = qnum_destroy_obj, 2355e1819cSEric Blake [QTYPE_QSTRING] = qstring_destroy_obj, 2455e1819cSEric Blake [QTYPE_QDICT] = qdict_destroy_obj, 2555e1819cSEric Blake [QTYPE_QLIST] = qlist_destroy_obj, 2655e1819cSEric Blake [QTYPE_QBOOL] = qbool_destroy_obj, 2755e1819cSEric Blake }; 2855e1819cSEric Blake 2955e1819cSEric Blake void qobject_destroy(QObject *obj) 3055e1819cSEric Blake { 3155e1819cSEric Blake assert(!obj->refcnt); 327264f5c5SEric Blake assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX); 3355e1819cSEric Blake qdestroy[obj->type](obj); 3455e1819cSEric Blake } 35b38dd678SMax Reitz 36b38dd678SMax Reitz 37b38dd678SMax Reitz static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) = { 38b38dd678SMax Reitz [QTYPE_NONE] = NULL, /* No such object exists */ 39b38dd678SMax Reitz [QTYPE_QNULL] = qnull_is_equal, 40b38dd678SMax Reitz [QTYPE_QNUM] = qnum_is_equal, 41b38dd678SMax Reitz [QTYPE_QSTRING] = qstring_is_equal, 42b38dd678SMax Reitz [QTYPE_QDICT] = qdict_is_equal, 43b38dd678SMax Reitz [QTYPE_QLIST] = qlist_is_equal, 44b38dd678SMax Reitz [QTYPE_QBOOL] = qbool_is_equal, 45b38dd678SMax Reitz }; 46b38dd678SMax Reitz 47b38dd678SMax Reitz bool qobject_is_equal(const QObject *x, const QObject *y) 48b38dd678SMax Reitz { 49b38dd678SMax Reitz /* We cannot test x == y because an object does not need to be 50b38dd678SMax Reitz * equal to itself (e.g. NaN floats are not). */ 51b38dd678SMax Reitz 52b38dd678SMax Reitz if (!x && !y) { 53b38dd678SMax Reitz return true; 54b38dd678SMax Reitz } 55b38dd678SMax Reitz 56b38dd678SMax Reitz if (!x || !y || x->type != y->type) { 57b38dd678SMax Reitz return false; 58b38dd678SMax Reitz } 59b38dd678SMax Reitz 60b38dd678SMax Reitz assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX); 61b38dd678SMax Reitz 62b38dd678SMax Reitz return qis_equal[x->type](x, y); 63b38dd678SMax Reitz } 64