SharedPtr modification from Chris Jones (additional RC template arg that defaults to ReferenceCounter, thus fully backwards compatible)

This commit is contained in:
Guenter Obiltschnig
2008-05-07 16:33:25 +00:00
parent 6c014c25d3
commit c3d621c6fc

View File

@@ -9,7 +9,7 @@
// //
// Definition of the SharedPtr template class. // Definition of the SharedPtr template class.
// //
// Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH. // Copyright (c) 2005-2008, Applied Informatics Software Engineering GmbH.
// and Contributors. // and Contributors.
// //
// Permission is hereby granted, free of charge, to any person or organization // Permission is hereby granted, free of charge, to any person or organization
@@ -75,7 +75,7 @@ private:
}; };
template <class C> template <class C, class RC = ReferenceCounter>
class SharedPtr class SharedPtr
/// SharedPtr is a "smart" pointer for classes implementing /// SharedPtr is a "smart" pointer for classes implementing
/// reference counting based garbage collection. /// reference counting based garbage collection.
@@ -104,16 +104,16 @@ class SharedPtr
/// is required. /// is required.
{ {
public: public:
SharedPtr(): _pCounter(new ReferenceCounter), _ptr(0) SharedPtr(): _pCounter(new RC), _ptr(0)
{ {
} }
SharedPtr(C* ptr): _pCounter(new ReferenceCounter), _ptr(ptr) SharedPtr(C* ptr): _pCounter(new RC), _ptr(ptr)
{ {
} }
template <class Other> template <class Other>
SharedPtr(const SharedPtr<Other>& ptr): _pCounter(ptr._pCounter), _ptr(const_cast<Other*>(ptr.get())) SharedPtr(const SharedPtr<Other, RC>& ptr): _pCounter(ptr._pCounter), _ptr(const_cast<Other*>(ptr.get()))
{ {
_pCounter->duplicate(); _pCounter->duplicate();
} }
@@ -132,7 +132,7 @@ public:
{ {
if (get() != ptr) if (get() != ptr)
{ {
ReferenceCounter* pTmp = new ReferenceCounter; RC* pTmp = new RC;
release(); release();
_pCounter = pTmp; _pCounter = pTmp;
_ptr = ptr; _ptr = ptr;
@@ -151,7 +151,7 @@ public:
} }
template <class Other> template <class Other>
SharedPtr& assign(const SharedPtr<Other>& ptr) SharedPtr& assign(const SharedPtr<Other, RC>& ptr)
{ {
if (ptr.get() != _ptr) if (ptr.get() != _ptr)
{ {
@@ -172,7 +172,7 @@ public:
} }
template <class Other> template <class Other>
SharedPtr& operator = (const SharedPtr<Other>& ptr) SharedPtr& operator = (const SharedPtr<Other, RC>& ptr)
{ {
return assign<Other>(ptr); return assign<Other>(ptr);
} }
@@ -184,7 +184,7 @@ public:
} }
template <class Other> template <class Other>
SharedPtr<Other> cast() const SharedPtr<Other, RC> 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)
@@ -199,7 +199,7 @@ public:
} }
template <class Other> template <class Other>
SharedPtr<Other> unsafeCast() const SharedPtr<Other, RC> 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());
@@ -207,7 +207,7 @@ public:
/// poco_assert (sub.get()); /// poco_assert (sub.get());
{ {
Other* pOther = static_cast<Other*>(_ptr); Other* pOther = static_cast<Other*>(_ptr);
return SharedPtr<Other>(_pCounter, pOther); return SharedPtr<Other, RC>(_pCounter, pOther);
} }
C* operator -> () C* operator -> ()
@@ -374,7 +374,7 @@ private:
} }
} }
SharedPtr(ReferenceCounter* pCounter, C* ptr): _pCounter(pCounter), _ptr(ptr) SharedPtr(RC* pCounter, C* ptr): _pCounter(pCounter), _ptr(ptr)
/// for cast operation /// for cast operation
{ {
poco_assert_dbg (_pCounter); poco_assert_dbg (_pCounter);
@@ -382,15 +382,15 @@ private:
} }
private: private:
ReferenceCounter* _pCounter; RC* _pCounter;
C* _ptr; C* _ptr;
template <class Other> friend class SharedPtr; template <class Other, class RC> friend class SharedPtr;
}; };
template <class C> template <class C, class RC>
inline void swap(SharedPtr<C>& p1, SharedPtr<C>& p2) inline void swap(SharedPtr<C, RC>& p1, SharedPtr<C, RC>& p2)
{ {
p1.swap(p2); p1.swap(p2);
} }