C12Adapter Opensource C++ Interface
MValueEndScopeSetter< Type > Class Template Reference

Class that helps set a certain value when a certain scope exits. More...

Inheritance diagram for MValueEndScopeSetter< Type >:

Public Member Functions

 MValueEndScopeSetter (Type *var, const Type &endScopeValue)
 Constructor of the end scope setter that accepts the pointer to the variable which shall be assigned at the end of the scope. More...
 
 ~MValueEndScopeSetter ()
 Destructor, a place of delayed assignment of the variable given in constructor.
 
void SetEndScopeValue (const Type &endScopeValue)
 A way of overwriting of the value that has to be assigned at destruction of end scope setter.
 
- Public Member Functions inherited from MGenericNoncopyablePtr< Type >
Type * operator-> () const
 Field dereference operator.
 
Type & operator* () const
 Pointer dereference operator.
 
Type * get () const
 Get the underlying pointer.
 
Type * release ()
 Return the the underlying pointer while nullifying the unique pointer object.
 

Additional Inherited Members

- Public Types inherited from MGenericNoncopyablePtr< Type >
typedef Type element_type
 Type of the unique pointer.
 
- Protected Member Functions inherited from MGenericNoncopyablePtr< Type >
 MGenericNoncopyablePtr (Type *ptr=NULL)
 Protected explicit initialization constructor.
 
 ~MGenericNoncopyablePtr ()
 Protected destructor. More...
 
- Protected Attributes inherited from MGenericNoncopyablePtr< Type >
Type * m_pointer
 Pointer to object.
 

Detailed Description

template<typename Type>
class MValueEndScopeSetter< Type >

Class that helps set a certain value when a certain scope exits.

This is basically to delay the assignment of a given variable to the time of destruction of end scope setter. The type of the variable to manipulate shall be assignable, or a compile error will result from the usage attempt. Typical use case:

doneAnyhow = false; // assume this is a global variable
{
...
MValueEndScopeSetter<int> endScopeSetter(doneAnyhow, true);
...
// here the value of doneAnyhow can be freely manipulated
...
}
// No matter how this scope is exited, with an exception or not, doneAnyhow will be true here

Constructor & Destructor Documentation

template<typename Type>
MValueEndScopeSetter< Type >::MValueEndScopeSetter ( Type *  var,
const Type &  endScopeValue 
)
inline

Constructor of the end scope setter that accepts the pointer to the variable which shall be assigned at the end of the scope.

The constructor will store the address of the variable and the value that shall be assigned at the end, and delay the assignment to the destructor.

Parameters
varPointer to the value that has to be assigned at destructor.
endScopeValueThe value to which to assign *var at the time of destruction of end scope setter.
Precondition
The type of variable shall be assignable with the assignment operator, or the code will not compile.
See also
SetEndScopeValue - a way to overwrite endScopeValue parameter before destruction of end scope setter.