- 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
This commit is contained in:
tchernobog 2006-02-10 11:47:25 +00:00
parent 4dc9012c33
commit be3de1dade
2 changed files with 50 additions and 33 deletions

View File

@ -17,6 +17,8 @@
#ifndef SMARTP_HH #ifndef SMARTP_HH
#define SMARTP_HH 1 #define SMARTP_HH 1
#include <new>
namespace memory { namespace memory {
/** \brief A simple reference counted smart pointer /** \brief A simple reference counted smart pointer
@ -52,47 +54,47 @@ namespace memory {
* reference count. This class requires some effort from * reference count. This class requires some effort from
* the user to abid to some rules. It's meant to be * the user to abid to some rules. It's meant to be
* an help, not a painkiller. */ * an help, not a painkiller. */
smart_ptr( T* ptr = 0 ); smart_ptr(T* ptr = 0) throw(std::bad_alloc);
smart_ptr( const smart_ptr& sptr ); smart_ptr(const smart_ptr& sptr) throw();
~smart_ptr(); ~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 throw();
inline bool operator!=( const smart_ptr& sptr ) const; inline bool operator!=(const smart_ptr& sptr) const throw();
/** \brief Access to stored object's members /** \brief Access to stored object's members
* *
* Use this operator to access object * Use this operator to access object
* methods and data. */ * methods and data. */
inline T* operator->(); inline T* operator->() throw();
/** \brief Access to stored object's members /** \brief Access to stored object's members
* *
* Const version of the above operator. */ * Const version of the above operator. */
inline const T* operator->() const; inline const T* operator->() const throw();
/** \brief Access to stored object /** \brief Access to stored object
* *
* \warning Use with care */ * \warning Use with care */
inline T& operator*(); inline T& operator*() throw();
/** \brief Access to stored object /** \brief Access to stored object
* *
* \warning Use with care */ * \warning Use with care */
inline const T& operator*() const; inline const T& operator*() const throw();
/** \brief Convenience operator for use in predicates /** \brief Convenience operator for use in predicates
* *
* \return true if the stored pointer is valid, * \return true if the stored pointer is valid,
* false otherwise. */ * false otherwise. */
inline operator bool() const; inline operator bool() const throw();
/** \brief Returns the number of alive references to /** \brief Returns the number of alive references to
* the stored object * the stored object
* *
* \return The number of references */ * \return The number of references */
inline unsigned int alive_refs() const; inline unsigned int alive_refs() const throw();
private: private:
struct contents_type { struct contents_type {

View File

@ -27,8 +27,8 @@ namespace memory {
// ------------------------------ // ------------------------------
template<typename T, bool isArray> template<typename T, bool isArray>
smart_ptr<T, isArray>::smart_ptr( T* ptr ) smart_ptr<T, isArray>::smart_ptr( T* ptr ) throw(std::bad_alloc)
: _contents( new contents_type() ) : _contents(new contents_type())
{ {
_contents->rc = 1; _contents->rc = 1;
_contents->ptr = ptr; _contents->ptr = ptr;
@ -36,15 +36,19 @@ namespace memory {
template<typename T, bool isArray> template<typename T, bool isArray>
smart_ptr<T, isArray>::smart_ptr( const smart_ptr& sptr ) smart_ptr<T, isArray>::smart_ptr(const smart_ptr& sptr) throw()
: _contents(sptr._contents) { : _contents(sptr._contents)
{
(_contents->rc)++; (_contents->rc)++;
} }
template<typename T, bool isArray> template<typename T, bool isArray>
smart_ptr<T, isArray>::~smart_ptr() { smart_ptr<T, isArray>::~smart_ptr() throw()
if( --(_contents->rc) == 0 ) { {
if( _contents->ptr != 0 ) if(--(_contents->rc) == 0)
{
if(_contents->ptr != 0)
!isArray ? delete _contents->ptr : delete [] _contents->ptr; !isArray ? delete _contents->ptr : delete [] _contents->ptr;
delete _contents; delete _contents;
} }
@ -54,10 +58,13 @@ namespace memory {
template<typename T, bool isArray> template<typename T, bool isArray>
typename smart_ptr<T, isArray>::smart_ptr& typename smart_ptr<T, isArray>::smart_ptr&
smart_ptr<T, isArray>::operator=( const smart_ptr& sptr) { smart_ptr<T, isArray>::operator=(const smart_ptr& sptr) throw()
if( this != &sptr && _contents != sptr._contents ) { {
if( --(_contents->rc) == 0 ) { if(this != &sptr && _contents != sptr._contents)
if( _contents->ptr != 0 ) {
if(--(_contents->rc) == 0)
{
if(_contents->ptr != 0)
!isArray ? delete _contents->ptr : delete [] _contents->ptr; !isArray ? delete _contents->ptr : delete [] _contents->ptr;
delete _contents; delete _contents;
} }
@ -65,55 +72,62 @@ namespace memory {
_contents = sptr._contents; _contents = sptr._contents;
(_contents->rc)++; (_contents->rc)++;
} }
return *this; return *this;
} }
template<typename T, bool isArray> template<typename T, bool isArray>
bool bool
smart_ptr<T, isArray>::operator==( const smart_ptr& sptr ) const { smart_ptr<T, isArray>::operator==( const smart_ptr& sptr ) const throw()
{
return _contents->ptr == sptr._contents->ptr; return _contents->ptr == sptr._contents->ptr;
} }
template<typename T, bool isArray> template<typename T, bool isArray>
bool bool
smart_ptr<T, isArray>::operator!=( const smart_ptr& sptr ) const { smart_ptr<T, isArray>::operator!=( const smart_ptr& sptr ) const throw()
{
return _contents->ptr != sptr._contents->ptr; return _contents->ptr != sptr._contents->ptr;
} }
template<typename T, bool isArray> template<typename T, bool isArray>
T& T&
smart_ptr<T, isArray>::operator*() { smart_ptr<T, isArray>::operator*() throw()
{
return *(_contents->ptr); return *(_contents->ptr);
} }
template<typename T, bool isArray> template<typename T, bool isArray>
T* T*
smart_ptr<T, isArray>::operator->() { smart_ptr<T, isArray>::operator->() throw()
{
return _contents->ptr; return _contents->ptr;
} }
template<typename T, bool isArray> template<typename T, bool isArray>
const T& const T&
smart_ptr<T, isArray>::operator*() const { smart_ptr<T, isArray>::operator*() const throw()
{
return *(_contents->ptr); return *(_contents->ptr);
} }
template<typename T, bool isArray> template<typename T, bool isArray>
const T* const T*
smart_ptr<T, isArray>::operator->() const { smart_ptr<T, isArray>::operator->() const throw()
{
return _contents->ptr; return _contents->ptr;
} }
template<typename T, bool isArray> template<typename T, bool isArray>
smart_ptr<T, isArray>::operator bool() const { smart_ptr<T, isArray>::operator bool() const throw()
{
return _contents->ptr != 0; return _contents->ptr != 0;
} }
@ -121,7 +135,8 @@ namespace memory {
template<typename T, bool isArray> template<typename T, bool isArray>
unsigned int unsigned int
smart_ptr<T, isArray>::alive_refs() const { smart_ptr<T, isArray>::alive_refs() const throw()
{
return _contents->ptr != 0 ? _contents->rc : 0; return _contents->ptr != 0 ? _contents->rc : 0;
} }