diff --git a/common/autoptr.hh b/common/autoptr.hh new file mode 100644 index 0000000000000000000000000000000000000000..5094c551b139d1fa7115758fbf684ec1f5e262da --- /dev/null +++ b/common/autoptr.hh @@ -0,0 +1,94 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#ifndef DUNE_AUTOPTR_HH +#define DUNE_AUTOPTR_HH + +namespace Dune { + /** \brief An auto pointer class */ + template <class Pointer> + class AutoPointer + { + //! Pointer to Object + Pointer * ptr_; + + //! number of copies that exist from this Object + mutable int *refCount_; + + //! true if we own the pointer + mutable bool owner_; + + public: + //! if a copy is made, the refcount is increased + inline AutoPointer(const AutoPointer<Pointer> & copy) + { + ptr_ = 0; + refCount_ = 0; + if(copy.ptr_) + { + ptr_ = copy.ptr_; + refCount_ = copy.refCount_; + (*refCount_)++; + // now we own the pointer + owner_ = true; + copy.owner_ = false; + } + } + + //! initialize the member variables + AutoPointer() : ptr_ (0) , refCount_ (0) , owner_(false) {} + + // store object pointer and create refCount + void store (Pointer * ptr) + { + assert(ptr_ == 0); + ptr_ = ptr; + if(ptr_) + { + int * tmp = new int; + refCount_ = tmp; + (*refCount_) = 1; + owner_ = true; + } + } + + //! set Stack free, if no more refences exist + ~AutoPointer() + { + if(refCount_ && ptr_) + { + (*refCount_)--; + if((*refCount_) <= 0) + { + if(ptr_) delete ptr_; + if(refCount_) delete refCount_; + owner_ = false; + } + } + } + + //! return object reference + Pointer & operator * () const + { + assert(ptr_ != 0); + assert(owner_); + return *ptr_; + } + + //! return object pointer + Pointer * operator -> () const + { + assert(ptr_ != 0); + assert(owner_); + return ptr_; + } + + private: + //! if copy is made than one more Reference exists + AutoPointer<Pointer> & operator = (const AutoPointer<Pointer> & copy) + { + assert(false); + return (*this); + } + }; +} // end namespace Dune +#endif