OpenTTD Source: src/core/backup_type.hpp Source File

OpenTTD Source 13.2.1

backup_type.hpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef BACKUP_TYPE_HPP
11 #define BACKUP_TYPE_HPP
12
13 #include "../debug.h"
14
20 template <typename T>
21 struct Backup {
28 Backup(T &original, const char * const file, const int line) : original(original), valid(true), original_value(original), file(file), line(line) {}
29
37 template <typename U>
38 Backup(T &original, const U &new_value, const char * const file, const int line) : original(original), valid(true), original_value(original), file(file), line(line)
39 {
40 /* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
41 original = new_value;
42 }
43
47 ~Backup()
48 {
49 /* Check whether restoration was done */
50 if (this->valid)
51 {
52 /* We cannot assert here, as missing restoration is 'normal' when exceptions are thrown.
53 * Exceptions are especially used to abort world generation. */
54 Debug(misc, 0, "{}:{}: Backed-up value was not restored!", this->file, this->line);
55 this->Restore();
56 }
57 }
58
63 bool IsValid() const
64 {
65 return this->valid;
66 }
67
72 const T &GetOriginalValue() const
73 {
74 assert(this->valid);
75 return original_value;
76 }
77
83 template <typename U>
84 void Change(const U &new_value)
85 {
86 /* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
87 assert(this->valid);
88 original = new_value;
89 }
90
94 void Revert()
95 {
96 assert(this->valid);
97 this->original = this->original_value;
98 }
99
103 void Trash()
104 {
105 assert(this->valid);
106 this->valid = false;
107 }
108
112 void Restore()
113 {
114 this->Revert();
115 this->Trash();
116 }
117
122 void Update()
123 {
124 assert(this->valid);
125 this->original_value = this->original;
126 }
127
132 bool Verify() const
133 {
134 assert(this->valid);
135 return this->original_value == this->original;
136 }
137
138 private:
139 T &original;
140 bool valid;
141 T original_value;
142
143 const char * const file;
144 const int line;
145 };
146
147 #endif /* BACKUP_TYPE_HPP */
Backup::Change
void Change(const U &new_value)
Change the value of the variable.
Definition: backup_type.hpp:84
Backup
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
Backup::Verify
bool Verify() const
Check whether the variable is currently equals the backup.
Definition: backup_type.hpp:132
Backup::Backup
Backup(T &original, const char *const file, const int line)
Backup variable.
Definition: backup_type.hpp:28
Backup::Backup
Backup(T &original, const U &new_value, const char *const file, const int line)
Backup variable and switch to new value.
Definition: backup_type.hpp:38
Backup::~Backup
~Backup()
Check whether the variable was restored on object destruction.
Definition: backup_type.hpp:47
Backup::Update
void Update()
Update the backup.
Definition: backup_type.hpp:122
Backup::IsValid
bool IsValid() const
Checks whether the variable was already restored.
Definition: backup_type.hpp:63
Backup::Trash
void Trash()
Trash the backup.
Definition: backup_type.hpp:103
Backup::Restore
void Restore()
Restore the variable.
Definition: backup_type.hpp:112
Backup::GetOriginalValue
const T & GetOriginalValue() const
Returns the backupped value.
Definition: backup_type.hpp:72
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Backup::Revert
void Revert()
Revert the variable to its original value, but do not mark it as restored.
Definition: backup_type.hpp:94