Logo Search packages:      
Sourcecode: scummvm version File versions

Common::SharedPtr< T > Class Template Reference

#include <ptr.h>

List of all members.

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 106 of file ptr.h.

Public Types

typedef T * Pointer
typedef int RefValue
typedef T ValueType

Public Member Functions

Pointer get () const
 operator bool () const
ValueType & operator* () const
Pointer operator-> () const
template<class T2>
SharedPtroperator= (const SharedPtr< T2 > &r)
SharedPtroperator= (const SharedPtr &r)
RefValue refCount () const
void reset ()
template<class T2>
 SharedPtr (const SharedPtr< T2 > &r)
 SharedPtr (const SharedPtr &r)
template<class T2, class D>
 SharedPtr (T2 *p, D d)
template<class T2>
 SharedPtr (T2 *p)
bool unique () const

Private Member Functions

void decRef ()

Private Attributes

SharedPtrDeletionInternal * _deletion
T * _pointer
RefValue * _refCount


class SharedPtr

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

Generated by  Doxygen 1.6.0   Back to index