OpenTTD Source 20260218-master-g2123fca5ea
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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef BACKUP_TYPE_HPP
11#define BACKUP_TYPE_HPP
12
13#include "../debug.h"
14
20template <typename T>
21struct Backup {
27 Backup(T &original, const std::source_location location = std::source_location::current()) : original(original), valid(true), original_value(original), location(location) {}
28
35 template <typename U>
36 Backup(T &original, const U &new_value, const std::source_location location = std::source_location::current()) : original(original), valid(true), original_value(original), location(location)
37 {
38 /* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
39 original = new_value;
40 }
41
46 {
47 /* Check whether restoration was done */
48 if (this->valid) {
49 /* We cannot assert here, as missing restoration is 'normal' when exceptions are thrown.
50 * Exceptions are especially used to abort world generation. */
51 Debug(misc, 0, "{}:{}: Backed-up value was not restored!", this->location.file_name(), this->location.line());
52 this->Restore();
53 }
54 }
55
60 bool IsValid() const
61 {
62 return this->valid;
63 }
64
69 const T &GetOriginalValue() const
70 {
71 assert(this->valid);
72 return this->original_value;
73 }
74
80 template <typename U>
81 void Change(const U &new_value)
82 {
83 /* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
84 assert(this->valid);
85 this->original = new_value;
86 }
87
91 void Revert()
92 {
93 assert(this->valid);
94 this->original = this->original_value;
95 }
96
100 void Trash()
101 {
102 assert(this->valid);
103 this->valid = false;
104 }
105
109 void Restore()
110 {
111 this->Revert();
112 this->Trash();
113 }
114
119 void Update()
120 {
121 assert(this->valid);
122 this->original_value = this->original;
123 }
124
129 bool Verify() const
130 {
131 assert(this->valid);
132 return this->original_value == this->original;
133 }
134
135private:
137 bool valid;
139
140 const std::source_location location;
141};
142
148template <typename T>
150 /*
151 * There is explicitly no only original constructor version, as that would make it possible
152 * for the new value to go out of scope before this object goes out of scope, thus defeating
153 * the whole goal and reason for existing of this object.
154 */
155
162 {
163 original = new_value;
164 }
165
170 {
171 this->original = this->original_value;
172 }
173
178 const T &GetOriginalValue() const
179 {
180 return this->original_value;
181 }
182
183private:
186
187 /* Prevent copy, assignment and allocation on stack. */
188 AutoRestoreBackup(const AutoRestoreBackup&) = delete;
189 AutoRestoreBackup& operator=(AutoRestoreBackup&) = delete;
190 static void *operator new(std::size_t) = delete;
191 static void *operator new[](std::size_t) = delete;
192};
193
194#endif /* BACKUP_TYPE_HPP */
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Definition debug.h:37
const T & GetOriginalValue() const
Returns the backupped value.
~AutoRestoreBackup()
Restore the variable upon object destruction.
T original_value
The value at the moment of making a backup.
AutoRestoreBackup(T &original, T new_value)
Backup variable and switch to new value.
T & original
Reference to the value we are backing up.
void Revert()
Revert the variable to its original value, but do not mark it as restored.
bool IsValid() const
Checks whether the variable was already restored.
Backup(T &original, const std::source_location location=std::source_location::current())
Backup variable.
void Trash()
Trash the backup.
~Backup()
Check whether the variable was restored on object destruction.
bool Verify() const
Check whether the variable is currently equals the backup.
T original_value
The value at the moment of making a backup.
void Change(const U &new_value)
Change the value of the variable.
bool valid
Whether the original value has been restored.
T & original
Reference to the value we are backing up.
void Update()
Update the backup.
const T & GetOriginalValue() const
Returns the backupped value.
Backup(T &original, const U &new_value, const std::source_location location=std::source_location::current())
Backup variable and switch to new value.
void Restore()
Restore the variable.
const std::source_location location
Call location where the backup was created.