/* * $Header: /home/dscott/nasty/memory/refcount.h,v 1.1 2001/11/06 21:48:51 dscott Exp $ * $NoKeywords: $ */ #ifndef REFCOUNT_H_ #define REFCOUNT_H_ 1 #include <autohndl.h> class base_ref_counted { public: base_ref_counted() : pCount_(new unsigned int(1)) {} base_ref_counted(const base_ref_counted& that) : pCount_(that.pCount_) { ++*pCount_; } virtual ~base_ref_counted() { if (--*pCount_ == 0) { delete pCount_; } } base_ref_counted& operator=( const base_ref_counted& rhs) { base_ref_counted tmp(rhs); this->swap(tmp); return *this; } void swap(base_ref_counted& that) throw() { std::swap(pCount_, that.pCount_); } unsigned int get_count() const { return *pCount_; } private: unsigned int* pCount_; }; // A reference counted class, // can be used as alternate base // of auto_handle (instead of // base_handle) template<class H, class T = handle_traits<H> > class ref_counted : public base_ref_counted { public: void dispose(H h) throw() { if (get_count() == 1) { T::dispose(h); } } static H copy(H& h) throw() { return h; } }; #endif//REFCOUNT_H_
Hosted by www.Geocities.ws

1