You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

175 lines
5.3 KiB

3 months ago
  1. #if !defined(__OBJECT_H)
  2. #define __OBJECT_H
  3. #include <atomic>
  4. #include "constructor_stats.h"
  5. /// Reference counted object base class
  6. class Object {
  7. public:
  8. /// Default constructor
  9. Object() { print_default_created(this); }
  10. /// Copy constructor
  11. Object(const Object &) : m_refCount(0) { print_copy_created(this); }
  12. /// Return the current reference count
  13. int getRefCount() const { return m_refCount; };
  14. /// Increase the object's reference count by one
  15. void incRef() const { ++m_refCount; }
  16. /** \brief Decrease the reference count of
  17. * the object and possibly deallocate it.
  18. *
  19. * The object will automatically be deallocated once
  20. * the reference count reaches zero.
  21. */
  22. void decRef(bool dealloc = true) const {
  23. --m_refCount;
  24. if (m_refCount == 0 && dealloc)
  25. delete this;
  26. else if (m_refCount < 0)
  27. throw std::runtime_error("Internal error: reference count < 0!");
  28. }
  29. virtual std::string toString() const = 0;
  30. protected:
  31. /** \brief Virtual protected deconstructor.
  32. * (Will only be called by \ref ref)
  33. */
  34. virtual ~Object() { print_destroyed(this); }
  35. private:
  36. mutable std::atomic<int> m_refCount { 0 };
  37. };
  38. // Tag class used to track constructions of ref objects. When we track constructors, below, we
  39. // track and print out the actual class (e.g. ref<MyObject>), and *also* add a fake tracker for
  40. // ref_tag. This lets us check that the total number of ref<Anything> constructors/destructors is
  41. // correct without having to check each individual ref<Whatever> type individually.
  42. class ref_tag {};
  43. /**
  44. * \brief Reference counting helper
  45. *
  46. * The \a ref refeference template is a simple wrapper to store a
  47. * pointer to an object. It takes care of increasing and decreasing
  48. * the reference count of the object. When the last reference goes
  49. * out of scope, the associated object will be deallocated.
  50. *
  51. * \ingroup libcore
  52. */
  53. template <typename T> class ref {
  54. public:
  55. /// Create a nullptr reference
  56. ref() : m_ptr(nullptr) { print_default_created(this); track_default_created((ref_tag*) this); }
  57. /// Construct a reference from a pointer
  58. ref(T *ptr) : m_ptr(ptr) {
  59. if (m_ptr) ((Object *) m_ptr)->incRef();
  60. print_created(this, "from pointer", m_ptr); track_created((ref_tag*) this, "from pointer");
  61. }
  62. /// Copy constructor
  63. ref(const ref &r) : m_ptr(r.m_ptr) {
  64. if (m_ptr)
  65. ((Object *) m_ptr)->incRef();
  66. print_copy_created(this, "with pointer", m_ptr); track_copy_created((ref_tag*) this);
  67. }
  68. /// Move constructor
  69. ref(ref &&r) : m_ptr(r.m_ptr) {
  70. r.m_ptr = nullptr;
  71. print_move_created(this, "with pointer", m_ptr); track_move_created((ref_tag*) this);
  72. }
  73. /// Destroy this reference
  74. ~ref() {
  75. if (m_ptr)
  76. ((Object *) m_ptr)->decRef();
  77. print_destroyed(this); track_destroyed((ref_tag*) this);
  78. }
  79. /// Move another reference into the current one
  80. ref& operator=(ref&& r) {
  81. print_move_assigned(this, "pointer", r.m_ptr); track_move_assigned((ref_tag*) this);
  82. if (*this == r)
  83. return *this;
  84. if (m_ptr)
  85. ((Object *) m_ptr)->decRef();
  86. m_ptr = r.m_ptr;
  87. r.m_ptr = nullptr;
  88. return *this;
  89. }
  90. /// Overwrite this reference with another reference
  91. ref& operator=(const ref& r) {
  92. print_copy_assigned(this, "pointer", r.m_ptr); track_copy_assigned((ref_tag*) this);
  93. if (m_ptr == r.m_ptr)
  94. return *this;
  95. if (m_ptr)
  96. ((Object *) m_ptr)->decRef();
  97. m_ptr = r.m_ptr;
  98. if (m_ptr)
  99. ((Object *) m_ptr)->incRef();
  100. return *this;
  101. }
  102. /// Overwrite this reference with a pointer to another object
  103. ref& operator=(T *ptr) {
  104. print_values(this, "assigned pointer"); track_values((ref_tag*) this, "assigned pointer");
  105. if (m_ptr == ptr)
  106. return *this;
  107. if (m_ptr)
  108. ((Object *) m_ptr)->decRef();
  109. m_ptr = ptr;
  110. if (m_ptr)
  111. ((Object *) m_ptr)->incRef();
  112. return *this;
  113. }
  114. /// Compare this reference with another reference
  115. bool operator==(const ref &r) const { return m_ptr == r.m_ptr; }
  116. /// Compare this reference with another reference
  117. bool operator!=(const ref &r) const { return m_ptr != r.m_ptr; }
  118. /// Compare this reference with a pointer
  119. bool operator==(const T* ptr) const { return m_ptr == ptr; }
  120. /// Compare this reference with a pointer
  121. bool operator!=(const T* ptr) const { return m_ptr != ptr; }
  122. /// Access the object referenced by this reference
  123. T* operator->() { return m_ptr; }
  124. /// Access the object referenced by this reference
  125. const T* operator->() const { return m_ptr; }
  126. /// Return a C++ reference to the referenced object
  127. T& operator*() { return *m_ptr; }
  128. /// Return a const C++ reference to the referenced object
  129. const T& operator*() const { return *m_ptr; }
  130. /// Return a pointer to the referenced object
  131. operator T* () { return m_ptr; }
  132. /// Return a const pointer to the referenced object
  133. T* get_ptr() { return m_ptr; }
  134. /// Return a pointer to the referenced object
  135. const T* get_ptr() const { return m_ptr; }
  136. private:
  137. T *m_ptr;
  138. };
  139. #endif /* __OBJECT_H */