2006-02-07 21:11:03 +01:00
|
|
|
// 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<typename T, bool isArray>
|
|
|
|
const smart_ptr<T, isArray> smart_ptr<T, isArray>::null = 0;
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::smart_ptr( T* ptr ) throw(std::bad_alloc)
|
|
|
|
: _contents(new contents_type())
|
2006-02-07 21:11:03 +01:00
|
|
|
{
|
|
|
|
_contents->rc = 1;
|
|
|
|
_contents->ptr = ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::smart_ptr(const smart_ptr& sptr) throw()
|
|
|
|
: _contents(sptr._contents)
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
(_contents->rc)++;
|
|
|
|
}
|
|
|
|
|
2006-02-10 12:47:25 +01:00
|
|
|
|
2006-02-07 21:11:03 +01:00
|
|
|
template<typename T, bool isArray>
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::~smart_ptr() throw()
|
|
|
|
{
|
|
|
|
if(--(_contents->rc) == 0)
|
|
|
|
{
|
|
|
|
if(_contents->ptr != 0)
|
2006-02-07 21:11:03 +01:00
|
|
|
!isArray ? delete _contents->ptr : delete [] _contents->ptr;
|
|
|
|
delete _contents;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
typename smart_ptr<T, isArray>::smart_ptr&
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator=(const smart_ptr& sptr) throw()
|
|
|
|
{
|
|
|
|
if(this != &sptr && _contents != sptr._contents)
|
|
|
|
{
|
|
|
|
if(--(_contents->rc) == 0)
|
|
|
|
{
|
|
|
|
if(_contents->ptr != 0)
|
2006-02-07 21:11:03 +01:00
|
|
|
!isArray ? delete _contents->ptr : delete [] _contents->ptr;
|
|
|
|
delete _contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
_contents = sptr._contents;
|
|
|
|
(_contents->rc)++;
|
|
|
|
}
|
2006-02-10 12:47:25 +01:00
|
|
|
|
2006-02-07 21:11:03 +01:00
|
|
|
return *this;
|
|
|
|
}
|
2006-02-10 12:47:25 +01:00
|
|
|
|
2006-02-07 21:11:03 +01:00
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
bool
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator==( const smart_ptr& sptr ) const throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return _contents->ptr == sptr._contents->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
bool
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator!=( const smart_ptr& sptr ) const throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return _contents->ptr != sptr._contents->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
T&
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator*() throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return *(_contents->ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
T*
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator->() throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return _contents->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
const T&
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator*() const throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return *(_contents->ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
const T*
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator->() const throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return _contents->ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::operator bool() const throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return _contents->ptr != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------
|
|
|
|
|
|
|
|
template<typename T, bool isArray>
|
|
|
|
unsigned int
|
2006-02-10 12:47:25 +01:00
|
|
|
smart_ptr<T, isArray>::alive_refs() const throw()
|
|
|
|
{
|
2006-02-07 21:11:03 +01:00
|
|
|
return _contents->ptr != 0 ? _contents->rc : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //~ namespace memory
|
|
|
|
|
|
|
|
#endif
|