blob: 9235f19c20bff3afb59c6880a84c809205eff6ea [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001#if !defined(__OBJECT_H)
2#define __OBJECT_H
3
4#include <atomic>
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02005#include "constructor_stats.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
7/// Reference counted object base class
8class Object {
9public:
10 /// Default constructor
Jason Rhinelander3f589372016-08-07 13:05:26 -040011 Object() { print_default_created(this); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012
13 /// Copy constructor
Jason Rhinelander3f589372016-08-07 13:05:26 -040014 Object(const Object &) : m_refCount(0) { print_copy_created(this); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020015
Jason Rhinelander540ae612016-08-28 13:46:25 -040016 /// Return the current reference count
17 int getRefCount() const { return m_refCount; };
Wenzel Jakob38bd7112015-07-05 20:05:44 +020018
Jason Rhinelander540ae612016-08-28 13:46:25 -040019 /// Increase the object's reference count by one
20 void incRef() const { ++m_refCount; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020021
Jason Rhinelander540ae612016-08-28 13:46:25 -040022 /** \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)
Wenzel Jakob38bd7112015-07-05 20:05:44 +020031 delete this;
32 else if (m_refCount < 0)
Jason Rhinelander540ae612016-08-28 13:46:25 -040033 throw std::runtime_error("Internal error: reference count < 0!");
Wenzel Jakob38bd7112015-07-05 20:05:44 +020034 }
35
36 virtual std::string toString() const = 0;
37protected:
Jason Rhinelander540ae612016-08-28 13:46:25 -040038 /** \brief Virtual protected deconstructor.
39 * (Will only be called by \ref ref)
40 */
41 virtual ~Object() { print_destroyed(this); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020042private:
43 mutable std::atomic<int> m_refCount { 0 };
44};
45
Jason Rhinelander3f589372016-08-07 13:05:26 -040046// 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
Wenzel Jakob38bd7112015-07-05 20:05:44 +020052/**
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:
Jason Rhinelander540ae612016-08-28 13:46:25 -040064 /// Create a nullptr reference
Jason Rhinelander3f589372016-08-07 13:05:26 -040065 ref() : m_ptr(nullptr) { print_default_created(this); track_default_created((ref_tag*) this); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020066
67 /// Construct a reference from a pointer
Jason Rhinelander540ae612016-08-28 13:46:25 -040068 ref(T *ptr) : m_ptr(ptr) {
69 if (m_ptr) ((Object *) m_ptr)->incRef();
Jason Rhinelander3f589372016-08-07 13:05:26 -040070
71 print_created(this, "from pointer", m_ptr); track_created((ref_tag*) this, "from pointer");
72
Jason Rhinelander540ae612016-08-28 13:46:25 -040073 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020074
Jason Rhinelander540ae612016-08-28 13:46:25 -040075 /// Copy constructor
Wenzel Jakob38bd7112015-07-05 20:05:44 +020076 ref(const ref &r) : m_ptr(r.m_ptr) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020077 if (m_ptr)
78 ((Object *) m_ptr)->incRef();
Jason Rhinelander3f589372016-08-07 13:05:26 -040079
80 print_copy_created(this, "with pointer", m_ptr); track_copy_created((ref_tag*) this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020081 }
82
83 /// Move constructor
84 ref(ref &&r) : m_ptr(r.m_ptr) {
Wenzel Jakobfe342412016-09-06 13:02:29 +090085 r.m_ptr = nullptr;
Jason Rhinelander3f589372016-08-07 13:05:26 -040086
87 print_move_created(this, "with pointer", m_ptr); track_move_created((ref_tag*) this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020088 }
89
90 /// Destroy this reference
91 ~ref() {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020092 if (m_ptr)
93 ((Object *) m_ptr)->decRef();
Jason Rhinelander3f589372016-08-07 13:05:26 -040094
95 print_destroyed(this); track_destroyed((ref_tag*) this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020096 }
97
98 /// Move another reference into the current one
Jason Rhinelander540ae612016-08-28 13:46:25 -040099 ref& operator=(ref&& r) {
Jason Rhinelander3f589372016-08-07 13:05:26 -0400100 print_move_assigned(this, "pointer", r.m_ptr); track_move_assigned((ref_tag*) this);
101
Jason Rhinelander540ae612016-08-28 13:46:25 -0400102 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 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200110
Jason Rhinelander540ae612016-08-28 13:46:25 -0400111 /// Overwrite this reference with another reference
112 ref& operator=(const ref& r) {
Jason Rhinelander3f589372016-08-07 13:05:26 -0400113 print_copy_assigned(this, "pointer", r.m_ptr); track_copy_assigned((ref_tag*) this);
114
Jason Rhinelander540ae612016-08-28 13:46:25 -0400115 if (m_ptr == r.m_ptr)
116 return *this;
117 if (m_ptr)
118 ((Object *) m_ptr)->decRef();
119 m_ptr = r.m_ptr;
120 if (m_ptr)
121 ((Object *) m_ptr)->incRef();
122 return *this;
123 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200124
Jason Rhinelander540ae612016-08-28 13:46:25 -0400125 /// Overwrite this reference with a pointer to another object
126 ref& operator=(T *ptr) {
Jason Rhinelander3f589372016-08-07 13:05:26 -0400127 print_values(this, "assigned pointer"); track_values((ref_tag*) this, "assigned pointer");
128
Jason Rhinelander540ae612016-08-28 13:46:25 -0400129 if (m_ptr == ptr)
130 return *this;
131 if (m_ptr)
132 ((Object *) m_ptr)->decRef();
133 m_ptr = ptr;
134 if (m_ptr)
135 ((Object *) m_ptr)->incRef();
136 return *this;
137 }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200138
Jason Rhinelander540ae612016-08-28 13:46:25 -0400139 /// Compare this reference with another reference
140 bool operator==(const ref &r) const { return m_ptr == r.m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200141
Jason Rhinelander540ae612016-08-28 13:46:25 -0400142 /// Compare this reference with another reference
143 bool operator!=(const ref &r) const { return m_ptr != r.m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200144
Jason Rhinelander540ae612016-08-28 13:46:25 -0400145 /// Compare this reference with a pointer
146 bool operator==(const T* ptr) const { return m_ptr == ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200147
Jason Rhinelander540ae612016-08-28 13:46:25 -0400148 /// Compare this reference with a pointer
149 bool operator!=(const T* ptr) const { return m_ptr != ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200150
Jason Rhinelander540ae612016-08-28 13:46:25 -0400151 /// Access the object referenced by this reference
152 T* operator->() { return m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200153
Jason Rhinelander540ae612016-08-28 13:46:25 -0400154 /// Access the object referenced by this reference
155 const T* operator->() const { return m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200156
Jason Rhinelander540ae612016-08-28 13:46:25 -0400157 /// Return a C++ reference to the referenced object
158 T& operator*() { return *m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200159
Jason Rhinelander540ae612016-08-28 13:46:25 -0400160 /// Return a const C++ reference to the referenced object
161 const T& operator*() const { return *m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200162
Jason Rhinelander540ae612016-08-28 13:46:25 -0400163 /// Return a pointer to the referenced object
164 operator T* () { return m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200165
Jason Rhinelander540ae612016-08-28 13:46:25 -0400166 /// Return a const pointer to the referenced object
Dean Moldovanec009a72017-01-31 17:05:44 +0100167 T* get_ptr() { return m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200168
Jason Rhinelander540ae612016-08-28 13:46:25 -0400169 /// Return a pointer to the referenced object
Dean Moldovanec009a72017-01-31 17:05:44 +0100170 const T* get_ptr() const { return m_ptr; }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200171private:
Jason Rhinelander540ae612016-08-28 13:46:25 -0400172 T *m_ptr;
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200173};
174
175#endif /* __OBJECT_H */