|
#if !defined(__OBJECT_H) |
|
# define __OBJECT_H |
|
|
|
# include "constructor_stats.h" |
|
|
|
# include <atomic> |
|
|
|
|
|
class Object { |
|
public: |
|
|
|
Object() { print_default_created(this); } |
|
|
|
|
|
Object(const Object &) : m_refCount(0) { print_copy_created(this); } |
|
|
|
|
|
int getRefCount() const { return m_refCount; }; |
|
|
|
|
|
void incRef() const { ++m_refCount; } |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void decRef(bool dealloc = true) const { |
|
--m_refCount; |
|
if (m_refCount == 0 && dealloc) { |
|
delete this; |
|
} else if (m_refCount < 0) { |
|
throw std::runtime_error("Internal error: reference count < 0!"); |
|
} |
|
} |
|
|
|
virtual std::string toString() const = 0; |
|
|
|
protected: |
|
|
|
|
|
|
|
virtual ~Object() { print_destroyed(this); } |
|
|
|
private: |
|
mutable std::atomic<int> m_refCount{0}; |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
class ref_tag {}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template <typename T> |
|
class ref { |
|
public: |
|
|
|
ref() : m_ptr(nullptr) { |
|
print_default_created(this); |
|
track_default_created((ref_tag *) this); |
|
} |
|
|
|
|
|
explicit ref(T *ptr) : m_ptr(ptr) { |
|
if (m_ptr) { |
|
((Object *) m_ptr)->incRef(); |
|
} |
|
|
|
print_created(this, "from pointer", m_ptr); |
|
track_created((ref_tag *) this, "from pointer"); |
|
} |
|
|
|
|
|
ref(const ref &r) : m_ptr(r.m_ptr) { |
|
if (m_ptr) { |
|
((Object *) m_ptr)->incRef(); |
|
} |
|
|
|
print_copy_created(this, "with pointer", m_ptr); |
|
track_copy_created((ref_tag *) this); |
|
} |
|
|
|
|
|
ref(ref &&r) noexcept : m_ptr(r.m_ptr) { |
|
r.m_ptr = nullptr; |
|
|
|
print_move_created(this, "with pointer", m_ptr); |
|
track_move_created((ref_tag *) this); |
|
} |
|
|
|
|
|
~ref() { |
|
if (m_ptr) { |
|
((Object *) m_ptr)->decRef(); |
|
} |
|
|
|
print_destroyed(this); |
|
track_destroyed((ref_tag *) this); |
|
} |
|
|
|
|
|
ref &operator=(ref &&r) noexcept { |
|
print_move_assigned(this, "pointer", r.m_ptr); |
|
track_move_assigned((ref_tag *) this); |
|
|
|
if (*this == r) { |
|
return *this; |
|
} |
|
if (m_ptr) { |
|
((Object *) m_ptr)->decRef(); |
|
} |
|
m_ptr = r.m_ptr; |
|
r.m_ptr = nullptr; |
|
return *this; |
|
} |
|
|
|
|
|
ref &operator=(const ref &r) { |
|
if (this == &r) { |
|
return *this; |
|
} |
|
print_copy_assigned(this, "pointer", r.m_ptr); |
|
track_copy_assigned((ref_tag *) this); |
|
|
|
if (m_ptr == r.m_ptr) { |
|
return *this; |
|
} |
|
if (m_ptr) { |
|
((Object *) m_ptr)->decRef(); |
|
} |
|
m_ptr = r.m_ptr; |
|
if (m_ptr) { |
|
((Object *) m_ptr)->incRef(); |
|
} |
|
return *this; |
|
} |
|
|
|
|
|
ref &operator=(T *ptr) { |
|
print_values(this, "assigned pointer"); |
|
track_values((ref_tag *) this, "assigned pointer"); |
|
|
|
if (m_ptr == ptr) { |
|
return *this; |
|
} |
|
if (m_ptr) { |
|
((Object *) m_ptr)->decRef(); |
|
} |
|
m_ptr = ptr; |
|
if (m_ptr) { |
|
((Object *) m_ptr)->incRef(); |
|
} |
|
return *this; |
|
} |
|
|
|
|
|
bool operator==(const ref &r) const { return m_ptr == r.m_ptr; } |
|
|
|
|
|
bool operator!=(const ref &r) const { return m_ptr != r.m_ptr; } |
|
|
|
|
|
bool operator==(const T *ptr) const { return m_ptr == ptr; } |
|
|
|
|
|
bool operator!=(const T *ptr) const { return m_ptr != ptr; } |
|
|
|
|
|
T *operator->() { return m_ptr; } |
|
|
|
|
|
const T *operator->() const { return m_ptr; } |
|
|
|
|
|
T &operator*() { return *m_ptr; } |
|
|
|
|
|
const T &operator*() const { return *m_ptr; } |
|
|
|
|
|
explicit operator T *() { return m_ptr; } |
|
|
|
|
|
T *get_ptr() { return m_ptr; } |
|
|
|
|
|
const T *get_ptr() const { return m_ptr; } |
|
|
|
private: |
|
T *m_ptr; |
|
}; |
|
|
|
#endif |
|
|