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

Shared ownership smart pointer allowing safe access to resources. More...

List of all members.

Public Member Functions

 ResourcePtr ()
 Default constructor.
 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 ()
 Resets the internal pointer to NULL.
 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.

Detailed Description

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

Shared ownership 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. The only exception is the ResourceManager's destructor, which releases all stored resources and sets all ResourcePtrs in use to NULL.

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

Decrements the old reference counter, and increments the new one (if the pointer is not NULL).

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 ( )

Resets the internal pointer to NULL.

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.

template<class Resource>
void thor::ResourcePtr< Resource >::Swap ( ResourcePtr< Resource > &  other)

Exchanges the contents of two instances.

Note that this operation is for once not terribly efficient, since it involves updates of the resource-managing entities which keep track of the shared ResourcePtrs.


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