OpenTTD Source  20241124-master-g9399a92a4f
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 {
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  {
50  /* We cannot assert here, as missing restoration is 'normal' when exceptions are thrown.
51  * Exceptions are especially used to abort world generation. */
52  Debug(misc, 0, "{}:{}: Backed-up value was not restored!", this->location.file_name(), this->location.line());
53  this->Restore();
54  }
55  }
56 
61  bool IsValid() const
62  {
63  return this->valid;
64  }
65 
70  const T &GetOriginalValue() const
71  {
72  assert(this->valid);
73  return original_value;
74  }
75 
81  template <typename U>
82  void Change(const U &new_value)
83  {
84  /* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
85  assert(this->valid);
86  original = new_value;
87  }
88 
92  void Revert()
93  {
94  assert(this->valid);
95  this->original = this->original_value;
96  }
97 
101  void Trash()
102  {
103  assert(this->valid);
104  this->valid = false;
105  }
106 
110  void Restore()
111  {
112  this->Revert();
113  this->Trash();
114  }
115 
120  void Update()
121  {
122  assert(this->valid);
123  this->original_value = this->original;
124  }
125 
130  bool Verify() const
131  {
132  assert(this->valid);
133  return this->original_value == this->original;
134  }
135 
136 private:
137  T &original;
138  bool valid;
139  T original_value;
140 
141  const std::source_location location;
142 };
143 
149 template <typename T>
151  /*
152  * There is explicitly no only original constructor version, as that would make it possible
153  * for the new value to go out of scope before this object goes out of scope, thus defeating
154  * the whole goal and reason for existing of this object.
155  */
156 
162  AutoRestoreBackup(T &original, T new_value) : original(original), original_value(original)
163  {
164  original = new_value;
165  }
166 
171  {
172  this->original = this->original_value;
173  }
174 
175 private:
176  T &original;
177  T original_value;
178 
179  /* Prevent copy, assignment and allocation on stack. */
180  AutoRestoreBackup(const AutoRestoreBackup&) = delete;
181  AutoRestoreBackup& operator=(AutoRestoreBackup&) = delete;
182  static void *operator new(std::size_t) = delete;
183  static void *operator new[](std::size_t) = delete;
184 };
185 
186 #endif /* BACKUP_TYPE_HPP */
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
Class to backup a specific variable and restore it upon destruction of this object to prevent stack v...
~AutoRestoreBackup()
Restore the variable upon object destruction.
AutoRestoreBackup(T &original, T new_value)
Backup variable and switch to new value.
Class to backup a specific variable and restore it later.
Definition: backup_type.hpp:21
void Revert()
Revert the variable to its original value, but do not mark it as restored.
Definition: backup_type.hpp:92
const T & GetOriginalValue() const
Returns the backupped value.
Definition: backup_type.hpp:70
bool IsValid() const
Checks whether the variable was already restored.
Definition: backup_type.hpp:61
Backup(T &original, const std::source_location location=std::source_location::current())
Backup variable.
Definition: backup_type.hpp:27
void Trash()
Trash the backup.
~Backup()
Check whether the variable was restored on object destruction.
Definition: backup_type.hpp:45
bool Verify() const
Check whether the variable is currently equals the backup.
void Change(const U &new_value)
Change the value of the variable.
Definition: backup_type.hpp:82
void Update()
Update the backup.
Backup(T &original, const U &new_value, const std::source_location location=std::source_location::current())
Backup variable and switch to new value.
Definition: backup_type.hpp:36
void Restore()
Restore the variable.