Public Member Functions

kNet::SharedPtr< T > Class Template Reference

SharedPtr is an intrusive refcount-tracked single-object lifetime-manager. More...

#include <SharedPtr.h>

List of all members.

Public Member Functions

 SharedPtr ()
 Constructs a zero pointer.
 SharedPtr (T *ptr)
 Constructs from a raw pointer.
 SharedPtr (const SharedPtr< T > &rhs)
 Constructs from an existing SharedPtr.
template<typename T2 >
 SharedPtr (const SharedPtr< T2 > &rhs)
 Template copy constructor for types that allow conversions to other types.
SharedPtr< T > & operator= (const SharedPtr< T > &rhs)
 Assignment from another SharedPtr.
SharedPtr< T > & operator= (T *ptr)
 Assignment from a raw ptr.
 ~SharedPtr ()
 Decreases refCount on the object and frees it if it's no longer used.
unsigned int RefCount () const
 Returns the reference count of the currently held object.
const T * ptr () const
 Const access to the pointer.
const T * operator-> () const
 Const dereference and access object.
const T & operator* () const
 Const dereference object.
 operator T * ()
 Implicit conversion to a raw pointer.
T * ptr ()
 Returns a pointer to the object or 0 if empty.
T * operator-> ()
 Dereference and access object.
T & operator* ()
 Dereference object.
 operator bool () const
 Test if pointer is good.
 operator bool ()
 Test if pointer is good.

Detailed Description

template<typename T>
class kNet::SharedPtr< T >

SharedPtr is an intrusive refcount-tracked single-object lifetime-manager.

SharedPtr wraps raw pointers to objects and manages reference counting on the number of users on the pointer. When all users abandon the object, FreeFunc::free(T*) is called to clean up the memory used by the resource.


Constructor & Destructor Documentation

template<typename T >
kNet::SharedPtr< T >::SharedPtr ( T *  ptr_  ) 

Constructs from a raw pointer.

This constructor takes the ownership of the raw pointer and starts managing the reference count on it, assuming that it is the first pointer to access it.

Thus, when invoking this constructor you need to be sure that the passed pointer is not being reference-counted someplace else, because the reference count is stored in the referencing smart pointers rather than the object itself.

Parameters:
ptr_ Raw pointer to an object that is not being refCounted anywhere else.
template<typename T >
kNet::SharedPtr< T >::SharedPtr ( const SharedPtr< T > &  rhs  ) 

Constructs from an existing SharedPtr.

This constructor increases reference on the existing SharedPtr, and initializes this to point to the same object and refCount as the existing one.

This is the safe way to copy pointers.

Parameters:
rhs The pointer to make copy of.
template<typename T >
template<typename T2 >
kNet::SharedPtr< T >::SharedPtr ( const SharedPtr< T2 > &  rhs  ) 

Template copy constructor for types that allow conversions to other types.

This constructor allows to convert SharedPtr's from Derived types to SharedPtr's of Base types, or more specifically, from any type A to type B, as long as there is an implicit conversion operator available for the specified types.

Parameters:
rhs The pointer to make copy of.
template<typename T >
kNet::SharedPtr< T >::~SharedPtr (  ) 

Decreases refCount on the object and frees it if it's no longer used.


Member Function Documentation

template<typename T >
SharedPtr< T > & kNet::SharedPtr< T >::operator= ( const SharedPtr< T > &  rhs  ) 

Assignment from another SharedPtr.

Decreases refCount on old object and modifies this to point to the new object, increasing it's refCount.

Parameters:
rhs The pointer to make copy of.
Returns:
Reference to this.
template<typename T >
SharedPtr< T > & kNet::SharedPtr< T >::operator= ( T *  ptr_  ) 

Assignment from a raw ptr.

Decreases refCount on old object (possibly frees it) and takes the ownership of the new object, making the assumption that this is the first SharedPtr to count reference to the object.

Parameters:
ptr_ Raw pointer to an object that is not being refCounted anywhere else.
Returns:
Reference to this.
template<typename T >
unsigned int kNet::SharedPtr< T >::RefCount (  )  const

Returns the reference count of the currently held object.

Returns:
The reference count of the object this SharedPtr is pointing at, or 0 if the SharedPtr isn't pointing to anything.
template<typename T >
const T * kNet::SharedPtr< T >::ptr (  )  const

Const access to the pointer.

Returns:
Const pointer to the object or 0.

Referenced by kNet::operator==().

template<typename T >
const T * kNet::SharedPtr< T >::operator-> (  )  const

Const dereference and access object.

Causes an assert failure if the pointer is null.

Returns:
Const pointer to the object.
template<typename T >
const T & kNet::SharedPtr< T >::operator* (  )  const

Const dereference object.

Causes an assert failure if the pointer is null.

Returns:
Const reference to the object.
template<typename T >
kNet::SharedPtr< T >::operator T * (  ) 

Implicit conversion to a raw pointer.

Causes an assert failure if the pointer is null.

If reading a null pointer should be considered a legal operation, use .ptr() instead.

template<typename T >
T * kNet::SharedPtr< T >::ptr (  ) 

Returns a pointer to the object or 0 if empty.

Returns:
Pointer to the object or 0.
template<typename T >
T * kNet::SharedPtr< T >::operator-> (  ) 

Dereference and access object.

Causes an assert failure if the pointer is null.

Returns:
Pointer to the object.
template<typename T >
T & kNet::SharedPtr< T >::operator* (  ) 

Dereference object.

Causes an assert failure if the pointer is null.

Returns:
Reference to the object.
template<typename T >
kNet::SharedPtr< T >::operator bool (  )  const

Test if pointer is good.

Returns:
true if the pointer is good, false if it's null.
template<typename T >
kNet::SharedPtr< T >::operator bool (  ) 

Test if pointer is good.

Returns:
true if the pointer is good, false if it's null.

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines