qobject.c 2.0 KB

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