qobject.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * QEMU Object Model.
  3. *
  4. * Based on ideas by Avi Kivity <avi@redhat.com>
  5. *
  6. * Copyright (C) 2009, 2015 Red Hat Inc.
  7. *
  8. * Authors:
  9. * Luiz Capitulino <lcapitulino@redhat.com>
  10. *
  11. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  12. * See the COPYING.LIB file in the top-level directory.
  13. *
  14. * QObject Reference Counts Terminology
  15. * ------------------------------------
  16. *
  17. * - Returning references: A function that returns an object may
  18. * return it as either a weak or a strong reference. If the
  19. * reference is strong, you are responsible for calling
  20. * qobject_unref() on the reference when you are done.
  21. *
  22. * If the reference is weak, the owner of the reference may free it at
  23. * any time in the future. Before storing the reference anywhere, you
  24. * should call qobject_ref() to make the reference strong.
  25. *
  26. * - Transferring ownership: when you transfer ownership of a reference
  27. * by calling a function, you are no longer responsible for calling
  28. * qobject_unref() when the reference is no longer needed. In other words,
  29. * when the function returns you must behave as if the reference to the
  30. * passed object was weak.
  31. */
  32. #ifndef QOBJECT_H
  33. #define QOBJECT_H
  34. #include "qapi/qapi-builtin-types.h"
  35. /* Not for use outside include/qobject/ */
  36. struct QObjectBase_ {
  37. QType type;
  38. size_t refcnt;
  39. };
  40. /* this struct must have no other members than base */
  41. struct QObject {
  42. struct QObjectBase_ base;
  43. };
  44. /*
  45. * Preprocessor sorcery ahead: use a different identifier for the
  46. * local variable in each expansion, so we can nest macro calls
  47. * without shadowing variables.
  48. */
  49. #define QOBJECT_INTERNAL(obj, _obj) ({ \
  50. typeof(obj) _obj = (obj); \
  51. _obj ? container_of(&_obj->base, QObject, base) : NULL; \
  52. })
  53. #define QOBJECT(obj) QOBJECT_INTERNAL((obj), MAKE_IDENTIFIER(_obj))
  54. /* Required for qobject_to() */
  55. #define QTYPE_CAST_TO_QNull QTYPE_QNULL
  56. #define QTYPE_CAST_TO_QNum QTYPE_QNUM
  57. #define QTYPE_CAST_TO_QString QTYPE_QSTRING
  58. #define QTYPE_CAST_TO_QDict QTYPE_QDICT
  59. #define QTYPE_CAST_TO_QList QTYPE_QLIST
  60. #define QTYPE_CAST_TO_QBool QTYPE_QBOOL
  61. QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,
  62. "The QTYPE_CAST_TO_* list needs to be extended");
  63. #define qobject_to(type, obj) \
  64. ((type *)qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)))
  65. static inline void qobject_ref_impl(QObject *obj)
  66. {
  67. if (obj) {
  68. obj->base.refcnt++;
  69. }
  70. }
  71. /**
  72. * qobject_is_equal(): Return whether the two objects are equal.
  73. *
  74. * Any of the pointers may be NULL; return true if both are. Always
  75. * return false if only one is (therefore a QNull object is not
  76. * considered equal to a NULL pointer).
  77. */
  78. bool qobject_is_equal(const QObject *x, const QObject *y);
  79. /**
  80. * qobject_destroy(): Free resources used by the object
  81. * For use via qobject_unref() only!
  82. */
  83. void qobject_destroy(QObject *obj);
  84. static inline void qobject_unref_impl(QObject *obj)
  85. {
  86. assert(!obj || obj->base.refcnt);
  87. if (obj && --obj->base.refcnt == 0) {
  88. qobject_destroy(obj);
  89. }
  90. }
  91. /**
  92. * qobject_ref(): Increment QObject's reference count
  93. *
  94. * Returns: the same @obj. The type of @obj will be propagated to the
  95. * return type.
  96. */
  97. #define qobject_ref(obj) ({ \
  98. typeof(obj) _o = (obj); \
  99. qobject_ref_impl(QOBJECT(_o)); \
  100. _o; \
  101. })
  102. /**
  103. * qobject_unref(): Decrement QObject's reference count, deallocate
  104. * when it reaches zero
  105. */
  106. #define qobject_unref(obj) qobject_unref_impl(QOBJECT(obj))
  107. /**
  108. * qobject_type(): Return the QObject's type
  109. */
  110. static inline QType qobject_type(const QObject *obj)
  111. {
  112. assert(QTYPE_NONE < obj->base.type && obj->base.type < QTYPE__MAX);
  113. return obj->base.type;
  114. }
  115. /**
  116. * qobject_check_type(): Helper function for the qobject_to() macro.
  117. * Return @obj, but only if @obj is not NULL and @type is equal to
  118. * @obj's type. Return NULL otherwise.
  119. */
  120. static inline QObject *qobject_check_type(const QObject *obj, QType type)
  121. {
  122. if (obj && qobject_type(obj) == type) {
  123. return (QObject *)obj;
  124. } else {
  125. return NULL;
  126. }
  127. }
  128. #endif /* QOBJECT_H */