Spinning Topp Logo BlackTopp Studios
inc
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
Mezzanine::CountedPtr< TypePointedTo > Class Template Reference

A simple reference counting pointer. More...

#include <countedptr.h>

+ Collaboration diagram for Mezzanine::CountedPtr< TypePointedTo >:

Public Types

typedef TypePointedTo element_type
 This makes referencing the type of the pointer object easier for external classes.
 
typedef ReferenceCountTraits< TypePointedTo >::RefCountType RefCountType
 The non pointer version of PtrType.
 

Public Member Functions

 CountedPtr (TypePointedTo *PointerTarget=0) throw ()
 Initializing Constructor. More...
 
 CountedPtr (const CountedPtr &Original) throw ()
 Copy constructor. More...
 
template<typename OtherPointer >
 CountedPtr (const CountedPtr< OtherPointer > &Original) throw ()
 Casting copy constructor. More...
 
 ~CountedPtr () throw ()
 Deconstructor, Just calls Release().
 
TypePointedTo * Get () const throw ()
 Get the raw pointer to the managed object. More...
 
TypePointedTo * get () const throw ()
 Get the raw pointer to the managed object. More...
 
RefCountTypeGetReferenceCount () const throw ()
 Get the internal Reference count. More...
 
 operator Boole () const throw ()
 Returns true if this pointer points to something. More...
 
TypePointedTo & operator* () const throw ()
 Dereference operator. More...
 
TypePointedTo * operator-> () const throw ()
 The Structure dereference operator. More...
 
CountedPtroperator= (const CountedPtr &Other) throw ()
 Assignement operator. More...
 
Boole operator== (const CountedPtr &Other) const throw ()
 A comparision of two CountedPtr instances. More...
 
void Reset ()
 Reset this to point at nothing.
 
void reset ()
 Reset this to point at nothing. More...
 
void Reset (const CountedPtr< TypePointedTo > &Other)
 Reset this to point at the same as another CountedPtr of the same type. More...
 
void reset (const CountedPtr< TypePointedTo > &Other)
 Reset this to point at the same as another CountedPtr of the same type. More...
 
void Reset (TypePointedTo *PointerTarget)
 Take ownership of the passed pointer. More...
 
void reset (TypePointedTo *PointerTarget)
 Take ownership of the passed pointer. More...
 
Boole Unique () const throw ()
 Is this the only pointer to the managed object. More...
 
Boole unique () const throw ()
 Is this the only pointer to the managed object. More...
 
Whole use_count () const throw ()
 Get the current count of references. More...
 
Whole UseCount () const throw ()
 Get the current count of references. More...
 

Protected Member Functions

template<typename AnyReferenceCountType >
void Acquire (AnyReferenceCountType *CounterToAcquire) throw ()
 Have this pointer point at the same thing another pointer points to,. More...
 
void FirstAcquire (TypePointedTo *PointerTarget)
 If required create a reference counter and point this at the passed pointer. More...
 
void Release () throw ()
 This decrements the reference count and deletes the managed items if there are no remaining references.
 

Protected Attributes

RefCountType_ReferenceCounter
 This is the only data on this class, a pointer to the counter and the managed object.
 

Friends

template<typename OtherPointer >
class CountedPtr
 declare all pointers as friends so they can reference eachother _ReferenceCounter.
 
template<typename ReturnType , typename OtherPointerTargetType >
CountedPtr< ReturnType > CountedPtrStaticCast (CountedPtr< OtherPointerTargetType > &Original)
 Static casting can access internals also. More...
 

Detailed Description

template<typename TypePointedTo>
class Mezzanine::CountedPtr< TypePointedTo >

A simple reference counting pointer.

This is a pointer that automatically deallocates the object it manages when all CountedPtr intances managing it are destroyed or fall out of scope. This is a simpler version of std::shared_ptr.

Warning
This is not thread safe by default.
If you intead to have covariant pointers (Multiple pointers to the same item but with different type date, like more base or more derived pointers) all classes pointed to must have virtual deconstructors
Note
The basis of this class originated externally, please see the counted pointer from http://ootips.org/yonat/4dev/smart-pointers.html which came with written permission for use stated as "Feel free to use my own smart pointers in your code" on that page.

Definition at line 70 of file countedptr.h.

Constructor & Destructor Documentation

template<typename TypePointedTo>
Mezzanine::CountedPtr< TypePointedTo >::CountedPtr ( TypePointedTo *  PointerTarget = 0)
throw (
)
inlineexplicit

Initializing Constructor.

This should only be used for initial creation of a shared pointer group. This will allocate the raw pointer and the ReferenceCounter that will be used to track the pointer passed. This will only be explicitly called to prevent accidental premature deletion of the item managed.

It is quite easy to accidentally make a secondary group of counted pointers if not using the new statement inline with this constructor, and it is not recomended to use this in any other way. Here is an example of the recommended way to use new inline with this: "Mezzanine::CountedPtr<Mezzanine::Vector3> VecPtr (new Mezzanine::Vector3);"

Parameters
PointerTargetThe item that will be deleted once all the pointer of this group disappear.

Definition at line 346 of file countedptr.h.

template<typename TypePointedTo>
Mezzanine::CountedPtr< TypePointedTo >::CountedPtr ( const CountedPtr< TypePointedTo > &  Original)
throw (
)
inline

Copy constructor.

Parameters
OriginalThe pointer being copied. This fresh pointer will use the same ReferenceCounter as the original.
Exceptions
NothingThis member function does throws exceptions.

Definition at line 361 of file countedptr.h.

template<typename TypePointedTo>
template<typename OtherPointer >
Mezzanine::CountedPtr< TypePointedTo >::CountedPtr ( const CountedPtr< OtherPointer > &  Original)
throw (
)
inline

Casting copy constructor.

Parameters
OriginalThe CountedPtr being copied. This fresh pointer will use the same ReferenceCounter as the original.
Exceptions
NothingThis member function does throws exceptions, unless the casting mechanism throws, which it shouldn't.

Definition at line 368 of file countedptr.h.

Member Function Documentation

template<typename TypePointedTo>
template<typename AnyReferenceCountType >
void Mezzanine::CountedPtr< TypePointedTo >::Acquire ( AnyReferenceCountType *  CounterToAcquire)
throw (
)
inlineprotected

Have this pointer point at the same thing another pointer points to,.

Parameters
CounterToAcquireThe ReferenceCounter that this pointer will use.
Warning
This does not Release the previous Reference counter. This means it is possible leak memory if a ReferenceCounter is acquired that differs from the previous one without plans to manage the original.
Exceptions
NothingThis member function does not throw exceptions, unless debug logging is enabled, then it can throw any exception the logger can throw.

Definition at line 311 of file countedptr.h.

template<typename TypePointedTo>
void Mezzanine::CountedPtr< TypePointedTo >::FirstAcquire ( TypePointedTo *  PointerTarget)
inlineprotected

If required create a reference counter and point this at the passed pointer.

Parameters
PointerTargetthe Pointer to take ownership of.

Definition at line 333 of file countedptr.h.

template<typename TypePointedTo>
TypePointedTo* Mezzanine::CountedPtr< TypePointedTo >::Get ( ) const
throw (
)
inline

Get the raw pointer to the managed object.

Returns
The raw pointer to the managed object or 0 if this pointer is invalid.
Exceptions
NothingThis member function does not throw exceptions.
Note
This name was chosen to match standard compliant names, and should be usable in templates that require this function.

Definition at line 458 of file countedptr.h.

template<typename TypePointedTo>
TypePointedTo* Mezzanine::CountedPtr< TypePointedTo >::get ( ) const
throw (
)
inline

Get the raw pointer to the managed object.

Returns
The raw pointer to the managed object or 0 if this pointer is invalid.
Exceptions
NothingThis member function does not throw exceptions.
Note
This name was chosen to match standard compliant names, and should be usable in templates that require this function.
Provided to match method names on standard smart pointers

Definition at line 463 of file countedptr.h.

template<typename TypePointedTo>
RefCountType* Mezzanine::CountedPtr< TypePointedTo >::GetReferenceCount ( ) const
throw (
)
inline

Get the internal Reference count.

Returns
A pointer to internal reference count of this pointer.

Definition at line 494 of file countedptr.h.

template<typename TypePointedTo>
Mezzanine::CountedPtr< TypePointedTo >::operator Boole ( ) const
throw (
)
inline

Returns true if this pointer points to something.

Warning
Without C++11 this can be accidentally easily be cast to a Boole and can do sill things.

Definition at line 488 of file countedptr.h.

template<typename TypePointedTo>
TypePointedTo& Mezzanine::CountedPtr< TypePointedTo >::operator* ( ) const
throw (
)
inline

Dereference operator.

Returns
The managed object is returned by reference.
Exceptions
NothingThis member function does not throw exceptions.

Definition at line 445 of file countedptr.h.

template<typename TypePointedTo>
TypePointedTo* Mezzanine::CountedPtr< TypePointedTo >::operator-> ( ) const
throw (
)
inline

The Structure dereference operator.

Returns
Makes it appear, syntactically, as though you are dereferencing the raw pointer.
Exceptions
NothingThis member function does not throw exceptions.

Definition at line 451 of file countedptr.h.

template<typename TypePointedTo>
CountedPtr& Mezzanine::CountedPtr< TypePointedTo >::operator= ( const CountedPtr< TypePointedTo > &  Other)
throw (
)
inline

Assignement operator.

This safely handles the semantics or release the previously assigned object and acquiring the new managed object. This performs basic checks as expected.

Parameters
OtherThe Item on the right hand side of the '=', which this class instance will copy.
Returns
A reference to this.

Definition at line 433 of file countedptr.h.

template<typename TypePointedTo>
Boole Mezzanine::CountedPtr< TypePointedTo >::operator== ( const CountedPtr< TypePointedTo > &  Other) const
throw (
)
inline

A comparision of two CountedPtr instances.

This is used to see if this and another CountedPtr are managing the same object and are part of the same group of CountedPtr managing that object.

Parameters
OtherThe CountedPtr on the right hand side of the ==
Returns
This returns true if this and Other use the same reference count and pointer.

Definition at line 483 of file countedptr.h.

template<typename TypePointedTo>
void Mezzanine::CountedPtr< TypePointedTo >::reset ( )
inline

Reset this to point at nothing.

Note
Provided to match method names on standard smart pointers

Definition at line 383 of file countedptr.h.

template<typename TypePointedTo>
void Mezzanine::CountedPtr< TypePointedTo >::Reset ( const CountedPtr< TypePointedTo > &  Other)
inline

Reset this to point at the same as another CountedPtr of the same type.

Parameters
OtherAnother CountedPtr which will share ownership of the target.

Definition at line 388 of file countedptr.h.

template<typename TypePointedTo>
void Mezzanine::CountedPtr< TypePointedTo >::reset ( const CountedPtr< TypePointedTo > &  Other)
inline

Reset this to point at the same as another CountedPtr of the same type.

Parameters
OtherAnother CountedPtr which will share ownership of the target.
Note
Provided to match method names on standard smart pointers

Definition at line 398 of file countedptr.h.

template<typename TypePointedTo>
void Mezzanine::CountedPtr< TypePointedTo >::Reset ( TypePointedTo *  PointerTarget)
inline

Take ownership of the passed pointer.

Parameters
PointerTargetThe pointer to assume ownership of.

Definition at line 403 of file countedptr.h.

template<typename TypePointedTo>
void Mezzanine::CountedPtr< TypePointedTo >::reset ( TypePointedTo *  PointerTarget)
inline

Take ownership of the passed pointer.

Parameters
PointerTargetThe pointer to assume ownership of.
Note
Provided to match method names on standard smart pointers

Definition at line 413 of file countedptr.h.

template<typename TypePointedTo>
Boole Mezzanine::CountedPtr< TypePointedTo >::Unique ( ) const
throw (
)
inline

Is this the only pointer to the managed object.

Returns
True if use_count() == 1 or if the pointer is invalid
Exceptions
NothingThis member function does not throw exceptions.
Note
This name was chosen to match standard compliant names, and should be usable in templates that require this function.

Definition at line 470 of file countedptr.h.

template<typename TypePointedTo>
Boole Mezzanine::CountedPtr< TypePointedTo >::unique ( ) const
throw (
)
inline

Is this the only pointer to the managed object.

Returns
True if use_count() == 1 or if the pointer is invalid
Exceptions
NothingThis member function does not throw exceptions.
Note
This name was chosen to match standard compliant names, and should be usable in templates that require this function.
Provided to match method names on standard smart pointers

Definition at line 475 of file countedptr.h.

template<typename TypePointedTo>
Whole Mezzanine::CountedPtr< TypePointedTo >::use_count ( ) const
throw (
)
inline

Get the current count of references.

Note
This name was chosen to match standard compliant names, and should be usable in templates that require this function.
Returns
The amount of references which still exist, or 0 if the reference counter is somehow invalid.
Exceptions
NothingThis member function does not throws exceptions.
Note
Provided to match method names on standard smart pointers

Definition at line 425 of file countedptr.h.

template<typename TypePointedTo>
Whole Mezzanine::CountedPtr< TypePointedTo >::UseCount ( ) const
throw (
)
inline

Get the current count of references.

Note
This name was chosen to match standard compliant names, and should be usable in templates that require this function.
Returns
The amount of references which still exist, or 0 if the reference counter is somehow invalid.
Exceptions
NothingThis member function does not throws exceptions.

Definition at line 420 of file countedptr.h.

Friends And Related Function Documentation

template<typename TypePointedTo>
template<typename ReturnType , typename OtherPointerTargetType >
CountedPtr<ReturnType> CountedPtrStaticCast ( CountedPtr< OtherPointerTargetType > &  Original)
friend

Static casting can access internals also.

Template Parameters
ReturnTypeThe type to be returned, must be specified
OtherPointerTargetTypeThe type of the provided pointer, this can be infered and should not be provided.
Parameters
OriginalThe pointer to convert.
Returns
Either a pointer of the desired or a compilation error

Definition at line 661 of file countedptr.h.


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