Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ScopedOverride Class Reference

Overrides the value of a variable and restores it to the original value when destructed. More...

#include <test_main.hxx>

Classes

class  Holder
 Type-accurate class that holds the temporary variable with the old value, the pointer to the variable and restores the previous state upon destruction. More...
 
class  HolderBase
 Virtual base class for the destructible holders. More...
 

Public Member Functions

template<class T , typename U >
 ScopedOverride (T *variable, U new_value)
 Constructor.
 
void restore ()
 Restores the original value.
 

Private Attributes

std::unique_ptr< HolderBaseholder_
 Smart ptr that will reset the variable to the previous value when going out of scope.
 

Detailed Description

Overrides the value of a variable and restores it to the original value when destructed.

Useful for changing flags for a single test only.

Usage: { ScopedOverride ov(&DATAGRAM_RESPONSE_TIMEOUT_NSEC, 100000); ... test code assuming new value ... } ... now the original value is restored.

Definition at line 332 of file test_main.hxx.

Constructor & Destructor Documentation

◆ ScopedOverride()

template<class T , typename U >
ScopedOverride::ScopedOverride ( T *  variable,
new_value 
)
inline

Constructor.

Parameters
variablewhat to set temporarily
new_valuewhat should be the new value of variable during this code block.

Definition at line 342 of file test_main.hxx.

Member Function Documentation

◆ restore()

void ScopedOverride::restore ( )
inline

Restores the original value.

Definition at line 348 of file test_main.hxx.

Member Data Documentation

◆ holder_

std::unique_ptr<HolderBase> ScopedOverride::holder_
private

Smart ptr that will reset the variable to the previous value when going out of scope.

Definition at line 393 of file test_main.hxx.


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