mirror of
				https://github.com/pocoproject/poco.git
				synced 2025-10-27 19:10:20 +01:00 
			
		
		
		
	SharedPtr modification from Chris Jones (additional RC template arg that defaults to ReferenceCounter, thus fully backwards compatible)
This commit is contained in:
		| @@ -9,7 +9,7 @@ | ||||
| // | ||||
| // 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. | ||||
| // | ||||
| // 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 | ||||
| 	/// SharedPtr is a "smart" pointer for classes implementing | ||||
| 	/// reference counting based garbage collection. | ||||
| @@ -104,16 +104,16 @@ class SharedPtr | ||||
| 	/// is required. | ||||
| { | ||||
| 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>  | ||||
| 	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(); | ||||
| 	} | ||||
| @@ -132,7 +132,7 @@ public: | ||||
| 	{ | ||||
| 		if (get() != ptr) | ||||
| 		{ | ||||
| 			ReferenceCounter* pTmp = new ReferenceCounter; | ||||
| 			RC* pTmp = new RC; | ||||
| 			release(); | ||||
| 			_pCounter = pTmp; | ||||
| 			_ptr = ptr; | ||||
| @@ -151,7 +151,7 @@ public: | ||||
| 	} | ||||
| 	 | ||||
| 	template <class Other> | ||||
| 	SharedPtr& assign(const SharedPtr<Other>& ptr) | ||||
| 	SharedPtr& assign(const SharedPtr<Other, RC>& ptr) | ||||
| 	{ | ||||
| 		if (ptr.get() != _ptr) | ||||
| 		{ | ||||
| @@ -172,7 +172,7 @@ public: | ||||
| 	} | ||||
|  | ||||
| 	template <class Other> | ||||
| 	SharedPtr& operator = (const SharedPtr<Other>& ptr) | ||||
| 	SharedPtr& operator = (const SharedPtr<Other, RC>& ptr) | ||||
| 	{ | ||||
| 		return assign<Other>(ptr); | ||||
| 	} | ||||
| @@ -184,7 +184,7 @@ public: | ||||
| 	} | ||||
|  | ||||
| 	template <class Other>  | ||||
| 	SharedPtr<Other> cast() const | ||||
| 	SharedPtr<Other, RC> cast() const | ||||
| 		/// Casts the SharedPtr via a dynamic cast to the given type. | ||||
| 		/// Returns an SharedPtr containing NULL if the cast fails. | ||||
| 		/// Example: (assume class Sub: public Super) | ||||
| @@ -199,7 +199,7 @@ public: | ||||
| 	} | ||||
|  | ||||
| 	template <class Other>  | ||||
| 	SharedPtr<Other> unsafeCast() const | ||||
| 	SharedPtr<Other, RC> unsafeCast() const | ||||
| 		/// Casts the SharedPtr via a static cast to the given type. | ||||
| 		/// Example: (assume class Sub: public Super) | ||||
| 		///    SharedPtr<Super> super(new Sub()); | ||||
| @@ -207,7 +207,7 @@ public: | ||||
| 		///    poco_assert (sub.get()); | ||||
| 	{ | ||||
| 		Other* pOther = static_cast<Other*>(_ptr); | ||||
| 		return SharedPtr<Other>(_pCounter, pOther); | ||||
| 		return SharedPtr<Other, RC>(_pCounter, pOther); | ||||
| 	} | ||||
|  | ||||
| 	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 | ||||
| 	{ | ||||
| 		poco_assert_dbg (_pCounter); | ||||
| @@ -382,15 +382,15 @@ private: | ||||
| 	} | ||||
|  | ||||
| private: | ||||
| 	ReferenceCounter* _pCounter; | ||||
| 	C*                _ptr; | ||||
| 	RC* _pCounter; | ||||
| 	C*  _ptr; | ||||
|  | ||||
| 	template <class Other> friend class SharedPtr; | ||||
| 	template <class Other, class RC> friend class SharedPtr; | ||||
| }; | ||||
|  | ||||
|  | ||||
| template <class C> | ||||
| inline void swap(SharedPtr<C>& p1, SharedPtr<C>& p2) | ||||
| template <class C, class RC> | ||||
| inline void swap(SharedPtr<C, RC>& p1, SharedPtr<C, RC>& p2) | ||||
| { | ||||
| 	p1.swap(p2); | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Guenter Obiltschnig
					Guenter Obiltschnig