#pragma once #ifndef __PL_SCOPEDPTR_H__ #define __PL_SCOPEDPTR_H__ #include "CoreDefs.h" namespace PortabilityLayer { template class ScopedPtr { public: ScopedPtr(); ScopedPtr(T *ref); ~ScopedPtr(); void Swap(ScopedPtr &other); operator T*(); operator const T*() const; T *operator->(); const T *operator->() const; void Set(T *ref); private: ScopedPtr(const ScopedPtr &other) PL_DELETED; void operator=(const ScopedPtr &other) PL_DELETED; T *m_ref; }; } namespace PortabilityLayer { template inline ScopedPtr::ScopedPtr() : m_ref(nullptr) { } template inline ScopedPtr::ScopedPtr(T *ref) : m_ref(ref) { } template inline ScopedPtr::~ScopedPtr() { if (m_ref) delete m_ref; } template inline void ScopedPtr::Swap(ScopedPtr &other) { T *temp = m_ref; m_ref = other.m_ref; other.m_ref = temp; } template inline ScopedPtr::operator T*() { return m_ref; } template inline ScopedPtr::operator const T*() const { return m_ref; } template inline T *ScopedPtr::operator->() { return m_ref; } template inline const T *ScopedPtr::operator->() const { return m_ref; } template inline void ScopedPtr::Set(T *ref) { if (m_ref && m_ref != ref) delete m_ref; m_ref = ref; } } #endif