- 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
#define SMARTP_HH 1
#include <new>
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 {

View File

@ -27,7 +27,7 @@ namespace memory {
// ------------------------------
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->rc = 1;
@ -36,14 +36,18 @@ namespace memory {
template<typename T, bool isArray>
smart_ptr<T, isArray>::smart_ptr( const smart_ptr& sptr )
: _contents(sptr._contents) {
smart_ptr<T, isArray>::smart_ptr(const smart_ptr& sptr) throw()
: _contents(sptr._contents)
{
(_contents->rc)++;
}
template<typename T, bool isArray>
smart_ptr<T, isArray>::~smart_ptr() {
if( --(_contents->rc) == 0 ) {
smart_ptr<T, isArray>::~smart_ptr() throw()
{
if(--(_contents->rc) == 0)
{
if(_contents->ptr != 0)
!isArray ? delete _contents->ptr : delete [] _contents->ptr;
delete _contents;
@ -54,9 +58,12 @@ namespace memory {
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 ) {
smart_ptr<T, isArray>::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;
@ -72,48 +79,55 @@ namespace memory {
template<typename T, bool isArray>
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;
}
template<typename T, bool isArray>
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;
}
template<typename T, bool isArray>
T&
smart_ptr<T, isArray>::operator*() {
smart_ptr<T, isArray>::operator*() throw()
{
return *(_contents->ptr);
}
template<typename T, bool isArray>
T*
smart_ptr<T, isArray>::operator->() {
smart_ptr<T, isArray>::operator->() throw()
{
return _contents->ptr;
}
template<typename T, bool isArray>
const T&
smart_ptr<T, isArray>::operator*() const {
smart_ptr<T, isArray>::operator*() const throw()
{
return *(_contents->ptr);
}
template<typename T, bool isArray>
const T*
smart_ptr<T, isArray>::operator->() const {
smart_ptr<T, isArray>::operator->() const throw()
{
return _contents->ptr;
}
template<typename T, bool isArray>
smart_ptr<T, isArray>::operator bool() const {
smart_ptr<T, isArray>::operator bool() const throw()
{
return _contents->ptr != 0;
}
@ -121,7 +135,8 @@ namespace memory {
template<typename T, bool isArray>
unsigned int
smart_ptr<T, isArray>::alive_refs() const {
smart_ptr<T, isArray>::alive_refs() const throw()
{
return _contents->ptr != 0 ? _contents->rc : 0;
}