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>

Inheritance diagram for util::scoped_resource< T, ReleasePolicy >:

Inheritance graph
[legend]
Collaboration diagram for util::scoped_resource< T, ReleasePolicy >:

Collaboration graph
[legend]

List of all members.

Public Types

typedef T resource_type
typedef ReleasePolicy release_type

Public Member Functions

 scoped_resource (resource_type res=resource_type())
 Constructor.
virtual ~scoped_resource ()
 The destructor is the main point in this class.
 operator resource_type () const
 This operator makes sure you can access and use the scoped_resource just like you were using the resource itself.
resource_type get () const
 This function provides explicit access to the resource.
resource_type operator-> () const
 This function provides convenient direct access to the -> operator if the underlying resource is a pointer.
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 58 of file scoped_resource.hpp.


Member Typedef Documentation

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

Definition at line 66 of file scoped_resource.hpp.

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

Definition at line 67 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:
res This is the resource to be managed

Definition at line 74 of file scoped_resource.hpp.

template<typename T, typename ReleasePolicy>
virtual util::scoped_resource< T, ReleasePolicy >::~scoped_resource (  )  [inline, virtual]

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 82 of file scoped_resource.hpp.


Member Function Documentation

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

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 93 of file scoped_resource.hpp.

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

This function provides explicit access to the resource.

Its behaviour is identical to operator resource_type()

Returns:
the underlying resource

Definition at line 101 of file scoped_resource.hpp.

Referenced by gui::floating_textbox::active(), surface::assign(), surface::get(), map_editor::new_map_dialog(), surface::null(), surface::operator SDL_Surface *(), surface::operator->(), random_generate_map(), surface::surface(), and write_file().

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 108 of file scoped_resource.hpp.

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


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:

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs