qobject.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "qemu-common.h"
  11. #include "qapi/qmp/types.h"
  12. static void (*qdestroy[QTYPE__MAX])(QObject *) = {
  13. [QTYPE_NONE] = NULL, /* No such object exists */
  14. [QTYPE_QNULL] = NULL, /* qnull_ is indestructible */
  15. [QTYPE_QNUM] = qnum_destroy_obj,
  16. [QTYPE_QSTRING] = qstring_destroy_obj,
  17. [QTYPE_QDICT] = qdict_destroy_obj,
  18. [QTYPE_QLIST] = qlist_destroy_obj,
  19. [QTYPE_QBOOL] = qbool_destroy_obj,
  20. };
  21. void qobject_destroy(QObject *obj)
  22. {
  23. assert(!obj->refcnt);
  24. assert(QTYPE_QNULL < obj->type && obj->type < QTYPE__MAX);
  25. qdestroy[obj->type](obj);
  26. }
  27. static bool (*qis_equal[QTYPE__MAX])(const QObject *, const QObject *) = {
  28. [QTYPE_NONE] = NULL, /* No such object exists */
  29. [QTYPE_QNULL] = qnull_is_equal,
  30. [QTYPE_QNUM] = qnum_is_equal,
  31. [QTYPE_QSTRING] = qstring_is_equal,
  32. [QTYPE_QDICT] = qdict_is_equal,
  33. [QTYPE_QLIST] = qlist_is_equal,
  34. [QTYPE_QBOOL] = qbool_is_equal,
  35. };
  36. bool qobject_is_equal(const QObject *x, const QObject *y)
  37. {
  38. /* We cannot test x == y because an object does not need to be
  39. * equal to itself (e.g. NaN floats are not). */
  40. if (!x && !y) {
  41. return true;
  42. }
  43. if (!x || !y || x->type != y->type) {
  44. return false;
  45. }
  46. assert(QTYPE_NONE < x->type && x->type < QTYPE__MAX);
  47. return qis_equal[x->type](x, y);
  48. }