diff --git a/config/Doxyfile.in b/config/Doxyfile.in index f5b7b03..e9b3e2b 100644 --- a/config/Doxyfile.in +++ b/config/Doxyfile.in @@ -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. diff --git a/configure.ac b/configure.ac index c795e29..70eaa14 100644 --- a/configure.ac +++ b/configure.ac @@ -147,6 +147,7 @@ src/Makefile src/backend/Makefile src/builtin-policies/Makefile src/pyloader/Makefile +src/templates/Makefile src/testsuite/Makefile ]) AC_OUTPUT diff --git a/src/templates/Makefile.am b/src/templates/Makefile.am new file mode 100644 index 0000000..dab4f7c --- /dev/null +++ b/src/templates/Makefile.am @@ -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 diff --git a/src/templates/smartp.hh b/src/templates/smartp.hh new file mode 100644 index 0000000..03176a6 --- /dev/null +++ b/src/templates/smartp.hh @@ -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 + 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 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 diff --git a/src/templates/smartp.tcc b/src/templates/smartp.tcc new file mode 100644 index 0000000..5176fea --- /dev/null +++ b/src/templates/smartp.tcc @@ -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 + 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