// smartp.tcc - Copyright 2005, Matteo Settenvini // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA #ifndef SMARTP_TCC #define SMARTP_TCC 1 #include "smartp.hh" namespace memory { template const smart_ptr smart_ptr::null = 0; // ------------------------------ template smart_ptr::smart_ptr( T* ptr ) : _contents( new contents_type() ) { _contents->rc = 1; _contents->ptr = ptr; } template smart_ptr::smart_ptr( const smart_ptr& sptr ) : _contents(sptr._contents) { (_contents->rc)++; } template smart_ptr::~smart_ptr() { if( --(_contents->rc) == 0 ) { if( _contents->ptr != 0 ) !isArray ? delete _contents->ptr : delete [] _contents->ptr; delete _contents; } } // ------------------------------ template typename smart_ptr::smart_ptr& smart_ptr::operator=( const smart_ptr& sptr) { if( this != &sptr && _contents != sptr._contents ) { if( --(_contents->rc) == 0 ) { if( _contents->ptr != 0 ) !isArray ? delete _contents->ptr : delete [] _contents->ptr; delete _contents; } _contents = sptr._contents; (_contents->rc)++; } return *this; } template bool smart_ptr::operator==( const smart_ptr& sptr ) const { return _contents->ptr == sptr._contents->ptr; } template bool smart_ptr::operator!=( const smart_ptr& sptr ) const { return _contents->ptr != sptr._contents->ptr; } template T& smart_ptr::operator*() { return *(_contents->ptr); } template T* smart_ptr::operator->() { return _contents->ptr; } template const T& smart_ptr::operator*() const { return *(_contents->ptr); } template const T* smart_ptr::operator->() const { return _contents->ptr; } template smart_ptr::operator bool() const { return _contents->ptr != 0; } // ------------------------------ template unsigned int smart_ptr::alive_refs() const { return _contents->ptr != 0 ? _contents->rc : 0; } } //~ namespace memory #endif