Bromeon
Public Member Functions | Related Functions
thor::ResourcePtr< Resource > Class Template Reference

Reference-counted smart pointer allowing safe access to resources. More...

List of all members.

Public Member Functions

 ResourcePtr ()
 Default constructor.
 ResourcePtr (Resource *rawPointer)
 Construct from new resource.
template<typename D >
 ResourcePtr (Resource *rawPointer, D deleter)
 Construct from new resource with custom deleter.
 ResourcePtr (const ResourcePtr &origin)
 Copy constructor.
template<class Resource2 >
 ResourcePtr (const ResourcePtr< Resource2 > &origin)
 Template constructor from less const-qualified ResourcePtr.
ResourcePtroperator= (const ResourcePtr &origin)
 Copy assignment operator.
template<class Resource2 >
ResourcePtroperator= (const ResourcePtr< Resource2 > &origin)
 Template assignment operator from less const-qualified ResourcePtr.
 ~ResourcePtr ()
 Destructor.
void Swap (ResourcePtr &other)
 Exchanges the contents of two instances.
void Reset ()
 Reset the pointer to NULL.
void Reset (Resource *rawPointer)
 Reset pointer to new resource.
template<typename D >
void Reset (Resource *rawPointer, D deleter)
 Reset pointer to new resource with custom deleter.
 operator SafeBool () const
 Checks whether the ResourcePtr references a valid resource or points to NULL.
Resource & operator* () const
 Dereferences the pointer.
Resource * operator-> () const
 Dereferences the pointer for member access.

Related Functions

template<class Resource >
void thor::swap (ResourcePtr< Resource > &lhs, ResourcePtr< Resource > &rhs)
 Swaps both ResourcePtr objects.
template<class Resource1 , class Resource2 >
bool thor::operator== (const ResourcePtr< Resource1 > &lhs, const ResourcePtr< Resource2 > &rhs)
 Equality comparison operator.
template<class Resource1 , class Resource2 >
bool thor::operator!= (const ResourcePtr< Resource1 > &lhs, const ResourcePtr< Resource2 > &rhs)
 Inequality comparison operator.
template<class Resource >
ResourcePtr< Resource > thor::NoDeletePtr (Resource *pointer)
 Returns a ResourcePtr that doesn't own the resource.

Detailed Description

template<class Resource>
class thor::ResourcePtr< Resource >

Reference-counted smart pointer allowing safe access to resources.

Objects of this class are returned from ResourceManager, when you acquire new or search for existing resources. You should keep a ResourcePtr instance everywhere you need access to a resource.

The ResourcePtr has the semantics of a strong reference. That is, as long as it points to a resource, the referenced resource cannot be released. This ensures that program parts that currently use the resource do not suddenly lose access to it.

Template Parameters:
ResourceThe resource class you want to access. If Resource is const-qualified, you cannot modify the resource object through this smart pointer. The type Resource can be incomplete as long as you don't dereference the pointer.

Constructor & Destructor Documentation

template<class Resource>
thor::ResourcePtr< Resource >::ResourcePtr ( )

Default constructor.

Initializes the object with a null pointer.

template<class Resource>
thor::ResourcePtr< Resource >::ResourcePtr ( Resource *  rawPointer) [explicit]

Construct from new resource.

Increments the reference counter for the new resource.

Parameters:
rawPointerRaw pointer to resource, allocated with the new operator.
template<class Resource>
template<typename D >
thor::ResourcePtr< Resource >::ResourcePtr ( Resource *  rawPointer,
deleter 
)

Construct from new resource with custom deleter.

Increments the reference counter for the new resource.

Parameters:
rawPointerRaw pointer to resource.
deleterCallable with the signature void(Resource*) that is invoked when the last ResourcePtr loses ownership. To allow ResourcePtr<Resource> instances where Resource is const-qualified, the deleter shall take a pointer to const.
template<class Resource>
thor::ResourcePtr< Resource >::ResourcePtr ( const ResourcePtr< Resource > &  origin)

Copy constructor.

Increments the reference counter (if the resource is not NULL).

template<class Resource>
template<class Resource2 >
thor::ResourcePtr< Resource >::ResourcePtr ( const ResourcePtr< Resource2 > &  origin)

Template constructor from less const-qualified ResourcePtr.

Allows conversion from ResourcePtr<Resource> to ResourcePtr<const Resource>.

template<class Resource>
thor::ResourcePtr< Resource >::~ResourcePtr ( )

Destructor.

Decrements the reference counter. If it reaches zero (i.e. this is the last ResourcePtr pointing to the resource) and if the corresponding ResourceManager uses the AutoRelease strategy, then the resource is released.


Member Function Documentation

template<class Resource>
thor::ResourcePtr< Resource >::operator SafeBool ( ) const

Checks whether the ResourcePtr references a valid resource or points to NULL.

This allows you to write statements of the form if(ptr) or if(!ptr) in a type-safe way (ptr is a ResourcePtr instance). The actual return type is implementation-defined.

Returns:
bool-like value that behaves in conditional expressions like true, if the pointer is valid; and like false, if it points to NULL.
template<class Resource>
Resource& thor::ResourcePtr< Resource >::operator* ( ) const

Dereferences the pointer.

Precondition:
The ResourcePtr must be non-empty.
template<class Resource>
Resource* thor::ResourcePtr< Resource >::operator-> ( ) const

Dereferences the pointer for member access.

Precondition:
The ResourcePtr must be non-empty.
template<class Resource>
ResourcePtr& thor::ResourcePtr< Resource >::operator= ( const ResourcePtr< Resource > &  origin)

Copy assignment operator.

Increments the reference counter for the new resource, and decrements the one for the old resource.

template<class Resource>
template<class Resource2 >
ResourcePtr& thor::ResourcePtr< Resource >::operator= ( const ResourcePtr< Resource2 > &  origin)

Template assignment operator from less const-qualified ResourcePtr.

Allows conversion from ResourcePtr<Resource> to ResourcePtr<const Resource>.

template<class Resource>
void thor::ResourcePtr< Resource >::Reset ( )

Reset the pointer to NULL.

If this instance is a null pointer, the call has no effects. Otherwise, the reference counter is decremented.

template<class Resource>
void thor::ResourcePtr< Resource >::Reset ( Resource *  rawPointer)

Reset pointer to new resource.

Increments the reference counter for the new resource, and decrements the one for the old resource.

Parameters:
rawPointerRaw pointer to resource.
template<class Resource>
template<typename D >
void thor::ResourcePtr< Resource >::Reset ( Resource *  rawPointer,
deleter 
)

Reset pointer to new resource with custom deleter.

Increments the reference counter for the new resource, and decrements the one for the old resource.

Parameters:
rawPointerRaw pointer to resource.
deleterCallable with the signature void(Resource*) that is invoked when the last ResourcePtr loses ownership. To allow ResourcePtr<Resource> instances where Resource is const-qualified, the deleter shall take a pointer to const.

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