#ifndef SHRPTR #define SHRPTR 1 template struct store_object { explicit store_object( TYPE* ptr_ ) : _ptr(ptr_), _ref_count(1), _ptr_count(1) { } TYPE * _ptr; unsigned int _ref_count; unsigned int _ptr_count; }; template class shared_ptr { friend class weak_ptr; public: shared_ptr( const shared_ptr& src_ ) : _val(src_._val) { _val->_ref_count += 1; _val->_ptr_count += 1; } ~shared_ptr( void ) { _val->_ref_count -= 1; if ( _val->_ref_count == 0 ) { delete _val->_ptr; _val->_ptr = 0; } _val->_ptr_count -= 1; if ( _val->_ptr_count == 0 ) { delete _val; } } private: shared_ptr( store_object * val_ ) : _val(val_) { _val->_ref_count += 1; _val->_ptr_count += 1; } store_object * _val; }; template class weak_ptr { public: weak_ptr( const shared_ptr& src_ ) : _val(src_._val) { _val->_ptr_count += 1; } ~weak_ptr( void ) { _val->_ptr_count -= 1; if ( _val->_ptr_count == 0 ) { delete _val; } } shared_ptr lock( void ) const { if ( _val->_ref_count > 0 ) { return shared_ptr(_val); } return shared_ptr(0); } private: store_object * _val; }; #endif