qobject.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "qemu/osdep.h"
  10. #include "qobject/qbool.h"
  11. #include "qobject/qnull.h"
  12. #include "qobject/qnum.h"
  13. #include "qobject/qdict.h"
  14. #include "qobject/qlist.h"
  15. #include "qobject/qstring.h"
  16. #include "qobject-internal.h"
  17. QEMU_BUILD_BUG_MSG(
  18. offsetof(QNull, base) != 0 ||
  19. offsetof(QNum, base) != 0 ||
  20. offsetof(QString, base) != 0 ||
  21. offsetof(QDict, base) != 0 ||
  22. offsetof(QList, base) != 0 ||
  23. offsetof(QBool, base) != 0,
  24. "base qobject must be at offset 0");
  25. static void (*qdestroy[QTYPE__MAX])(QObject *) = {
  26. [QTYPE_NONE] = NULL, /* No such object exists */
  27. [QTYPE_QNULL] = NULL, /* qnull_ is indestructible */
  28. [QTYPE_QNUM] = qnum_destroy_obj,
  29. [QTYPE_QSTRING] = qstring_destroy_obj,
  30. [QTYPE_QDICT] = qdict_destroy_obj,
  31. [QTYPE_QLIST] = qlist_destroy_obj,
  32. [QTYPE_QBOOL] = qbool_destroy_obj,
  33. };
  34. void qobject_destroy(QObject *obj)
  35. {
  36. assert(!obj->base.refcnt);
  37. assert(QTYPE_QNULL < obj->base.type && obj->base.type < QTYPE__MAX);
  38. qdestroy[obj->base.type](obj);
  39. }
  40. static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) = {
  41. [QTYPE_NONE] = NULL, /* No such object exists */
  42. [QTYPE_QNULL] = qnull_is_equal,
  43. [QTYPE_QNUM] = qnum_is_equal,
  44. [QTYPE_QSTRING] = qstring_is_equal,
  45. [QTYPE_QDICT] = qdict_is_equal,
  46. [QTYPE_QLIST] = qlist_is_equal,
  47. [QTYPE_QBOOL] = qbool_is_equal,
  48. };
  49. bool qobject_is_equal(const QObject *x, const QObject *y)
  50. {
  51. /* We cannot test x == y because an object does not need to be
  52. * equal to itself (e.g. NaN floats are not). */
  53. if (!x && !y) {
  54. return true;
  55. }
  56. if (!x || !y || x->base.type != y->base.type) {
  57. return false;
  58. }
  59. assert(QTYPE_NONE < x->base.type && x->base.type < QTYPE__MAX);
  60. return qis_equal[x->base.type](x, y);
  61. }