OpenTTD Source: Backup< T > Struct Template Reference

OpenTTD Source 13.1

Public Member Functions | Private Attributes
Backup< T > Struct Template Reference

Class to backup a specific variable and restore it later. More...

`#include <backup_type.hpp>`

## Public Member Functions
| Backup (T &original, const char *const file, const int line)
| Backup variable. More...
template<typename U >
| Backup (T &original, const U &new_value, const char *const file, const int line)
| Backup variable and switch to new value. More...
| ~Backup ()
| Check whether the variable was restored on object destruction.
bool | IsValid () const
| Checks whether the variable was already restored. More...
const T & | GetOriginalValue () const
| Returns the backupped value. More...
template<typename U >
void | Change (const U &new_value)
| Change the value of the variable. More...
void | Revert ()
| Revert the variable to its original value, but do not mark it as restored.
void | Trash ()
| Trash the backup. More...
void | Restore ()
| Restore the variable.
void | Update ()
| Update the backup. More...
bool | Verify () const
| Check whether the variable is currently equals the backup. More...

## Private Attributes
T & | original
bool | valid
T | original_value
const char *const | file
const int | line

## Detailed Description

### template<typename T>
struct Backup< T >

Class to backup a specific variable and restore it later.

The variable is not restored automatically, but assertions make sure it is restored. You have to call either Trash() or Restore() exactly once.

Definition at line 21 of file backup_type.hpp.

## Constructor & Destructor Documentation

## ◆ Backup() [1/2]

template<typename T >

Backup< T >::Backup | ( | T & | original,
|  | const char *const | file,
|  | const int | line
| ) |  |
| inline

Backup variable.

Parameters

  original | Variable to backup.
  file | Filename for debug output. Use FILE_LINE macro.
  line | Linenumber for debug output. Use FILE_LINE macro.

Definition at line 28 of file backup_type.hpp.

## ◆ Backup() [2/2]

template<typename T >
template<typename U >

Backup< T >::Backup | ( | T & | original,
|  | const U & | new_value,
|  | const char *const | file,
|  | const int | line
| ) |  |
| inline

Backup variable and switch to new value.

Parameters

  original | Variable to backup.
  new_value | New value for variable.
  file | Filename for debug output. Use FILE_LINE macro.
  line | Linenumber for debug output. Use FILE_LINE macro.

Definition at line 38 of file backup_type.hpp.

## Member Function Documentation

## ◆ Change()

template<typename T >
template<typename U >

void Backup< T >::Change | ( | const U & | new_value | ) |
| inline

Change the value of the variable.

While this does not touch the backup at all, it ensures that the variable is only modified while backupped.

Parameters

  new_value | New value for variable.

Definition at line 84 of file backup_type.hpp.

## ◆ GetOriginalValue()

template<typename T >

const T& Backup< T >::GetOriginalValue | ( |  | ) | const
| inline

Returns the backupped value.

Returns
  value from the backup.

Definition at line 72 of file backup_type.hpp.

## ◆ IsValid()

template<typename T >

bool Backup< T >::IsValid | ( |  | ) | const
| inline

Checks whether the variable was already restored.

Returns
  true if variable has already been restored.

Definition at line 63 of file backup_type.hpp.

## ◆ Trash()

template<typename T >

void Backup< T >::Trash | ( |  | ) |
| inline

Trash the backup.

The variable shall not be restored anymore.

Definition at line 103 of file backup_type.hpp.

Referenced by CommandHelperBase::InternalExecuteProcessResult(), and Backup< T >::Restore().

## ◆ Update()

template<typename T >

void Backup< T >::Update | ( |  | ) |
| inline

Update the backup.

That is trash the old value and make the current value of the variable the value to be restored later.

Definition at line 122 of file backup_type.hpp.

## ◆ Verify()

template<typename T >

bool Backup< T >::Verify | ( |  | ) | const
| inline

Check whether the variable is currently equals the backup.

Returns
  true if equal

Definition at line 132 of file backup_type.hpp.

Referenced by CommandHelperBase::InternalExecuteProcessResult(), and CommandHelperBase::InternalExecuteValidateTestAndPrepExec().

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

- src/core/backup_type.hpp