1 #if !defined(__OBJECT_H) 29 void decRef(
bool dealloc =
true)
const {
34 throw std::runtime_error(
"Internal error: reference count < 0!");
38 virtual std::string
toString()
const = 0;
70 ref() : m_ptr(nullptr) {
76 explicit ref(
T *ptr) : m_ptr(ptr) {
78 ((
Object *) m_ptr)->incRef();
88 ((
Object *) m_ptr)->incRef();
96 ref(
ref &&r) noexcept : m_ptr(r.m_ptr) {
106 ((
Object *) m_ptr)->decRef();
122 ((
Object *) m_ptr)->decRef();
137 if (m_ptr == r.
m_ptr) {
141 ((
Object *) m_ptr)->decRef();
145 ((
Object *) m_ptr)->incRef();
159 ((
Object *) m_ptr)->decRef();
163 ((
Object *) m_ptr)->incRef();
193 explicit operator T *() {
return m_ptr; }
Reference counted object base class.
const T * operator->() const
Access the object referenced by this reference.
~ref()
Destroy this reference.
ref & operator=(const ref &r)
Overwrite this reference with another reference.
void track_copy_assigned(T *, Values &&...values)
void print_destroyed(T *inst, Values &&...values)
ref & operator=(T *ptr)
Overwrite this reference with a pointer to another object.
void print_copy_assigned(T *inst, Values &&...values)
void print_copy_created(T *inst, Values &&...values)
int getRefCount() const
Return the current reference count.
ref & operator=(ref &&r) noexcept
Move another reference into the current one.
const T * get_ptr() const
Return a pointer to the referenced object.
const T & operator*() const
Return a const C++ reference to the referenced object.
T & operator*()
Return a C++ reference to the referenced object.
bool operator==(const ref &r) const
Compare this reference with another reference.
void track_move_created(T *inst)
std::atomic< int > m_refCount
Object(const Object &)
Copy constructor.
void print_default_created(T *inst, Values &&...values)
T * operator->()
Access the object referenced by this reference.
ref(ref &&r) noexcept
Move constructor.
void print_values(T *inst, Values &&...values)
ref()
Create a nullptr reference.
void print_move_assigned(T *inst, Values &&...values)
void track_copy_created(T *inst)
void track_destroyed(T *inst)
Reference counting helper.
ref(T *ptr)
Construct a reference from a pointer.
Object()
Default constructor.
void track_move_assigned(T *, Values &&...values)
bool operator!=(const ref &r) const
Compare this reference with another reference.
virtual std::string toString() const =0
void print_created(T *inst, Values &&...values)
ref(const ref &r)
Copy constructor.
bool operator==(const T *ptr) const
Compare this reference with a pointer.
virtual ~Object()
Virtual protected deconstructor. (Will only be called by ref)
bool operator!=(const T *ptr) const
Compare this reference with a pointer.
void print_move_created(T *inst, Values &&...values)
void track_created(T *inst, Values &&...values)
void track_default_created(T *inst, Values &&...values)
T * get_ptr()
Return a const pointer to the referenced object.
void incRef() const
Increase the object's reference count by one.
void track_values(T *, Values &&...values)
void decRef(bool dealloc=true) const
Decrease the reference count of the object and possibly deallocate it.