A class template, scoped_resource, designed to implement the Resource Acquisition Is Initialization (RAII) approach to resource management. More...
#include <scoped_resource.hpp>
Public Types | |
typedef T | resource_type |
typedef ReleasePolicy | release_type |
Public Member Functions | |
scoped_resource (resource_type res=resource_type()) | |
Constructor. More... | |
virtual | ~scoped_resource () |
The destructor is the main point in this class. More... | |
operator resource_type () const | |
This operator makes sure you can access and use the scoped_resource just like you were using the resource itself. More... | |
resource_type | get () const |
This function provides explicit access to the resource. More... | |
resource_type | operator-> () const |
This function provides convenient direct access to the -> operator if the underlying resource is a pointer. More... | |
void | assign (const resource_type &o) |
Private Member Functions | |
scoped_resource (const scoped_resource &) | |
scoped_resource & | operator= (const scoped_resource &) |
Private Attributes | |
T | resource |
A class template, scoped_resource, designed to implement the Resource Acquisition Is Initialization (RAII) approach to resource management.
scoped_resource is designed to be used when a resource is initialized at the beginning or middle of a scope, and released at the end of the scope. The template argument ReleasePolicy is a functor which takes an argument of the type of the resource, and releases it.
Usage example, for working with files:
Note that scoped_resource has an explicit constructor, and prohibits copy-construction, and thus the initialization syntax. The assignment syntax must be used when initializing.
I.e. using scoped_resource<int,close_file> file = open("file.txt",O_RDONLY); in the above example is illegal.
Definition at line 60 of file scoped_resource.hpp.
typedef ReleasePolicy util::scoped_resource< T, ReleasePolicy >::release_type |
Definition at line 69 of file scoped_resource.hpp.
typedef T util::scoped_resource< T, ReleasePolicy >::resource_type |
Definition at line 68 of file scoped_resource.hpp.
|
private |
|
inline |
Constructor.
res | This is the resource to be managed |
Definition at line 76 of file scoped_resource.hpp.
|
inlinevirtual |
The destructor is the main point in this class.
It takes care of proper deletion of the resource, using the provided release policy.
Definition at line 84 of file scoped_resource.hpp.
|
inline |
Definition at line 112 of file scoped_resource.hpp.
Referenced by surface::assign(), and mp::create::hide_children().
|
inline |
This function provides explicit access to the resource.
Its behavior is identical to operator resource_type()
Definition at line 103 of file scoped_resource.hpp.
Referenced by surface::assign(), surface::get(), surface::null(), surface::operator SDL_Surface *(), surface::operator->(), and surface::surface().
|
inline |
This operator makes sure you can access and use the scoped_resource just like you were using the resource itself.
Definition at line 95 of file scoped_resource.hpp.
|
inline |
This function provides convenient direct access to the -> operator if the underlying resource is a pointer.
Only call this function if resource_type is a pointer type.
Definition at line 110 of file scoped_resource.hpp.
|
private |
|
private |
Definition at line 62 of file scoped_resource.hpp.
Referenced by util::scoped_resource< SDL_Surface *, free_sdl_surface >::assign(), util::scoped_resource< SDL_Surface *, free_sdl_surface >::get(), util::scoped_resource< SDL_Surface *, free_sdl_surface >::operator resource_type(), util::scoped_resource< SDL_Surface *, free_sdl_surface >::operator->(), and util::scoped_resource< SDL_Surface *, free_sdl_surface >::~scoped_resource().