UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SharedPtr< T > Class Template Reference

#include <sharedptr.h>

Public Types

typedef int RefValue
 
typedef T ValueType
 
typedef T * PointerType
 
typedef const T * ConstPointerType
 
typedef T & ReferenceType
 
typedef const T & ConstReferenceType
 

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
 
bool operator< (ConstPointerType other) const
 
bool operator< (ConstReferenceType other) const
 
PointerType get () const
 
 operator bool () const
 
bool unique () const
 
void reset ()
 
template<class T2 >
bool operator== (const SharedPtr< T2 > &r) const
 
template<class T2 >
bool operator!= (const SharedPtr< T2 > &r) const
 
RefValue refCount () const
 

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 SharedPtr< T >

A simple shared pointer implementation modeled 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: 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<B>" object to a "SharedPtr<A>" 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 95 of file sharedptr.h.

Member Typedef Documentation

template<class T>
typedef const T* SharedPtr< T >::ConstPointerType

Definition at line 103 of file sharedptr.h.

template<class T>
typedef const T& SharedPtr< T >::ConstReferenceType

Definition at line 105 of file sharedptr.h.

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

Definition at line 102 of file sharedptr.h.

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

Definition at line 104 of file sharedptr.h.

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

Definition at line 100 of file sharedptr.h.

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

Definition at line 101 of file sharedptr.h.

Constructor & Destructor Documentation

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

Definition at line 107 of file sharedptr.h.

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

Definition at line 113 of file sharedptr.h.

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

Definition at line 119 of file sharedptr.h.

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

Definition at line 124 of file sharedptr.h.

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

Definition at line 131 of file sharedptr.h.

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

Definition at line 138 of file sharedptr.h.

Member Function Documentation

template<class T>
void SharedPtr< T >::decRef ( )
inlineprivate
template<class T>
PointerType 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 197 of file sharedptr.h.

Referenced by SharedPtr< dbuffer >::operator!=(), SharedPtr< dbuffer >::operator==(), UI_AbstractScrollableNodeMoveDown(), UI_AbstractScrollableNodeMoveEnd(), UI_AbstractScrollableNodeMoveHome(), UI_AbstractScrollableNodeMoveUp(), UI_AbstractScrollableNodePageDown(), UI_AbstractScrollableNodePageUp(), UI_AbstractValue_DecValue(), UI_AbstractValue_GetDelta(), UI_AbstractValue_GetLastDiff(), UI_AbstractValue_GetMax(), UI_AbstractValue_GetMin(), UI_AbstractValue_GetShiftIncreaseFactor(), UI_AbstractValue_GetValue(), UI_AbstractValue_IncValue(), UI_AbstractValue_SetDelta(), UI_AbstractValue_SetMax(), UI_AbstractValue_SetMin(), UI_AbstractValue_SetRange(), UI_AbstractValue_SetValue(), UI_CheckBox_Toggle(), UI_CheckBox_ValueAsBoolean(), UI_CheckBox_ValueAsInteger(), UI_GeoscapeNodeZoomIn(), UI_GeoscapeNodeZoomOut(), UI_Node_Activate(), UI_Node_CapturedMouseLost(), UI_Node_CapturedMouseMove(), UI_Node_Clone(), UI_Node_DeleteNode(), UI_Node_DndDrop(), UI_Node_DndEnter(), UI_Node_DndFinished(), UI_Node_DndLeave(), UI_Node_DndMove(), UI_Node_DoLayout(), UI_Node_Draw(), UI_Node_DrawOverWindow(), UI_Node_DrawTooltip(), UI_Node_FocusGained(), UI_Node_FocusLost(), UI_Node_GetCellHeight(), UI_Node_GetCellWidth(), UI_Node_GetClientPosition(), UI_Node_InitNode(), UI_Node_InitNodeDynamic(), UI_Node_IsBattleScape(), UI_Node_IsDrawable(), UI_Node_IsOptionContainer(), UI_Node_IsScrollableContainer(), UI_Node_IsVirtual(), UI_Node_IsWindow(), UI_Node_KeyPressed(), UI_Node_KeyReleased(), UI_Node_LeftClick(), UI_Node_Loaded(), UI_Node_Loading(), UI_Node_MiddleClick(), UI_Node_MouseDown(), UI_Node_MouseEnter(), UI_Node_MouseLeave(), UI_Node_MouseLongPress(), UI_Node_MouseMove(), UI_Node_MouseUp(), UI_Node_PosChanged(), UI_Node_PropertyChanged(), UI_Node_RightClick(), UI_Node_Scroll(), UI_Node_SizeChanged(), UI_Node_StartDragging(), UI_Node_WindowActivate(), UI_Node_WindowClosed(), UI_Node_WindowOpened(), UI_RadioButton_SetValue(), UI_SpinnerNodeRepeat(), and UI_TextScrollEnd().

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

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

Definition at line 206 of file sharedptr.h.

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

Definition at line 239 of file sharedptr.h.

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

Definition at line 170 of file sharedptr.h.

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

Definition at line 175 of file sharedptr.h.

template<class T>
bool SharedPtr< T >::operator< ( ConstPointerType  other) const
inline

Definition at line 181 of file sharedptr.h.

template<class T>
bool SharedPtr< T >::operator< ( ConstReferenceType  other) const
inline

Definition at line 186 of file sharedptr.h.

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

Definition at line 143 of file sharedptr.h.

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

Definition at line 157 of file sharedptr.h.

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

Definition at line 233 of file sharedptr.h.

template<class T>
RefValue 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 248 of file sharedptr.h.

Referenced by SharedPtr< dbuffer >::unique().

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

Resets the SharedPtr object to a nullptr pointer.

Definition at line 224 of file sharedptr.h.

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

Checks if the SharedPtr object is the only object referring to the assigned pointer. This should just be used for debugging purposes.

Definition at line 216 of file sharedptr.h.

Friends And Related Function Documentation

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

Definition at line 97 of file sharedptr.h.

Field Documentation

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

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