Safe pointer to a sf::Resource<T> More...
#include <Resource.hpp>
Public Member Functions | |
ResourcePtr () | |
Default constructor. | |
ResourcePtr (const T *resource) | |
Construct from a raw pointer. | |
ResourcePtr (const ResourcePtr< T > ©) | |
Copy constructor. | |
~ResourcePtr () | |
Destructor. | |
ResourcePtr< T > & | operator= (const ResourcePtr< T > &right) |
Assignment operator for a ResourcePtr parameter. | |
ResourcePtr< T > & | operator= (const T *resource) |
Assignment operator for a raw pointer parameter. | |
operator const T * () const | |
Cast operator to implicitely convert the resource pointer to its raw pointer type (T*) | |
const T & | operator* () const |
Overload of unary operator *. | |
const T * | operator-> () const |
Overload of operator -> | |
void | OnResourceDestroyed () |
Function called when the observed resource is about to be destroyed. |
Safe pointer to a sf::Resource<T>
sf::ResourcePtr is a special kind of smart pointer for resources.
Its main feature is to automatically reset its internal pointer to 0 when the resource gets destroyed, so that pointers to a resource never become invalid when the resource is destroyed. Instead, it properly returns 0 when the resource no longer exists.
Its usage is completely transparent, so that it is similar to manipulating the raw resource directly (like any smart pointer).
For sf::ResourcePtr<T> to work, T must inherit from the sf::Resource class.
These two classes are heavily used internally in SFML to safely handle resources and the classes that use them:
sf::Resource and sf::ResourcePtr are designed for internal use, but if you feel like they would fit well in your implementation there's no problem to use them.
Definition at line 127 of file Resource.hpp.
ResourcePtr::ResourcePtr | ( | ) |
Default constructor.
A default constructed ResourcePtr is empty (null).
Definition at line 28 of file ResourcePtr.inl.
ResourcePtr::ResourcePtr | ( | const T * | resource | ) |
Construct from a raw pointer.
resource | Raw pointer to the resource to wrap |
Definition at line 37 of file ResourcePtr.inl.
ResourcePtr::ResourcePtr | ( | const ResourcePtr< T > & | copy | ) |
Copy constructor.
The new ResourcePtr will share the same resource as copy.
copy | Instance to copy |
Definition at line 47 of file ResourcePtr.inl.
sf::ResourcePtr< T >::~ResourcePtr | ( | ) |
Destructor.
void sf::ResourcePtr< T >::OnResourceDestroyed | ( | ) |
Function called when the observed resource is about to be destroyed.
This functions is called by the destructor of the pointed resource. It allows this instance to reset its internal pointer when the resource is destroyed, and avoid dangling pointers.
ResourcePtr::operator const T * | ( | ) | const |
Cast operator to implicitely convert the resource pointer to its raw pointer type (T*)
This might be dangerous in the general case, but in this context it is safe enough to define this operator.
Definition at line 98 of file ResourcePtr.inl.
const T& sf::ResourcePtr< T >::operator* | ( | ) | const |
Overload of unary operator *.
Like raw pointers, applying the * operator returns a reference to the pointed object.
const T* sf::ResourcePtr< T >::operator-> | ( | ) | const |
Overload of operator ->
Like raw pointers, applying the -> operator returns the pointed object.
ResourcePtr< T > & ResourcePtr::operator= | ( | const ResourcePtr< T > & | right | ) |
Assignment operator for a ResourcePtr parameter.
right | Instance to assign |
Definition at line 66 of file ResourcePtr.inl.
ResourcePtr< T > & ResourcePtr::operator= | ( | const T * | resource | ) |
Assignment operator for a raw pointer parameter.
resource | Resource to assign |
Definition at line 82 of file ResourcePtr.inl.