blob: d7077b8f2a9dadfe5b46563dfa011ca6244a9deb [file] [log] [blame]
Eric Blake55e18192015-12-01 22:20:45 -07001/*
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
Peter Maydellf2ad72b2016-01-29 17:50:01 +000010#include "qemu/osdep.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010011#include "qapi/qmp/qbool.h"
Markus Armbruster15280c32018-02-01 12:18:36 +010012#include "qapi/qmp/qnull.h"
13#include "qapi/qmp/qnum.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010014#include "qapi/qmp/qdict.h"
Markus Armbruster47e6b292018-02-01 12:18:38 +010015#include "qapi/qmp/qlist.h"
Markus Armbruster6b673952018-02-01 12:18:35 +010016#include "qapi/qmp/qstring.h"
Markus Armbruster80d71122020-12-11 18:11:40 +010017#include "qobject-internal.h"
Eric Blake55e18192015-12-01 22:20:45 -070018
Marc-André Lureau7ee9edf2018-04-19 17:01:41 +020019QEMU_BUILD_BUG_MSG(
20 offsetof(QNull, base) != 0 ||
21 offsetof(QNum, base) != 0 ||
22 offsetof(QString, base) != 0 ||
23 offsetof(QDict, base) != 0 ||
24 offsetof(QList, base) != 0 ||
25 offsetof(QBool, base) != 0,
26 "base qobject must be at offset 0");
27
Eric Blake7264f5c2015-12-01 22:20:47 -070028static void (*qdestroy[QTYPE__MAX])(QObject *) = {
Eric Blake55e18192015-12-01 22:20:45 -070029 [QTYPE_NONE] = NULL, /* No such object exists */
30 [QTYPE_QNULL] = NULL, /* qnull_ is indestructible */
Marc-André Lureau01b2ffc2017-06-07 20:35:58 +040031 [QTYPE_QNUM] = qnum_destroy_obj,
Eric Blake55e18192015-12-01 22:20:45 -070032 [QTYPE_QSTRING] = qstring_destroy_obj,
33 [QTYPE_QDICT] = qdict_destroy_obj,
34 [QTYPE_QLIST] = qlist_destroy_obj,
Eric Blake55e18192015-12-01 22:20:45 -070035 [QTYPE_QBOOL] = qbool_destroy_obj,
36};
37
38void qobject_destroy(QObject *obj)
39{
Marc-André Lureau3d3eaca2018-04-19 17:01:42 +020040 assert(!obj->base.refcnt);
41 assert(QTYPE_QNULL < obj->base.type && obj->base.type < QTYPE__MAX);
42 qdestroy[obj->base.type](obj);
Eric Blake55e18192015-12-01 22:20:45 -070043}
Max Reitzb38dd672017-11-14 19:01:25 +010044
45
46static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) = {
47 [QTYPE_NONE] = NULL, /* No such object exists */
48 [QTYPE_QNULL] = qnull_is_equal,
49 [QTYPE_QNUM] = qnum_is_equal,
50 [QTYPE_QSTRING] = qstring_is_equal,
51 [QTYPE_QDICT] = qdict_is_equal,
52 [QTYPE_QLIST] = qlist_is_equal,
53 [QTYPE_QBOOL] = qbool_is_equal,
54};
55
56bool qobject_is_equal(const QObject *x, const QObject *y)
57{
58 /* We cannot test x == y because an object does not need to be
59 * equal to itself (e.g. NaN floats are not). */
60
61 if (!x && !y) {
62 return true;
63 }
64
Marc-André Lureau3d3eaca2018-04-19 17:01:42 +020065 if (!x || !y || x->base.type != y->base.type) {
Max Reitzb38dd672017-11-14 19:01:25 +010066 return false;
67 }
68
Marc-André Lureau3d3eaca2018-04-19 17:01:42 +020069 assert(QTYPE_NONE < x->base.type && x->base.type < QTYPE__MAX);
Max Reitzb38dd672017-11-14 19:01:25 +010070
Marc-André Lureau3d3eaca2018-04-19 17:01:42 +020071 return qis_equal[x->base.type](x, y);
Max Reitzb38dd672017-11-14 19:01:25 +010072}