Overture  Version 25
Public Member Functions | Protected Member Functions | Friends | List of all members
KK::sptr< T > Class Template Reference

MY smart pointer class (not yours!) More...

#include <kk_ptr.hh>

Public Member Functions

 sptr (T *d_=0)
 initialize the pointer using a regular pointer (or null if none is provided)
 
 sptr (const sptr< T > &p)
 make a new reference a pointer of the same type (be carefull if the pointer is assigned to itself)
 
 ~sptr ()
 decrement and destroy the pointer
 
sptr< T > & operator= (const sptr< T > &p)
 assignment operator, this will make the reference and increment the reference count
 
bool operator== (const sptr< T > &p) const
 boolean equals; two sptrs are the same if this->data==p.data && this->gid==p.gid
 
bool operator!= (const sptr< T > &p) const
 boolean not equals; two sptrs are the same if this->data==p.data && this->gid==p.gid
 
T * operator-> ()
 dereferencing operator for the pointer; an error is thrown if the pointer is null
 
const T * operator-> () const
 const dereferencing operator for the pointer; an error is thrown if the pointer is null
 
T & operator* ()
 dereferencing operator for the pointer; an error is thrown if the pointer is null
 
const T & operator* () const
 const dereferencing operator for the pointer; an error is thrown if the pointer is null
 
const T * raw () const
 return the raw pointer; use it wisely!
 
 operator bool () const
 conversion to bool; the result is true if data!=0 && gid>=-1
 
long int GID () const
 return the global id
 
int nRefs () const
 return the number of references
 

Protected Member Functions

 sptr (T *rp, int *start_count, long int gid_)
 a "special" constructor used by sptr_cast that takes a raw pointer and reference count
 
int increment ()
 increment the reference count after checking to see if the pointer makes sense
 
int decrement ()
 decrement the reference count after checking to see if the pointer makes sense
 
void destroy ()
 

Friends

template<typename TO , typename FROM >
sptr< TO > sptr_cast (sptr< FROM > &p)
 cast from one pointer type to another; return a null ptr if the dynamic cast is invalid
 
template<typename TO , typename FROM >
sptr< TO > sptr_static_cast (sptr< FROM > &p)
 
template<typename TO , typename FROM >
sptr< TO > sptr_dynamic_cast (sptr< FROM > &p)
 

Detailed Description

template<typename T>
class KK::sptr< T >

MY smart pointer class (not yours!)

KK::sptr is a smart pointer class implemented using reference counting (heap allocated int). The object pointed to is destructed when the reference count goes to zero. A dynamic_cast mechanism is provided; most of the methods throw sptr_Err in the event of an unrecoverable error. Invalid casts return a null sptr (count==0, data==0)

Constructor & Destructor Documentation

template<typename T>
KK::sptr< T >::sptr ( T *  d_ = 0)
inline

initialize the pointer using a regular pointer (or null if none is provided)

References KK::acquire_gid(), KK::sptr< T >::increment(), and KK_PTR_OUT.

template<typename T>
KK::sptr< T >::sptr ( const sptr< T > &  p)
inline

make a new reference a pointer of the same type (be carefull if the pointer is assigned to itself)

References KK::sptr< T >::increment(), and KK_PTR_OUT.

template<typename T>
KK::sptr< T >::~sptr ( )
inline

decrement and destroy the pointer

References KK::sptr< T >::decrement(), KK::sptr< T >::destroy(), and KK_PTR_OUT.

template<typename T>
KK::sptr< T >::sptr ( T *  rp,
int *  start_count,
long int  gid_ 
)
inlineprotected

a "special" constructor used by sptr_cast that takes a raw pointer and reference count

References KK::Assert(), and KK::sptr< T >::increment().

Member Function Documentation

template<typename T>
int KK::sptr< T >::decrement ( )
inlineprotected

decrement the reference count after checking to see if the pointer makes sense

References KK_PTR_OUT.

Referenced by KK::sptr< T >::operator=(), and KK::sptr< T >::~sptr().

template<typename T>
void KK::sptr< T >::destroy ( )
inlineprotected
template<typename T>
long int KK::sptr< T >::GID ( ) const
inline

return the global id

template<typename T>
int KK::sptr< T >::increment ( )
inlineprotected

increment the reference count after checking to see if the pointer makes sense

References KK_PTR_OUT.

Referenced by KK::sptr< T >::operator=(), and KK::sptr< T >::sptr().

template<typename T>
int KK::sptr< T >::nRefs ( ) const
inline

return the number of references

template<typename T>
KK::sptr< T >::operator bool ( ) const
inline

conversion to bool; the result is true if data!=0 && gid>=-1

template<typename T>
bool KK::sptr< T >::operator!= ( const sptr< T > &  p) const
inline

boolean not equals; two sptrs are the same if this->data==p.data && this->gid==p.gid

boolean not equals; two sptrs are the same if this->data==p.data && this->gid==p.gid. An error is thrown if (this->data==p.data && this->gid!=p.gid) or (this->data!=p.data && this->gid==p.gid)

template<typename T>
T& KK::sptr< T >::operator* ( )
inline

dereferencing operator for the pointer; an error is thrown if the pointer is null

template<typename T>
const T& KK::sptr< T >::operator* ( ) const
inline

const dereferencing operator for the pointer; an error is thrown if the pointer is null

template<typename T>
T* KK::sptr< T >::operator-> ( )
inline

dereferencing operator for the pointer; an error is thrown if the pointer is null

template<typename T>
const T* KK::sptr< T >::operator-> ( ) const
inline

const dereferencing operator for the pointer; an error is thrown if the pointer is null

template<typename T>
sptr<T>& KK::sptr< T >::operator= ( const sptr< T > &  p)
inline

assignment operator, this will make the reference and increment the reference count

References KK::sptr< T >::decrement(), KK::sptr< T >::destroy(), KK::sptr< T >::increment(), and KK_PTR_OUT.

template<typename T>
bool KK::sptr< T >::operator== ( const sptr< T > &  p) const
inline

boolean equals; two sptrs are the same if this->data==p.data && this->gid==p.gid

boolean equals; two sptrs are the same if this->data==p.data && this->gid==p.gid. An error is thrown if (this->data==p.data && this->gid!=p.gid) or (this->data!=p.data && this->gid==p.gid)

References KK_PTR_OUT.

template<typename T>
const T* KK::sptr< T >::raw ( ) const
inline

return the raw pointer; use it wisely!

Referenced by KK::sptr_cast(), and KK::sptr_dynamic_cast().

Friends And Related Function Documentation

template<typename T>
template<typename TO , typename FROM >
sptr<TO> sptr_cast ( sptr< FROM > &  p)
friend

cast from one pointer type to another; return a null ptr if the dynamic cast is invalid

template<typename T>
template<typename TO , typename FROM >
sptr<TO> sptr_dynamic_cast ( sptr< FROM > &  p)
friend
template<typename T>
template<typename TO , typename FROM >
sptr<TO> sptr_static_cast ( sptr< FROM > &  p)
friend

The documentation for this class was generated from the following file: