- Add template class implementing a smart pointer (a class of general utility)

git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@298 3ecf2c5c-341e-0410-92b4-d18e462d057c
This commit is contained in:
tchernobog 2006-02-07 20:11:03 +00:00
parent f8be22e03d
commit 78de185d75
5 changed files with 269 additions and 1 deletions

View File

@ -468,7 +468,7 @@ INPUT = @abs_top_srcdir@/src
# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx
# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm *.py
FILE_PATTERNS =
FILE_PATTERNS = *.hh *.h *.cc *.c *.tcc *.py
# The RECURSIVE tag can be used to turn specify whether or not subdirectories
# should be searched for input files as well. Possible values are YES and NO.

View File

@ -147,6 +147,7 @@ src/Makefile
src/backend/Makefile
src/builtin-policies/Makefile
src/pyloader/Makefile
src/templates/Makefile
src/testsuite/Makefile
])
AC_OUTPUT

26
src/templates/Makefile.am Normal file
View File

@ -0,0 +1,26 @@
# src/templates/Makefile.am - Copyright 2005, 2006, University
# of Padova, dept. of Pure and Applied
# Mathematics
#
# This file is part of SGPEMv2.
#
# This 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.
#
# SGPEMv2 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 SGPEMv2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
EXTRA_DIST = \
smartp.tcc
noinst_HEADERS = \
smartp.hh

111
src/templates/smartp.hh Normal file
View File

@ -0,0 +1,111 @@
// smartp.hh - 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_HH
#define SMARTP_HH 1
namespace memory {
/** \brief A simple reference counted smart pointer
*
* This template is a smart pointer which uses
* a simple mechanism of reference count to
* keep track of when and if a stored object
* should be destroyed.
*
* Storing C++ built-in types in an object of
* this class doesn't make a lot of sense, since
* you can't dereference a smart_ptr<>.
*
* \param T The type of the object to store
* \param isArray a boolean value telling if we're
* storing an array or a single object (the default)
*
* \warning This class hasn't virtual methods
* to ensure greater speed. Don't inherit
* from it: its destructor isn't virtual, either.
*/
template<typename T, bool isArray = false>
class smart_ptr {
public:
/** \brief An alias for the smart_ptr which contains null
*
* When you've to compare some smart pointer with another
* to see if it points to nowhere, you can use this
* const object.
* */
static const smart_ptr<T, isArray> null;
/** Call this constructor only the first time. Whenever
* you'll need to keep other references to the same pointer,
* use the cctor, or we won't be able to keep updated the
* reference count. This class requires some effort from
* the user to abid to some rules. It's meant to be
* an help, not a painkiller. */
smart_ptr( T* ptr = 0 );
smart_ptr( const smart_ptr& sptr );
~smart_ptr();
smart_ptr& operator=( const smart_ptr& sptr );
inline bool operator==( const smart_ptr& sptr ) const;
inline bool operator!=( const smart_ptr& sptr ) const;
/** \brief Access to stored object's members
*
* Use this operator to access to object
* methods and data. */
inline T* operator->();
/** \brief Access to stored object's members
*
* Const version of the above operator. */
inline const T* operator->() const;
/** \brief Access to stored object
*
* \warning Use with care */
inline T& operator*();
/** \brief Access to stored object
*
* \warning Use with care */
inline const T& operator*() const;
/** \brief Convenience operator for use in predicates
*
* \return true if the stored pointer is valid,
* false otherwise. */
inline operator bool() const;
/** \brief Returns the number of alive references to
* the stored object
*
* \return The number of references */
inline unsigned int alive_refs() const;
private:
struct contents_type {
T* ptr;
unsigned int rc;
}* _contents;
};
}
#include "smartp.tcc"
#endif

130
src/templates/smartp.tcc Normal file
View File

@ -0,0 +1,130 @@
// 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>
smart_ptr<T, isArray>::smart_ptr( T* ptr )
: _contents( new contents_type() )
{
_contents->rc = 1;
_contents->ptr = ptr;
}
template<typename T, bool isArray>
smart_ptr<T, isArray>::smart_ptr( const smart_ptr& sptr )
: _contents(sptr._contents) {
(_contents->rc)++;
}
template<typename T, bool isArray>
smart_ptr<T, isArray>::~smart_ptr() {
if( --(_contents->rc) == 0 ) {
if( _contents->ptr != 0 )
!isArray ? delete _contents->ptr : delete [] _contents->ptr;
delete _contents;
}
}
// ------------------------------
template<typename T, bool isArray>
typename smart_ptr<T, isArray>::smart_ptr&
smart_ptr<T, isArray>::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<typename T, bool isArray>
bool
smart_ptr<T, isArray>::operator==( const smart_ptr& sptr ) const {
return _contents->ptr == sptr._contents->ptr;
}
template<typename T, bool isArray>
bool
smart_ptr<T, isArray>::operator!=( const smart_ptr& sptr ) const {
return _contents->ptr != sptr._contents->ptr;
}
template<typename T, bool isArray>
T&
smart_ptr<T, isArray>::operator*() {
return *(_contents->ptr);
}
template<typename T, bool isArray>
T*
smart_ptr<T, isArray>::operator->() {
return _contents->ptr;
}
template<typename T, bool isArray>
const T&
smart_ptr<T, isArray>::operator*() const {
return *(_contents->ptr);
}
template<typename T, bool isArray>
const T*
smart_ptr<T, isArray>::operator->() const {
return _contents->ptr;
}
template<typename T, bool isArray>
smart_ptr<T, isArray>::operator bool() const {
return _contents->ptr != 0;
}
// ------------------------------
template<typename T, bool isArray>
unsigned int
smart_ptr<T, isArray>::alive_refs() const {
return _contents->ptr != 0 ? _contents->rc : 0;
}
} //~ namespace memory
#endif