chsvlib
chsv helper source code
AutoResource< HANDLE_TYPE, CCloser, IsHandleValidClass > Class Template Reference

Manages the specified resource handles, providing a limited garbage-collection facility. More...

#include <chsvmem.h>

Public Types

typedef AutoResource< HANDLE_TYPE, CCloser, IsHandleValidClass > MyType
 Type of this class aliased for writing less code.
 
typedef HANDLE_TYPE handle_type
 A type of a handle managed by an object of the AutoResource template instantiation class. The default handle() must not be allowed to be closed.
 
typedef CCloser closer_type
 A type of a closer object used to close the resource managed by an object of the AutoResource template instantiation class.
 
typedef IsHandleValidClass checker_type
 A type of a checker object used to check if a handle is allowed to be closed by an object of the AutoResource template instantiation class.
 

Public Member Functions

 AutoResource ()
 Constructs a default AutoResource object. More...
 
 AutoResource (handle_lref refHandle)
 Constructs an AutoResource object from an lvalue handle with the default closer and checker. More...
 
 AutoResource (handle_rref rrefHandle)
 Constructs an AutoResource object from an rvalue reference to a handle, using move semantics, with the default closer and checker. More...
 
 AutoResource (closer_lref refCloser)
 Constructs an AutoResource object from the specified lvalue reference to a closer object, with the value-initialized default handle and the default checker object. More...
 
 AutoResource (handle_lref refHandle, closer_lref refCloser)
 Constructs an AutoResource object from an lvalue handle and an lvalue closer object, with the default checker object. More...
 
 AutoResource (handle_rref rrefHandle, closer_lref refCloser)
 Constructs an AutoResource object from an rvalue reference to a handle, using move semantics, and from an lvalue closer, with the default checker. More...
 
 AutoResource (closer_rref rrefCloser)
 Constructs an AutoResource object from an rvalue reference to a closer object using move semantics, with the value-initialized default handle and the default checker object. More...
 
 AutoResource (handle_lref refHandle, closer_rref rrefCloser)
 Constructs an AutoResource object from an lvalue handle and an rvalue closer, with the default checker object. More...
 
 AutoResource (handle_rref rrefHandle, closer_rref rrefCloser)
 Constructs an AutoResource object from an rvalue handle and closer using move semantics, with the default checker object. More...
 
 AutoResource (checker_lref refChecker)
 Constructs an AutoResource object from the specified lvalue reference to a checker object, with the value-initialized default handle and the default closer object. More...
 
 AutoResource (handle_lref refHandle, checker_lref refChecker)
 Constructs an AutoResource object from the specified lvalue references to a handle to be managed and to a checker object, with the value-initialized default closer object. More...
 
 AutoResource (handle_rref rrefHandle, checker_lref refChecker)
 Constructs an AutoResource object from the specified rvalue reference to a handle using move semantics and an lvalue reference to a checker object, with the value-initialized default closer. More...
 
 AutoResource (closer_lref refCloser, checker_lref refChecker)
 Constructs an AutoResource object from lvalue references to a closer and checker used to close a managed handle and to check allowance for closing respectively. The handle is value-initialized. More...
 
 AutoResource (handle_lref refHandle, closer_lref refCloser, checker_lref refChecker)
 Constructs an AutoResource object from lvalue handle and references to closer and checker used to close the handle and to check allowance for closing respectively. More...
 
 AutoResource (handle_rref rrefHandle, closer_lref refCloser, checker_lref refChecker)
 Constructs an AutoResource object from an rvalue handle using move semantics and from lvalue references to closer and checker used to close the handle and to check allowance for closing respectively. More...
 
 AutoResource (closer_rref rrefCloser, checker_lref refChecker)
 Constructs an AutoResource object from an rvalue closer and an lvalue checker used to close a managed handle and to check allowance for closing respectively. The handle is value-initialized. More...
 
 AutoResource (handle_lref refHandle, closer_rref rrefCloser, checker_lref refChecker)
 Constructs an AutoResource object from lvalue handle, rvalue closer and lvalue checker. The handle will be managed by the created object using the closer and the checker to close the handle and to get permission for it respectively. More...
 
 AutoResource (handle_rref rrefHandle, closer_rref rrefCloser, checker_lref refChecker)
 Constructs an AutoResource object from rvalues of a handle and a closer using move semantics and from an lvalue checker. The handle will be managed by the created object using the closer and the checker to close the handle and to get permission for it respectively. More...
 
 AutoResource (checker_rref rrefChecker)
 Constructs an AutoResource object from an rvalue reference to a checker object using move semantics, with the value-initialized default handle and the default closer object. More...
 
 AutoResource (handle_lref refHandle, checker_rref rrefChecker)
 Constructs an AutoResource object from an lvalue handle and an rvalue checker, with the default closer object. More...
 
 AutoResource (handle_rref rrefHandle, checker_rref rrefChecker)
 Constructs an AutoResource object from an rvalue handle and checker using move semantics, with the default closer object. More...
 
 AutoResource (closer_lref refCloser, checker_rref rrefChecker)
 Constructs an AutoResource object from an lvalue closer and an rvalue checker user to close a managed handle and to check allowance for closing respectively. The handle itself is value-initialized. More...
 
 AutoResource (handle_lref refHandle, closer_lref refCloser, checker_rref rrefChecker)
 Constructs an AutoResource object from lvalue handle and closer as well as an rvalue checker. The closer and the checker are to be used to close the handle and to check allowance for the closing respectively. More...
 
 AutoResource (handle_rref rrefHandle, closer_lref refCloser, checker_rref rrefChecker)
 Constructs an AutoResource object from rvalues of a handle and a checker using move semantics and from an lvalue reference to a closer used to close the handle. More...
 
 AutoResource (closer_rref rrefCloser, checker_rref rrefChecker)
 Constructs an AutoResource object from an rvalue of a closer and a checker to be used to close a managed handle and to check allowance for closing respectively. The handle is value-initialized. More...
 
 AutoResource (handle_lref refHandle, closer_rref rrefCloser, checker_rref rrefChecker)
 Constructs an AutoResource object from an lvalue handle, and from rvalues of a closer and a checker using move semantics. The handle will be managed by the created object using the closer and the checker to close the handle and to get permission for it respectively. More...
 
 AutoResource (handle_rref rrefHandle, closer_rref rrefCloser, checker_rref rrefChecker)
 Constructs an AutoResource object from an rvalues of a handle to be managed as well as rvalues of a closer and a checker using move semantics. The handle will be managed by the created object using the closer and the checker to close the handle and to get permission for it respectively. More...
 
 AutoResource (MyType &&rrRight)
 Constructs an AutoResource object by a given copy which is to be moved (semantically) to the instance being created. More...
 
MyTypeoperator= (MyType &&rrRight)
 An assignment operator. More...
 
const handle_norefget () const
 Returns a reference to an owned handle. The caller can call the get_checker()(get()) to verify validity of the returned handle. If verification fails the AutoResource object is assumed to own nothing. More...
 
handle_norefget ()
 Returns a reference to an owned handle. The caller can call the get_checker()(get()) to verify validity of the returned handle.
 
closer_norefget_closer ()
 Returns a reference to a stored closer object used to close the handle managed by the AutoResource instance.
 
const closer_norefget_closer () const
 Returns a reference to a stored closer object used to close the handle managed by the AutoResource instance.
 
checker_norefget_checker ()
 Returns a reference to a stored checker object used to verify if the current AutoResource instance is allowed to close its handle.
 
const checker_norefget_checker () const
 Returns a reference to a stored checker object used to verify if the current AutoResource instance is allowed to close its handle.
 
void swap (MyType &rRight)
 Exchanges the contents of the AutoResource object with those of rRight, transferring ownership of any managed object between them without destroying either. More...
 
 operator bool () const
 Checks if the stored handle can be closed by calling the checker. More...
 
handle_type release ()
 Releases ownership of the stored handle, if any, by returning its value and replacing it with a value-initialized handle, i.e. handle_type(). More...
 
void reset (handle_lref rHandle)
 Closes the resource currently managed by the AutoResource (if any) and takes ownership of rHandle. More...
 
void reset (handle_rref rrHandle=handle_type())
 Closes the resource currently managed by the AutoResource (if any) and takes ownership of rrHandle. More...
 

Protected Types

typedef std::remove_reference< handle_type >::type handle_noref
 
typedef std::conditional< std::is_reference< handle_type >::value, handle_type, const handle_noref & >::type handle_lref
 A type defined as a const lvalue reference to a handle_type object, if the handle_type itself is not a reference, or handle_type itself otherwise.
 
typedef handle_noref && handle_rref
 An rvalue reference to a handle_type object used in move expressions.
 
typedef std::remove_reference< closer_type >::type closer_noref
 
typedef std::conditional< std::is_reference< closer_type >::value, closer_type, const closer_noref & >::type closer_lref
 A type defined as a const lvalue reference to a closer object, if the closer_type itself is not a reference, or closer_type itself otherwise.
 
typedef closer_noref && closer_rref
 An rvalue reference to a closer_type object used in move expressions.
 
typedef std::remove_reference< checker_type >::type checker_noref
 
typedef std::conditional< std::is_reference< checker_type >::value, checker_type, const checker_type & >::type checker_lref
 A type defined as a const lvalue reference to a checker object, if the checker_type itself is not a reference, or checker_type itself otherwise.
 
typedef checker_noref && checker_rref
 An rvalue reference to a checker_type object used in move expressions.
 

Protected Attributes

handle_type m_handle
 Managed resource handle itself.
 

Detailed Description

template<class HANDLE_TYPE, class CCloser, class IsHandleValidClass = DefaultResourceHandleChecker<HANDLE_TYPE>>
class Chusov::Memory::AutoResource< HANDLE_TYPE, CCloser, IsHandleValidClass >

Manages the specified resource handles, providing a limited garbage-collection facility.

These objects have the ability of taking ownership of a resource handle: once they take ownership they manage the resource by becoming responsible for its closing at some point. AutoResource objects automatically close the handle they manage (using a closer) as soon as they themselves are destroyed, or as soon as their value changes either by an assignment operation or by an explicit call to AutoResource::reset.

AutoResource objects own their handles uniquely: no other facility shall take care of the owned object, and thus no other managed handle should describe the same resource, since as soon as they have to, AutoResource objects close their managed resources without taking into account whether other handles exist for the same resource or not, and thus leaving any other handles to the same resource invalid. This of course is not applied to specially duplicated handles so that closing of the one handle will keep access to the resource opened and valid if gained using the duplicated handle.

An AutoResource object has three components:

  • a stored handle to the resource it manages. This is set on construction, can be altered by an assignment operation or by calling member reset, and can be individually accessed for reading using members get or release.
  • a stored closer: a callable object that takes an argument of the same type as the stored handle and is called to close the managed resource. It is set on construction, can be altered by an assignment operation, and can be individually accessed using member get_closer.
  • a stored checker: a callable object taking an argument of the same type as the stored handle and is called to check if the handle is allowed to be closed. The default checker class is the DefaultResourceHandleChecker instances of which provide minimum check if the handle has its default value (the case when the handle is disallowed to be closed). It is set on construction, can be altered by an assignment operation, and can be individually accessed using member get_checker.
Template Parameters
HANDLE_TYPEis the type of the managed handle, aliased as member type handle_type.
CCloseris the type of the callable object used as closer, aliased as member type closer_type.
IsHandleValidClassis the type of the callable object used as checker, aliased as member type checker_type. The check is performed by calling the
bool IsHandleValidClass::operator()(HANDLE_TYPE h) const;
which must return true if the handle h is allowed to be closed and false otherwise. The check must fail for default value-initialized handles. By default, this is a DefaultResourceHandleChecker providing minimum check if the handle has its default value (the case when the handle is disallowed to be closed).
Warning
The checker callable object must not allow the closing of the default constructed handle (i.e. handle_type()).

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