xref: /qemu/qobject/qobject.c (revision 6b67395762a4c8b6ca94364e0a0f616a6470c46a)
1 /*
2  * QObject
3  *
4  * Copyright (C) 2015 Red Hat, Inc.
5  *
6  * This work is licensed under the terms of the GNU LGPL, version 2.1
7  * or later.  See the COPYING.LIB file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "qemu-common.h"
12 #include "qapi/qmp/qbool.h"
13 #include "qapi/qmp/qdict.h"
14 #include "qapi/qmp/qstring.h"
15 
16 static void (*qdestroy[QTYPE__MAX])(QObject *) = {
17     [QTYPE_NONE] = NULL,               /* No such object exists */
18     [QTYPE_QNULL] = NULL,              /* qnull_ is indestructible */
19     [QTYPE_QNUM] = qnum_destroy_obj,
20     [QTYPE_QSTRING] = qstring_destroy_obj,
21     [QTYPE_QDICT] = qdict_destroy_obj,
22     [QTYPE_QLIST] = qlist_destroy_obj,
23     [QTYPE_QBOOL] = qbool_destroy_obj,
24 };
25 
26 void qobject_destroy(QObject *obj)
27 {
28     assert(!obj->refcnt);
29     assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX);
30     qdestroy[obj->type](obj);
31 }
32 
33 
34 static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) = {
35     [QTYPE_NONE] = NULL,               /* No such object exists */
36     [QTYPE_QNULL] = qnull_is_equal,
37     [QTYPE_QNUM] = qnum_is_equal,
38     [QTYPE_QSTRING] = qstring_is_equal,
39     [QTYPE_QDICT] = qdict_is_equal,
40     [QTYPE_QLIST] = qlist_is_equal,
41     [QTYPE_QBOOL] = qbool_is_equal,
42 };
43 
44 bool qobject_is_equal(const QObject *x, const QObject *y)
45 {
46     /* We cannot test x == y because an object does not need to be
47      * equal to itself (e.g. NaN floats are not). */
48 
49     if (!x && !y) {
50         return true;
51     }
52 
53     if (!x || !y || x->type != y->type) {
54         return false;
55     }
56 
57     assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX);
58 
59     return qis_equal[x->type](x, y);
60 }
61