OpenTTD Source 20250312-master-gcdcc6b491d
newgrf_storage.h
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 NEWGRF_STORAGE_H
11#define NEWGRF_STORAGE_H
12
13#include "core/pool_type.hpp"
14#include "tile_type.h"
15
27
33 uint32_t grfid = 0;
34 uint8_t feature = 0;
36
38
39 static void SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode = false);
40
41protected:
45 virtual void ClearChanges() = 0;
46
51 static bool AreChangesPersistent() { return (gameloop || command) && !testmode; }
52
53private:
54 static bool gameloop;
55 static bool command;
56 static bool testmode;
57};
58
65template <typename TYPE, uint SIZE>
67 using StorageType = std::array<TYPE, SIZE>;
68
69 StorageType storage{};
70 std::unique_ptr<StorageType> prev_storage{};
71
79 void StoreValue(uint pos, int32_t value)
80 {
81 /* Out of the scope of the array */
82 if (pos >= SIZE) return;
83
84 /* The value hasn't changed, so we pretend nothing happened.
85 * Saves a few cycles and such and it's pretty easy to check. */
86 if (this->storage[pos] == value) return;
87
88 /* We do not have made a backup; lets do so */
90 assert(!this->prev_storage);
91 } else if (!this->prev_storage) {
92 this->prev_storage = std::make_unique<StorageType>(this->storage);
93
94 /* We only need to register ourselves when we made the backup
95 * as that is the only time something will have changed */
97 }
98
99 this->storage[pos] = value;
100 }
101
107 TYPE GetValue(uint pos) const
108 {
109 /* Out of the scope of the array */
110 if (pos >= SIZE) return 0;
111
112 return this->storage[pos];
113 }
114
115 void ClearChanges() override
116 {
117 if (this->prev_storage) {
118 this->storage = *this->prev_storage;
119 this->prev_storage.reset();
120 }
121 }
122};
123
124
131template <typename TYPE, uint SIZE>
133 using StorageType = std::array<TYPE, SIZE>;
134 using StorageInitType = std::array<uint16_t, SIZE>;
135
136 StorageType storage{};
137 StorageInitType init{};
138 uint16_t init_key = 1;
139
145 void StoreValue(uint pos, int32_t value)
146 {
147 /* Out of the scope of the array */
148 if (pos >= SIZE) return;
149
150 this->storage[pos] = value;
151 this->init[pos] = this->init_key;
152 }
153
159 TYPE GetValue(uint pos) const
160 {
161 /* Out of the scope of the array */
162 if (pos >= SIZE) return 0;
163
164 if (this->init[pos] != this->init_key) {
165 /* Unassigned since last call to ClearChanges */
166 return 0;
167 }
168
169 return this->storage[pos];
170 }
171
172 void ClearChanges()
173 {
174 /* Increment init_key to invalidate all storage */
175 this->init_key++;
176 if (this->init_key == 0) {
177 /* When init_key wraps around, we need to reset everything */
178 this->init = {};
179 this->init_key = 1;
180 }
181 }
182};
183
185
187
189
190struct PersistentStorage;
192
193extern PersistentStoragePool _persistent_storage_pool;
194
198struct PersistentStorage : PersistentStorageArray<int32_t, 256>, PersistentStoragePool::PoolItem<&_persistent_storage_pool> {
200 PersistentStorage(const uint32_t new_grfid, uint8_t feature, TileIndex tile)
201 {
202 this->grfid = new_grfid;
203 this->feature = feature;
204 this->tile = tile;
205 }
206};
207
208static_assert(std::tuple_size_v<decltype(OldPersistentStorage::storage)> <= std::tuple_size_v<decltype(PersistentStorage::storage)>);
209
210#endif /* NEWGRF_STORAGE_H */
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
@ PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
@ PSM_LEAVE_TESTMODE
Leave command test mode, revert to previous mode.
@ PSM_LEAVE_COMMAND
Leave command scope, revert to previous mode.
@ PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
@ PSM_ENTER_COMMAND
Enter command scope, changes will be permanent.
@ PSM_ENTER_TESTMODE
Enter command test mode, changes will be temporary.
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
SwitchMode
Mode which defines what mode we're switching to.
Definition openttd.h:26
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
Base class for all persistent NewGRF storage arrays.
static bool AreChangesPersistent()
Check whether currently changes to the storage shall be persistent or temporary till the next call to...
virtual void ClearChanges()=0
Discard temporary changes.
uint8_t feature
NOSAVE: Used to identify in the owner of the array in debug output.
TileIndex tile
NOSAVE: Used to identify in the owner of the array in debug output.
uint32_t grfid
GRFID associated to this persistent storage. A value of zero means "default".
virtual ~BasePersistentStorageArray()
Remove references to use.
Class for persistent storage of data.
StorageType storage
Memory for the storage array.
void ClearChanges() override
Discard temporary changes.
std::unique_ptr< StorageType > prev_storage
Temporary memory to store previous state so it can be reverted, e.g. for command tests.
TYPE GetValue(uint pos) const
Gets the value from a given position.
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
Class for pooled persistent storage of data.
PersistentStorage(const uint32_t new_grfid, uint8_t feature, TileIndex tile)
We don't want GCC to zero our struct! It already is zeroed and has an index!
Templated helper to make a PoolID a single POD value.
Definition pool_type.hpp:43
Base class for all PoolItems.
Base class for all pools.
Class for temporary storage of data.
uint16_t init_key
Magic key to 'init'.
TYPE GetValue(uint pos) const
Gets the value from a given position.
void StoreValue(uint pos, int32_t value)
Stores some value at a given position.
StorageInitType init
Storage has been assigned, if this equals 'init_key'.
StorageType storage
Memory for the storage array.
Types related to tiles.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition tile_type.h:95