#pragma once #include "CoreDefs.h" namespace PortabilityLayer { class RefCountedBase; template class RCPtr { public: RCPtr(); RCPtr(T *other); RCPtr(const RCPtr &other); #if IS_CPP11 RCPtr(RCPtr &&other); #endif ~RCPtr(); operator T*() const; T* operator ->() const; RCPtr &operator=(T *other); #if IS_CPP11 RCPtr &operator=(RCPtr &&other); #endif T *Get() const; private: RefCountedBase *m_target; }; } #include "RefCounted.h" template inline PortabilityLayer::RCPtr::RCPtr() : m_target(nullptr) { } template inline PortabilityLayer::RCPtr::RCPtr(const RCPtr &other) : m_target(other.m_target) { if (m_target) m_target->IncRef(); } template inline PortabilityLayer::RCPtr::RCPtr(T *other) : m_target(other) { if (other) other->IncRef(); } #if IS_CPP11 template inline PortabilityLayer::RCPtr::RCPtr(RCPtr &&other) : m_target(other.m_target) { other.m_target = nullptr; } #endif template inline PortabilityLayer::RCPtr::~RCPtr() { if (m_target) m_target->DecRef(); } template inline T *PortabilityLayer::RCPtr::operator T*() const { return m_target; } template inline T *PortabilityLayer::RCPtr::operator->() const { return m_target; } template inline PortabilityLayer::RCPtr::RCPtr &PortabilityLayer::RCPtr::operator=(T *other) { RefCountedBase *old = m_target; m_target = other; if (other) other->IncRef(); if (old) old->DecRef(); return *this; } #if IS_CPP11 template inline PortabilityLayer::RCPtr& PortabilityLayer::RCPtr::RCPtr &operator=(RCPtr &&other) { RefCountedBase *old = m_target; RefCountedBase *newRC = other.m_target; other.m_target = nullptr; m_target = newRC; if (newRC) newRC->IncRef(); if (old) old->DecRef(); return *this; } #endif