diff --git a/src/base/shared_ptr.h b/src/base/shared_ptr.h index a2ca97c47..d239aeba1 100644 --- a/src/base/shared_ptr.h +++ b/src/base/shared_ptr.h @@ -7,10 +7,20 @@ #ifndef BASE_SHARED_PTR_H_INCLUDED #define BASE_SHARED_PTR_H_INCLUDED +template +class DefaultSharedPtrDeleter +{ +public: + static void destroy(T* ptr) + { + delete ptr; + } +}; + // A class which wraps a pointer and keeps reference counting to // automatically delete the pointed object when it is no longer // used. -template +template > class SharedPtr { public: @@ -26,14 +36,14 @@ public: ref(); } - SharedPtr(const SharedPtr& other) + SharedPtr(const SharedPtr& other) : m_ptr(other.m_ptr) , m_refCount(other.m_refCount) { ref(); } - template - SharedPtr(const SharedPtr& other) + template + SharedPtr(const SharedPtr& other) : m_ptr(static_cast(other.m_ptr)) , m_refCount(other.m_refCount) { ref(); @@ -52,7 +62,7 @@ public: } } - SharedPtr& operator=(const SharedPtr& other) { + SharedPtr& operator=(const SharedPtr& other) { if (m_ptr != other.m_ptr) { unref(); m_ptr = other.m_ptr; @@ -62,8 +72,8 @@ public: return *this; } - template - SharedPtr& operator=(const SharedPtr& other) { + template + SharedPtr& operator=(const SharedPtr& other) { if (m_ptr != static_cast(other.m_ptr)) { unref(); m_ptr = static_cast(other.m_ptr); @@ -92,7 +102,7 @@ private: void unref() { if (m_ptr) { if (--(*m_refCount) == 0) { - delete m_ptr; + Deleter::destroy(m_ptr); delete m_refCount; } m_ptr = 0; @@ -104,21 +114,21 @@ private: T* m_ptr; // The pointee object. int* m_refCount; // Number of references. - template friend class SharedPtr; + template friend class SharedPtr; }; // Compares if two shared-pointers points to the same place (object, // memory address). -template -bool operator==(const SharedPtr& ptr1, const SharedPtr& ptr2) +template +bool operator==(const SharedPtr& ptr1, const SharedPtr& ptr2) { return ptr1.get() == ptr2.get(); } // Compares if two shared-pointers points to different places // (objects, memory addresses). -template -bool operator!=(const SharedPtr& ptr1, const SharedPtr& ptr2) +template +bool operator!=(const SharedPtr& ptr1, const SharedPtr& ptr2) { return ptr1.get() != ptr2.get(); }