Shared ownership smart pointer allowing safe access to resources. More...
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. | |
ResourcePtr & | operator= (const ResourcePtr &origin) |
Copy assignment operator. | |
template<class Resource2 > | |
ResourcePtr & | operator= (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. |
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.
Resource | The 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. |
thor::ResourcePtr< Resource >::ResourcePtr | ( | ) |
Default constructor.
Initializes the object with a null pointer.
thor::ResourcePtr< Resource >::ResourcePtr | ( | const ResourcePtr< Resource > & | origin | ) |
Copy constructor.
Increments the reference counter (if the resource is not NULL).
thor::ResourcePtr< Resource >::ResourcePtr | ( | const ResourcePtr< Resource2 > & | origin | ) |
Template constructor from less const-qualified ResourcePtr.
Allows conversion from ResourcePtr<Resource> to ResourcePtr<const 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.
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.
Resource& thor::ResourcePtr< Resource >::operator* | ( | ) | const |
Dereferences the pointer.
Resource* thor::ResourcePtr< Resource >::operator-> | ( | ) | const |
Dereferences the pointer for member access.
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).
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>.
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.
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.