ScummVM
Common::SharedPtr< T > Class Template Reference

A simple shared pointer implementation modelled after boost. More...

#include <ptr.h>

Inheritance diagram for Common::SharedPtr< T >:
Collaboration diagram for Common::SharedPtr< T >:

Public Types

typedef int RefValue
 
typedef T ValueType
 
typedef T * PointerType
 
typedef T & ReferenceType
 

Public Member Functions

 SharedPtr ()
 
template<class T2 >
 SharedPtr (T2 *p)
 
template<class T2 , class D >
 SharedPtr (T2 *p, D d)
 
 SharedPtr (const SharedPtr &r)
 
template<class T2 >
 SharedPtr (const SharedPtr< T2 > &r)
 
 ~SharedPtr ()
 
SharedPtroperator= (const SharedPtr &r)
 
template<class T2 >
SharedPtroperator= (const SharedPtr< T2 > &r)
 
ReferenceType operator* () const
 
PointerType operator-> () const
 
PointerType get () const
 Returns the plain pointer value. More...
 
bool operator_bool () const
 Implicit conversion operator to bool for convenience, to make checks like "if (sharedPtr) ..." possible. More...
 
bool unique () const
 Checks if the SharedPtr object is the only object refering to the assigned pointer. More...
 
void reset ()
 Resets the SharedPtr object to a NULL pointer. More...
 
template<class T2 >
bool operator== (const SharedPtr< T2 > &r) const
 
template<class T2 >
bool operator!= (const SharedPtr< T2 > &r) const
 
RefValue refCount () const
 Returns the number of references to the assigned pointer. More...
 
- Public Member Functions inherited from Common::SafeBool< SharedPtr< T > >
 operator bool_type () const
 
 operator bool_type ()
 

Private Member Functions

void decRef ()
 

Private Attributes

RefValue_refCount
 
SharedPtrDeletionInternal_deletion
 
PointerType _pointer
 

Friends

template<class T2 >
class SharedPtr
 

Detailed Description

template<class T>
class Common::SharedPtr< T >

A simple shared pointer implementation modelled after boost.

This object keeps track of the assigned pointer and automatically frees it when no more SharedPtr references to it exist.

To achieve that the object implements an internal reference counting. Thus you should try to avoid using the plain pointer after assigning it to a SharedPtr object for the first time. If you still use the plain pointer be sure you do not delete it on your own. You may also not use the plain pointer to create a new SharedPtr object, since that would result in a double deletion of the pointer sooner or later.

Example creation: Common::SharedPtr<int> pointer(new int(1)); would create a pointer to int. Later on usage via *pointer is the same as for a normal pointer. If you need to access the plain pointer value itself later on use the get method. The class also supplies a operator ->, which does the same as the -> operator on a normal pointer.

Be sure you are using new to initialize the pointer you want to manage. If you do not use new for allocating, you have to supply a deleter as second parameter when creating a SharedPtr object. The deleter has to implement operator() which takes the pointer it should free as argument.

Note that you have to specify the type itself not the pointer type as template parameter.

When creating a SharedPtr object from a normal pointer you need a real definition of the type you want SharedPtr to manage, a simple forward definition is not enough.

The class has implicit upcast support, so if you got a class B derived from class A, you can assign a pointer to B without any problems to a SharedPtr object with template parameter A. The very same applies to assignment of a SharedPtr object to a SharedPtr object.

There are also operators != and == to compare two SharedPtr objects with compatible pointers. Comparison between a SharedPtr object and a plain pointer is only possible via SharedPtr::get.

Definition at line 102 of file ptr.h.

Member Typedef Documentation

◆ PointerType

template<class T>
typedef T* Common::SharedPtr< T >::PointerType

Definition at line 109 of file ptr.h.

◆ ReferenceType

template<class T>
typedef T& Common::SharedPtr< T >::ReferenceType

Definition at line 110 of file ptr.h.

◆ RefValue

template<class T>
typedef int Common::SharedPtr< T >::RefValue

Definition at line 107 of file ptr.h.

◆ ValueType

template<class T>
typedef T Common::SharedPtr< T >::ValueType

Definition at line 108 of file ptr.h.

Constructor & Destructor Documentation

◆ SharedPtr() [1/5]

template<class T>
Common::SharedPtr< T >::SharedPtr ( )
inline

Definition at line 112 of file ptr.h.

◆ SharedPtr() [2/5]

template<class T>
template<class T2 >
Common::SharedPtr< T >::SharedPtr ( T2 *  p)
inlineexplicit

Definition at line 115 of file ptr.h.

◆ SharedPtr() [3/5]

template<class T>
template<class T2 , class D >
Common::SharedPtr< T >::SharedPtr ( T2 *  p,
D  d 
)
inline

Definition at line 118 of file ptr.h.

◆ SharedPtr() [4/5]

template<class T>
Common::SharedPtr< T >::SharedPtr ( const SharedPtr< T > &  r)
inline

Definition at line 120 of file ptr.h.

◆ SharedPtr() [5/5]

template<class T>
template<class T2 >
Common::SharedPtr< T >::SharedPtr ( const SharedPtr< T2 > &  r)
inline

Definition at line 122 of file ptr.h.

◆ ~SharedPtr()

template<class T>
Common::SharedPtr< T >::~SharedPtr ( )
inline

Definition at line 124 of file ptr.h.

Member Function Documentation

◆ decRef()

template<class T>
void Common::SharedPtr< T >::decRef ( )
inlineprivate

Definition at line 203 of file ptr.h.

◆ get()

template<class T>
PointerType Common::SharedPtr< T >::get ( ) const
inline

Returns the plain pointer value.

Be sure you know what you do if you are continuing to use that pointer.

Returns
the pointer the SharedPtr object manages

Definition at line 160 of file ptr.h.

◆ operator!=()

template<class T>
template<class T2 >
bool Common::SharedPtr< T >::operator!= ( const SharedPtr< T2 > &  r) const
inline

Definition at line 191 of file ptr.h.

◆ operator*()

template<class T>
ReferenceType Common::SharedPtr< T >::operator* ( ) const
inline

Definition at line 151 of file ptr.h.

◆ operator->()

template<class T>
PointerType Common::SharedPtr< T >::operator-> ( ) const
inline

Definition at line 152 of file ptr.h.

◆ operator=() [1/2]

template<class T>
SharedPtr& Common::SharedPtr< T >::operator= ( const SharedPtr< T > &  r)
inline

Definition at line 126 of file ptr.h.

◆ operator=() [2/2]

template<class T>
template<class T2 >
SharedPtr& Common::SharedPtr< T >::operator= ( const SharedPtr< T2 > &  r)
inline

Definition at line 139 of file ptr.h.

◆ operator==()

template<class T>
template<class T2 >
bool Common::SharedPtr< T >::operator== ( const SharedPtr< T2 > &  r) const
inline

Definition at line 186 of file ptr.h.

◆ operator_bool()

template<class T>
bool Common::SharedPtr< T >::operator_bool ( ) const
inline

Implicit conversion operator to bool for convenience, to make checks like "if (sharedPtr) ..." possible.

Definition at line 166 of file ptr.h.

◆ refCount()

template<class T>
RefValue Common::SharedPtr< T >::refCount ( ) const
inline

Returns the number of references to the assigned pointer.

This should just be used for debugging purposes.

Definition at line 199 of file ptr.h.

◆ reset()

template<class T>
void Common::SharedPtr< T >::reset ( )
inline

Resets the SharedPtr object to a NULL pointer.

Definition at line 178 of file ptr.h.

◆ unique()

template<class T>
bool Common::SharedPtr< T >::unique ( ) const
inline

Checks if the SharedPtr object is the only object refering to the assigned pointer.

This should just be used for debugging purposes.

Definition at line 173 of file ptr.h.

Friends And Related Function Documentation

◆ SharedPtr

template<class T>
template<class T2 >
friend class SharedPtr
friend

Definition at line 104 of file ptr.h.

Member Data Documentation

◆ _deletion

template<class T>
SharedPtrDeletionInternal* Common::SharedPtr< T >::_deletion
private

Definition at line 217 of file ptr.h.

◆ _pointer

template<class T>
PointerType Common::SharedPtr< T >::_pointer
private

Definition at line 218 of file ptr.h.

◆ _refCount

template<class T>
RefValue* Common::SharedPtr< T >::_refCount
private

Definition at line 216 of file ptr.h.


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