Reference-counted smart pointer allowing safe access to resources. More...
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. | |
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 () |
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. |
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.
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 | ( | Resource * | rawPointer | ) | [explicit] |
Construct from new resource.
Increments the reference counter for the new resource.
rawPointer | Raw pointer to resource, allocated with the new operator. |
thor::ResourcePtr< Resource >::ResourcePtr | ( | Resource * | rawPointer, |
D | deleter | ||
) |
Construct from new resource with custom deleter.
Increments the reference counter for the new resource.
rawPointer | Raw pointer to resource. |
deleter | Callable 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. |
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.
Increments the reference counter for the new resource, and decrements the one for the old resource.
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 | ( | ) |
Reset the pointer to NULL.
If this instance is a null pointer, the call has no effects. Otherwise, the reference counter is decremented.
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.
rawPointer | Raw pointer to resource. |
void thor::ResourcePtr< Resource >::Reset | ( | Resource * | rawPointer, |
D | 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.
rawPointer | Raw pointer to resource. |
deleter | Callable 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. |