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

Class that helps preserve a certain value of a variable outside a local C++ scope. More...

Inheritance diagram for MValueSavior< Type >:

Public Member Functions

 MValueSavior (Type *var)
 Constructor of the savior that accepts the pointer to the variable which value has to be saved. More...
 
 MValueSavior (Type *var, const Type &scopeValue)
 Constructor of the savior that accepts the pointer to the value that has to be saved, and a new value. More...
 
 ~MValueSavior ()
 Destructor that restores the variable given in constructor to its original value. More...
 
- 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 MValueSavior< Type >

Class that helps preserve a certain value of a variable outside a local C++ scope.

The constructor saves the given value, and the destructor restores it. The type of the variable to manipulate shall be assignable, or a compile error will result from the usage attempt. Typical use case:

// assume count is nonzero here, say 10
{
MValueSavior<int> countSavior(count, 0); // here the previous count is saved, and initialized to zero
... at this point the count can be manipulated freely
}
// here the value of count will be restored to 10 no matter how the above scope was exited

Constructor & Destructor Documentation

template<typename Type>
MValueSavior< Type >::MValueSavior ( Type *  var)
inline

Constructor of the savior that accepts the pointer to the variable which value has to be saved.

The constructor will store the address of the variable and its value, so it can be restored in the destructor.

Parameters
varPointer to the value that has to be saved through the time of existence of the value savior class.
Precondition
The type of variable shall be assignable with the assignment operator, or the code will not compile.
template<typename Type>
MValueSavior< Type >::MValueSavior ( Type *  var,
const Type &  scopeValue 
)
inline

Constructor of the savior that accepts the pointer to the value that has to be saved, and a new value.

The constructor will store the address of the value and the value itself, so it can be restored in the destructor. Then it will assign a new value given as parameter. This constructor is a convenient shortcut of the following:

MValueSavior<MStdString> savior(myString);
myString = "new value";

which can be written in a new line:

MValueSavior<MStdString> savior(myString, "new value");
Parameters
varPointer to the value that has to be saved through the time of existence of the value savior class.
scopeValueNew value with which the given variable will be initialized within this constructor.
Precondition
The type of variable shall be assignable with the assignment operator, or the code will not compile.
template<typename Type>
MValueSavior< Type >::~MValueSavior ( )
inline

Destructor that restores the variable given in constructor to its original value.

As value savior is often created on the stack, there is no necessity to call its destructor directly or by means of operator delete.