implement Poco::SharedPtr using std::shared_ptr (#1993)

This commit is contained in:
Günter Obiltschnig 2017-11-13 18:43:55 +01:00 committed by Aleksandar Fabijanic
parent 3ada2d9084
commit e12458a63a
2 changed files with 83 additions and 171 deletions

View File

@ -20,144 +20,64 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/AtomicCounter.h" #include <memory>
#include <algorithm> #include <algorithm>
namespace Poco { namespace Poco {
class ReferenceCounter //@ deprecated
/// Simple ReferenceCounter object, does not delete itself when count reaches 0.
{
public:
ReferenceCounter(): _cnt(1)
{
}
void duplicate()
{
++_cnt;
}
int release()
{
return --_cnt;
}
int referenceCount() const
{
return _cnt.value();
}
private:
AtomicCounter _cnt;
};
template <class C> template <class C>
class ReleasePolicy
/// The default release policy for SharedPtr, which
/// simply uses the delete operator to delete an object.
{
public:
static void release(C* pObj)
/// Delete the object.
/// Note that pObj can be 0.
{
delete pObj;
}
};
template <class C>
class ReleaseArrayPolicy
/// The release policy for SharedPtr holding arrays.
{
public:
static void release(C* pObj)
/// Delete the object.
/// Note that pObj can be 0.
{
delete [] pObj;
}
};
template <class C, class RC = ReferenceCounter, class RP = ReleasePolicy<C> >
class SharedPtr class SharedPtr
/// SharedPtr is a "smart" pointer for classes implementing /// As of POCO C++ Libraries Release 2.0, SharedPtr is a small wrapper around
/// reference counting based garbage collection. /// std::shared_ptr, providing basic backwards compatibility to the 1.x SharedPtr
/// SharedPtr is thus similar to AutoPtr. Unlike the /// implementation.
/// AutoPtr template, which can only be used with
/// classes that support reference counting, SharedPtr
/// can be used with any class. For this to work, a
/// SharedPtr manages a reference count for the object
/// it manages.
/// ///
/// SharedPtr works in the following way: /// Note: ReleasePolicy is no longer supported.
/// If an SharedPtr is assigned an ordinary pointer to ///
/// an object (via the constructor or the assignment operator), /// SharedPtr will be removed in a future release. New code should use
/// it takes ownership of the object and the object's reference /// std::shared_ptr and existing code should be changed to use std::shared_ptr
/// count is initialized to one. /// instead of Poco::SharedPtr.
/// If the SharedPtr is assigned another SharedPtr, the
/// object's reference count is incremented by one.
/// The destructor of SharedPtr decrements the object's
/// reference count by one and deletes the object if the
/// reference count reaches zero.
/// SharedPtr supports dereferencing with both the ->
/// and the * operator. An attempt to dereference a null
/// SharedPtr results in a NullPointerException being thrown.
/// SharedPtr also implements all relational operators and
/// a cast operator in case dynamic casting of the encapsulated data types
/// is required.
{ {
public: public:
SharedPtr(): _pCounter(new RC), _ptr(0) SharedPtr()
{ {
} }
SharedPtr(C* ptr) SharedPtr(C* ptr):
try:
_pCounter(new RC),
_ptr(ptr) _ptr(ptr)
{ {
} }
catch (...)
SharedPtr(const std::shared_ptr<C>& ptr):
_ptr(ptr)
{ {
RP::release(ptr);
} }
template <class Other, class OtherRP> template <class Other>
SharedPtr(const SharedPtr<Other, RC, OtherRP>& ptr): _pCounter(ptr._pCounter), _ptr(const_cast<Other*>(ptr.get())) SharedPtr(const SharedPtr<Other>& ptr):
_ptr(std::dynamic_pointer_cast<C>(ptr.shared_ptr()))
{ {
_pCounter->duplicate();
} }
SharedPtr(const SharedPtr& ptr): _pCounter(ptr._pCounter), _ptr(ptr._ptr) SharedPtr(const SharedPtr& ptr):
_ptr(ptr._ptr)
{
}
SharedPtr(SharedPtr&& ptr):
_ptr(std::move(ptr._ptr))
{ {
_pCounter->duplicate();
} }
~SharedPtr() ~SharedPtr()
{ {
try
{
release();
}
catch (...)
{
poco_unexpected();
}
} }
SharedPtr& assign(C* ptr) SharedPtr& assign(C* ptr)
{ {
if (get() != ptr) _ptr.reset(ptr);
{
SharedPtr tmp(ptr);
swap(tmp);
}
return *this; return *this;
} }
@ -171,8 +91,8 @@ public:
return *this; return *this;
} }
template <class Other, class OtherRP> template <class Other>
SharedPtr& assign(const SharedPtr<Other, RC, OtherRP>& ptr) SharedPtr& assign(const SharedPtr<Other>& ptr)
{ {
if (ptr.get() != _ptr) if (ptr.get() != _ptr)
{ {
@ -192,8 +112,8 @@ public:
return assign(ptr); return assign(ptr);
} }
template <class Other, class OtherRP> template <class Other>
SharedPtr& operator = (const SharedPtr<Other, RC, OtherRP>& ptr) SharedPtr& operator = (const SharedPtr<Other>& ptr)
{ {
return assign<Other>(ptr); return assign<Other>(ptr);
} }
@ -201,11 +121,10 @@ public:
void swap(SharedPtr& ptr) void swap(SharedPtr& ptr)
{ {
std::swap(_ptr, ptr._ptr); std::swap(_ptr, ptr._ptr);
std::swap(_pCounter, ptr._pCounter);
} }
template <class Other> template <class Other>
SharedPtr<Other, RC, RP> cast() const SharedPtr<Other> cast() const
/// Casts the SharedPtr via a dynamic cast to the given type. /// Casts the SharedPtr via a dynamic cast to the given type.
/// Returns an SharedPtr containing NULL if the cast fails. /// Returns an SharedPtr containing NULL if the cast fails.
/// Example: (assume class Sub: public Super) /// Example: (assume class Sub: public Super)
@ -213,22 +132,33 @@ public:
/// SharedPtr<Sub> sub = super.cast<Sub>(); /// SharedPtr<Sub> sub = super.cast<Sub>();
/// poco_assert (sub.get()); /// poco_assert (sub.get());
{ {
Other* pOther = dynamic_cast<Other*>(_ptr); return SharedPtr(std::dynamic_pointer_cast<Other>(_ptr));
if (pOther)
return SharedPtr<Other, RC, RP>(_pCounter, pOther);
return SharedPtr<Other, RC, RP>();
} }
template <class Other> template <class Other>
SharedPtr<Other, RC, RP> unsafeCast() const SharedPtr<Other> unsafeCast() const
/// Casts the SharedPtr via a static cast to the given type. /// Casts the SharedPtr via a static cast to the given type.
/// Example: (assume class Sub: public Super) /// Example: (assume class Sub: public Super)
/// SharedPtr<Super> super(new Sub()); /// SharedPtr<Super> super(new Sub());
/// SharedPtr<Sub> sub = super.unsafeCast<Sub>(); /// SharedPtr<Sub> sub = super.unsafeCast<Sub>();
/// poco_assert (sub.get()); /// poco_assert (sub.get());
{ {
Other* pOther = static_cast<Other*>(_ptr); return SharedPtr(std::static_pointer_cast<Other>(_ptr));
return SharedPtr<Other, RC, RP>(_pCounter, pOther); }
operator std::shared_ptr<C>()
{
return _ptr;
}
const std::shared_ptr<C>& shared_ptr() const
{
return _ptr;
}
std::shared_ptr<C>& shared_ptr()
{
return _ptr;
} }
C* operator -> () C* operator -> ()
@ -253,22 +183,22 @@ public:
C* get() C* get()
{ {
return _ptr; return _ptr.get();
} }
const C* get() const const C* get() const
{ {
return _ptr; return _ptr.get();
} }
operator C* () operator C* ()
{ {
return _ptr; return _ptr.get();
} }
operator const C* () const operator const C* () const
{ {
return _ptr; return _ptr.get();
} }
bool operator ! () const bool operator ! () const
@ -371,9 +301,9 @@ public:
return get() >= ptr; return get() >= ptr;
} }
int referenceCount() const long referenceCount() const
{ {
return _pCounter->referenceCount(); return _ptr.use_count();
} }
protected: protected:
@ -382,40 +312,16 @@ protected:
if (!_ptr) if (!_ptr)
throw NullPointerException(); throw NullPointerException();
return _ptr; return _ptr.get();
}
void release()
{
poco_assert_dbg (_pCounter);
int i = _pCounter->release();
if (i == 0)
{
RP::release(_ptr);
_ptr = 0;
delete _pCounter;
_pCounter = 0;
}
}
SharedPtr(RC* pCounter, C* ptr): _pCounter(pCounter), _ptr(ptr)
/// for cast operation
{
poco_assert_dbg (_pCounter);
_pCounter->duplicate();
} }
protected: protected:
RC* _pCounter; std::shared_ptr<C> _ptr;
C* _ptr;
template <class OtherC, class OtherRC, class OtherRP> friend class SharedPtr;
}; };
template <class C, class RC, class RP> template <class C>
inline void swap(SharedPtr<C, RC, RP>& p1, SharedPtr<C, RC, RP>& p2) inline void swap(SharedPtr<C>& p1, SharedPtr<C>& p2)
{ {
p1.swap(p2); p1.swap(p2);
} }

View File

@ -155,6 +155,12 @@ void SharedPtrTest::testSharedPtr()
assert (TestObject::count() == 1); assert (TestObject::count() == 1);
} }
assert (TestObject::count() == 0); assert (TestObject::count() == 0);
std::shared_ptr<TestObject> stdp(std::make_shared<TestObject>(""));
Poco::SharedPtr<TestObject> ptr5(stdp);
std::shared_ptr<TestObject> stdp2 = ptr5;
assert (stdp == stdp2);
} }