ScummVM API documentation
Common::SharedPtr< T > Class Template Reference

#include <ptr.h>

Inheritance diagram for Common::SharedPtr< T >:
Common::SafeBool< SharedPtr< T > > Common::impl::no_base< SharedPtr< T > >

Public Types

typedef T * PointerType
 
typedef T & ReferenceType
 
typedef BasePtrTrackerInternal::RefValue RefValue
 

Public Member Functions

 SharedPtr (std::nullptr_t)
 
template<class T2 >
 SharedPtr (T2 *p)
 
template<class T2 , class DL >
 SharedPtr (T2 *p, DL d)
 
 SharedPtr (const SharedPtr< T > &r)
 
template<class T2 >
 SharedPtr (const SharedPtr< T2 > &r)
 
template<class T2 >
 SharedPtr (const WeakPtr< T2 > &r)
 
SharedPtroperator= (const SharedPtr &r)
 
template<class T2 >
SharedPtroperator= (const SharedPtr< T2 > &r)
 
T & operator* () const
 
T * operator-> () const
 
PointerType get () const
 
template<class T2 >
bool operator== (const SharedPtr< T2 > &r) const
 
template<class T2 >
bool operator!= (const SharedPtr< T2 > &r) const
 
bool operator== (std::nullptr_t) const
 
bool operator!= (std::nullptr_t) const
 
bool operator_bool () const
 
int refCount () const
 
bool unique () const
 
void reset ()
 
template<class T2 >
void reset (const SharedPtr< T2 > &r)
 
template<class T2 >
void reset (const WeakPtr< T2 > &r)
 
void reset (T *ptr)
 
template<class T2 >
SharedPtr< T2 > staticCast () const
 
template<class T2 >
SharedPtr< T2 > dynamicCast () const
 
template<class T2 >
SharedPtr< T2 > constCast () const
 
template<class T2 >
SharedPtr< T2 > reinterpretCast () const
 
- Public Member Functions inherited from Common::SafeBool< SharedPtr< T > >
 operator bool_type () const
 
 operator bool_type ()
 

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.

Member Function Documentation

◆ 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

◆ 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.

◆ refCount()

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

Returns the number of strong references to the object.

◆ unique()

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

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

◆ reset() [1/4]

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

Resets the object to a NULL pointer.

◆ reset() [2/4]

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

Resets the object to the specified shared pointer

◆ reset() [3/4]

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

Resets the object to the specified weak pointer

◆ reset() [4/4]

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

Resets the object to the specified pointer

◆ staticCast()

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

Performs the equivalent of static_cast to a new pointer type

◆ dynamicCast()

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

Performs the equivalent of dynamic_cast to a new pointer type

◆ constCast()

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

Performs the equivalent of const_cast to a new pointer type

◆ reinterpretCast()

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

Performs the equivalent of const_cast to a new pointer type


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