From be3de1dade514b3a7d2a6812c33db49e5f70e413 Mon Sep 17 00:00:00 2001 From: tchernobog Date: Fri, 10 Feb 2006 11:47:25 +0000 Subject: [PATCH] - Add exception throwing specifications to methods - Coding style fixes git-svn-id: svn://svn.gna.org/svn/sgpemv2/trunk@313 3ecf2c5c-341e-0410-92b4-d18e462d057c --- src/templates/smartp.hh | 26 +++++++++--------- src/templates/smartp.tcc | 57 +++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 33 deletions(-) diff --git a/src/templates/smartp.hh b/src/templates/smartp.hh index f86aae1..f5fc0d6 100644 --- a/src/templates/smartp.hh +++ b/src/templates/smartp.hh @@ -17,6 +17,8 @@ #ifndef SMARTP_HH #define SMARTP_HH 1 +#include + namespace memory { /** \brief A simple reference counted smart pointer @@ -52,47 +54,47 @@ namespace memory { * 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(T* ptr = 0) throw(std::bad_alloc); + smart_ptr(const smart_ptr& sptr) throw(); + ~smart_ptr() throw(); - smart_ptr& operator=( const smart_ptr& sptr ); + smart_ptr& operator=(const smart_ptr& sptr) throw(); - inline bool operator==( const smart_ptr& sptr ) const; - inline bool operator!=( const smart_ptr& sptr ) const; + inline bool operator==(const smart_ptr& sptr) const throw(); + inline bool operator!=(const smart_ptr& sptr) const throw(); /** \brief Access to stored object's members * * Use this operator to access object * methods and data. */ - inline T* operator->(); + inline T* operator->() throw(); /** \brief Access to stored object's members * * Const version of the above operator. */ - inline const T* operator->() const; + inline const T* operator->() const throw(); /** \brief Access to stored object * * \warning Use with care */ - inline T& operator*(); + inline T& operator*() throw(); /** \brief Access to stored object * * \warning Use with care */ - inline const T& operator*() const; + inline const T& operator*() const throw(); /** \brief Convenience operator for use in predicates * * \return true if the stored pointer is valid, * false otherwise. */ - inline operator bool() const; + inline operator bool() const throw(); /** \brief Returns the number of alive references to * the stored object * * \return The number of references */ - inline unsigned int alive_refs() const; + inline unsigned int alive_refs() const throw(); private: struct contents_type { diff --git a/src/templates/smartp.tcc b/src/templates/smartp.tcc index 5176fea..8055042 100644 --- a/src/templates/smartp.tcc +++ b/src/templates/smartp.tcc @@ -27,8 +27,8 @@ namespace memory { // ------------------------------ template - smart_ptr::smart_ptr( T* ptr ) - : _contents( new contents_type() ) + smart_ptr::smart_ptr( T* ptr ) throw(std::bad_alloc) + : _contents(new contents_type()) { _contents->rc = 1; _contents->ptr = ptr; @@ -36,15 +36,19 @@ namespace memory { template - smart_ptr::smart_ptr( const smart_ptr& sptr ) - : _contents(sptr._contents) { + smart_ptr::smart_ptr(const smart_ptr& sptr) throw() + : _contents(sptr._contents) + { (_contents->rc)++; } + template - smart_ptr::~smart_ptr() { - if( --(_contents->rc) == 0 ) { - if( _contents->ptr != 0 ) + smart_ptr::~smart_ptr() throw() + { + if(--(_contents->rc) == 0) + { + if(_contents->ptr != 0) !isArray ? delete _contents->ptr : delete [] _contents->ptr; delete _contents; } @@ -54,10 +58,13 @@ namespace memory { 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 ) + smart_ptr::operator=(const smart_ptr& sptr) throw() + { + if(this != &sptr && _contents != sptr._contents) + { + if(--(_contents->rc) == 0) + { + if(_contents->ptr != 0) !isArray ? delete _contents->ptr : delete [] _contents->ptr; delete _contents; } @@ -65,55 +72,62 @@ namespace memory { _contents = sptr._contents; (_contents->rc)++; } - + return *this; } - + template bool - smart_ptr::operator==( const smart_ptr& sptr ) const { + smart_ptr::operator==( const smart_ptr& sptr ) const throw() + { return _contents->ptr == sptr._contents->ptr; } template bool - smart_ptr::operator!=( const smart_ptr& sptr ) const { + smart_ptr::operator!=( const smart_ptr& sptr ) const throw() + { return _contents->ptr != sptr._contents->ptr; } template T& - smart_ptr::operator*() { + smart_ptr::operator*() throw() + { return *(_contents->ptr); } template T* - smart_ptr::operator->() { + smart_ptr::operator->() throw() + { return _contents->ptr; } template const T& - smart_ptr::operator*() const { + smart_ptr::operator*() const throw() + { return *(_contents->ptr); } template const T* - smart_ptr::operator->() const { + smart_ptr::operator->() const throw() + { return _contents->ptr; } template - smart_ptr::operator bool() const { + smart_ptr::operator bool() const throw() + { return _contents->ptr != 0; } @@ -121,7 +135,8 @@ namespace memory { template unsigned int - smart_ptr::alive_refs() const { + smart_ptr::alive_refs() const throw() + { return _contents->ptr != 0 ? _contents->rc : 0; }