The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
util::scoped_resource< T, ReleasePolicy > Class Template Reference

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_resourceoperator= (const scoped_resource &)
 

Private Attributes

resource
 

Detailed Description

template<typename T, typename ReleasePolicy>
class util::scoped_resource< T, ReleasePolicy >

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:

struct close_file { void operator()(int fd) const {close(fd);} };
...
{
const scoped_resource<int,close_file> file(open("file.txt",O_RDONLY));
read(file, buf, 1000);
} // file is automatically closed here

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.

Member Typedef Documentation

template<typename T, typename ReleasePolicy>
typedef ReleasePolicy util::scoped_resource< T, ReleasePolicy >::release_type

Definition at line 69 of file scoped_resource.hpp.

template<typename T, typename ReleasePolicy>
typedef T util::scoped_resource< T, ReleasePolicy >::resource_type

Definition at line 68 of file scoped_resource.hpp.

Constructor & Destructor Documentation

template<typename T, typename ReleasePolicy>
util::scoped_resource< T, ReleasePolicy >::scoped_resource ( const scoped_resource< T, ReleasePolicy > &  )
private
template<typename T, typename ReleasePolicy>
util::scoped_resource< T, ReleasePolicy >::scoped_resource ( resource_type  res = resource_type())
inline

Constructor.

Parameters
resThis is the resource to be managed

Definition at line 76 of file scoped_resource.hpp.

template<typename T, typename ReleasePolicy>
virtual util::scoped_resource< T, ReleasePolicy >::~scoped_resource ( )
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.

Member Function Documentation

template<typename T, typename ReleasePolicy>
void util::scoped_resource< T, ReleasePolicy >::assign ( const resource_type o)
inline

Definition at line 112 of file scoped_resource.hpp.

Referenced by surface::assign(), and mp::create::hide_children().

template<typename T, typename ReleasePolicy>
resource_type util::scoped_resource< T, ReleasePolicy >::get ( ) const
inline

This function provides explicit access to the resource.

Its behavior is identical to operator resource_type()

Returns
the underlying resource

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

template<typename T, typename ReleasePolicy>
util::scoped_resource< T, ReleasePolicy >::operator resource_type ( ) const
inline

This operator makes sure you can access and use the scoped_resource just like you were using the resource itself.

Returns
the underlying resource

Definition at line 95 of file scoped_resource.hpp.

template<typename T, typename ReleasePolicy>
resource_type util::scoped_resource< T, ReleasePolicy >::operator-> ( ) const
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.

template<typename T, typename ReleasePolicy>
scoped_resource& util::scoped_resource< T, ReleasePolicy >::operator= ( const scoped_resource< T, ReleasePolicy > &  )
private

Member Data Documentation

template<typename T, typename ReleasePolicy>
T util::scoped_resource< T, ReleasePolicy >::resource
private

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