Files
@ r27778:6b29a42dcb99
Branch filter:
Location: cpp/openttd-patchpack/source/src/core/backup_type.hpp - annotation
r27778:6b29a42dcb99
5.3 KiB
text/x-c++hdr
Codechange: Add a DateToYear function instead of dividing each time (#11173)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15282:8e9c51163c93 r15282:8e9c51163c93 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15282:8e9c51163c93 r15282:8e9c51163c93 r15269:8b901e9e0694 r15282:8e9c51163c93 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15282:8e9c51163c93 r15282:8e9c51163c93 r15269:8b901e9e0694 r15269:8b901e9e0694 r15282:8e9c51163c93 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15282:8e9c51163c93 r15282:8e9c51163c93 r15282:8e9c51163c93 r15282:8e9c51163c93 r25655:1030dcb7eb52 r15282:8e9c51163c93 r15282:8e9c51163c93 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15269:8b901e9e0694 r15282:8e9c51163c93 r15282:8e9c51163c93 r15282:8e9c51163c93 r15269:8b901e9e0694 r15269:8b901e9e0694 r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r26823:cb65f12c003b r15269:8b901e9e0694 | /*
* This file is part of OpenTTD.
* 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.
* 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.
* 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/>.
*/
/** @file backup_type.hpp Class for backupping variables and making sure they are restored later. */
#ifndef BACKUP_TYPE_HPP
#define BACKUP_TYPE_HPP
#include "../debug.h"
/**
* 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.
*/
template <typename T>
struct Backup {
/**
* Backup variable.
* @param original Variable to backup.
* @param file Filename for debug output. Use FILE_LINE macro.
* @param line Linenumber for debug output. Use FILE_LINE macro.
*/
Backup(T &original, const char * const file, const int line) : original(original), valid(true), original_value(original), file(file), line(line) {}
/**
* Backup variable and switch to new value.
* @param original Variable to backup.
* @param new_value New value for variable.
* @param file Filename for debug output. Use FILE_LINE macro.
* @param line Linenumber for debug output. Use FILE_LINE macro.
*/
template <typename U>
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)
{
/* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
original = new_value;
}
/**
* Check whether the variable was restored on object destruction.
*/
~Backup()
{
/* Check whether restoration was done */
if (this->valid)
{
/* We cannot assert here, as missing restoration is 'normal' when exceptions are thrown.
* Exceptions are especially used to abort world generation. */
Debug(misc, 0, "{}:{}: Backed-up value was not restored!", this->file, this->line);
this->Restore();
}
}
/**
* Checks whether the variable was already restored.
* @return true if variable has already been restored.
*/
bool IsValid() const
{
return this->valid;
}
/**
* Returns the backupped value.
* @return value from the backup.
*/
const T &GetOriginalValue() const
{
assert(this->valid);
return original_value;
}
/**
* 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.
* @param new_value New value for variable.
*/
template <typename U>
void Change(const U &new_value)
{
/* Note: We use a separate typename U, so type conversions are handled by assignment operator. */
assert(this->valid);
original = new_value;
}
/**
* Revert the variable to its original value, but do not mark it as restored.
*/
void Revert()
{
assert(this->valid);
this->original = this->original_value;
}
/**
* Trash the backup. The variable shall not be restored anymore.
*/
void Trash()
{
assert(this->valid);
this->valid = false;
}
/**
* Restore the variable.
*/
void Restore()
{
this->Revert();
this->Trash();
}
/**
* Update the backup.
* That is trash the old value and make the current value of the variable the value to be restored later.
*/
void Update()
{
assert(this->valid);
this->original_value = this->original;
}
/**
* Check whether the variable is currently equals the backup.
* @return true if equal
*/
bool Verify() const
{
assert(this->valid);
return this->original_value == this->original;
}
private:
T &original;
bool valid;
T original_value;
const char * const file;
const int line;
};
/**
* Class to backup a specific variable and restore it upon destruction of this object to prevent
* stack values going out of scope before resetting the global to its original value. Contrary to
* #Backup this restores the variable automatically and there is no manual option to restore.
*/
template <typename T>
struct AutoRestoreBackup {
/*
* There is explicitly no only original constructor version, as that would make it possible
* for the new value to go out of scope before this object goes out of scope, thus defeating
* the whole goal and reason for existing of this object.
*/
/**
* Backup variable and switch to new value.
* @param original Variable to backup.
* @param new_value New value for variable.
*/
AutoRestoreBackup(T &original, T new_value) : original(original), original_value(original)
{
original = new_value;
}
/**
* Restore the variable upon object destruction.
*/
~AutoRestoreBackup()
{
this->original = this->original_value;
}
private:
T &original;
T original_value;
/* Prevent copy, assignment and allocation on stack. */
AutoRestoreBackup(const AutoRestoreBackup&) = delete;
AutoRestoreBackup& operator=(AutoRestoreBackup&) = delete;
static void *operator new(std::size_t) = delete;
static void *operator new[](std::size_t) = delete;
};
#endif /* BACKUP_TYPE_HPP */
|