diff options
Diffstat (limited to 'src/framework/Utilities/CountedReference')
3 files changed, 33 insertions, 0 deletions
diff --git a/src/framework/Utilities/CountedReference/Reference.h b/src/framework/Utilities/CountedReference/Reference.h index 49156178258..1e1083e1a66 100644 --- a/src/framework/Utilities/CountedReference/Reference.h +++ b/src/framework/Utilities/CountedReference/Reference.h @@ -17,8 +17,10 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef TRINITY_REFERENCE_H #define TRINITY_REFERENCE_H + /** * Referencer<T> * Referencer is an object that holds a reference holder that hold a reference @@ -29,10 +31,12 @@ * reference around. Objects can be reference counted in both single threaded * model and multi-threaded model */ + #include <stdexcept> #include "Platform/Define.h" #include "Policies/ThreadingModel.h" #include "ReferenceHolder.h" + template < typename T, @@ -43,39 +47,52 @@ class TRINITY_DLL_DECL Referencer typedef typename THREADING_MODEL::Lock Lock; typedef ReferenceHolder<T, THREADING_MODEL> ReferenceeHolder; public: + /// Constructs a referencer. Referencer(T *ref = NULL); + /// Copy constructor Referencer(const Referencer &obj) : i_holder(NULL) { *this = obj; } + /// Destructor ~Referencer(); + /// Referencee accessor T* referencee(void) { return (i_holder == NULL ? NULL : i_holder->i_referencee); } const T* referencee(void) const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + //T& referencee(void){ return _referencee(); } //const T& referencee(void) const { return const_cast<Referencer *>(this)->_referencee(); } operator T&(void) { return _referencee(); } operator const T&(void) const { return *const_cast<Referencer *>(this)->_referencee(); } + /// cast operators T* operator*() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } T const * operator*() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + /// overload operators T* operator->() { return (i_holder == NULL ? NULL : i_holder->i_referencee); } const T * operator->() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); } + /// operator = Referencer& operator=(const Referencer &obj); Referencer& operator=(T *); + /// returns true if i_referencee is null bool isNull(void) const { return i_holder == NULL; } + private: + T& _referencee(void) { if( i_holder == NULL ) throw std::runtime_error("Invalid access to null pointer"); return *i_holder->i_referencee; } + void deReference(ReferenceeHolder *); void addReference(ReferenceeHolder *); + // private data ReferenceeHolder *i_holder; }; diff --git a/src/framework/Utilities/CountedReference/ReferenceHolder.h b/src/framework/Utilities/CountedReference/ReferenceHolder.h index 80576a8700d..4cfb7d16ded 100644 --- a/src/framework/Utilities/CountedReference/ReferenceHolder.h +++ b/src/framework/Utilities/CountedReference/ReferenceHolder.h @@ -17,12 +17,15 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef TRINITY_REFERENCEHOLDER_H #define TRINITY_REFERENCEHOLDER_H + /** ReferenceHolder holds the actualy referenced obejct as well the refence count. The ReferenecHolder implements as a policy base object and will decided by the Reference class to be consnsitent. */ + template < typename T, diff --git a/src/framework/Utilities/CountedReference/ReferenceImpl.h b/src/framework/Utilities/CountedReference/ReferenceImpl.h index 22c31021bd8..c3116a21cf4 100644 --- a/src/framework/Utilities/CountedReference/ReferenceImpl.h +++ b/src/framework/Utilities/CountedReference/ReferenceImpl.h @@ -17,9 +17,12 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef TRINITY_REFERENCEIMPL_H #define TRINITY_REFERENCEIMPL_H + #include "Reference.h" + template < typename T, @@ -34,6 +37,7 @@ Referencer<T, THREADING_MODEL>::Referencer(T *ref) ++i_holder->i_referenceCount; } } + template < typename T, @@ -45,6 +49,7 @@ Referencer<T, THREADING_MODEL>::~Referencer() deReference(i_holder); i_holder = NULL; } + template < typename T, @@ -60,6 +65,7 @@ Referencer<T, THREADING_MODEL>::operator=(const Referencer<T, THREADING_MODEL> & i_holder = obj.i_holder; return *this; } + template < typename T, @@ -76,8 +82,10 @@ Referencer<T, THREADING_MODEL>::operator=(T *ref) i_holder = new ReferenceeHolder(ref); ++i_holder->i_referenceCount; } + return *this; } + template < typename T, @@ -88,21 +96,25 @@ Referencer<T, THREADING_MODEL>::deReference(ReferenceHolder<T, THREADING_MODEL> { assert( holder != NULL && holder->i_referenceCount > 0); bool delete_object = false; + { // The guard is within the scope due to the guard // must release earlier than expected. Lock guard(*holder); Guard(&guard); + --holder->i_referenceCount; if( holder->i_referenceCount == 0 ) delete_object = true; } + if( delete_object ) { delete holder->i_referencee; delete holder; } } + template < typename T, @@ -114,6 +126,7 @@ Referencer<T, THREADING_MODEL>::addReference(ReferenceHolder<T, THREADING_MODEL> assert( i_holder != NULL ); Lock guard(*holder); Guard(&guard); + ++holder->i_referenceCount; } #endif |
