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