From d07f55fb1a0ff7f7e4a7d6bfd00eb9f840d8534e Mon Sep 17 00:00:00 2001 From: Markus Blatt <mblatt@dune-project.org> Date: Wed, 10 Nov 2004 15:49:54 +0000 Subject: [PATCH] Added method for deallocation. [[Imported from SVN: r1047]] --- common/smartpointer.hh | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/common/smartpointer.hh b/common/smartpointer.hh index 9b0af168a..e36d39923 100644 --- a/common/smartpointer.hh +++ b/common/smartpointer.hh @@ -11,12 +11,12 @@ * pointer. * @author Markus Blatt */ -namespace Dune { +namespace Dune +{ /** @addtogroup Common * * @{ */ - int smcounter=0; /** * @brief A reference counting smart pointer. * @@ -25,7 +25,8 @@ namespace Dune { * references to it. */ template<class T> - class SmartPointer { + class SmartPointer + { public: /** * @brief The data type we are a pointer for. @@ -60,6 +61,11 @@ namespace Dune { inline const MemberType* operator->() const; + /** + * @brief Deallocates the references object if no other + * pointers reference it. + */ + inline void deallocate(); int count() const; private: /** @brief The object we reference. */ @@ -79,7 +85,6 @@ namespace Dune { inline SmartPointer<T>::SmartPointer() { rep_ = new PointerRep(MemberType()); - //std::cout<<"Constructing "<<smcounter++<<std::endl; } template<class T> @@ -101,7 +106,6 @@ namespace Dune { inline SmartPointer<T>::~SmartPointer() { if(rep_!=0 && --(rep_->count_)==0) { - //std::cout<< "Deleting! "<<rep_->count_<<" "<<rep_<<std::endl; delete rep_; rep_=0; } @@ -137,6 +141,13 @@ namespace Dune { return rep_->count_; } + template<class T> + inline void SmartPointer<T>::deallocate() + { + assert(rep_!=0 && rep_->count_==1); + delete rep_; + rep_=0; + } /** @} */ } #endif -- GitLab