10#ifndef NEWGRF_STORAGE_H
11#define NEWGRF_STORAGE_H
66template <
typename TYPE, u
int SIZE>
68 using StorageType = std::array<TYPE, SIZE>;
83 if (pos >= SIZE)
return;
87 if (this->storage[pos] == value)
return;
91 assert(!this->prev_storage);
92 }
else if (!this->prev_storage) {
93 this->prev_storage = std::make_unique<StorageType>(this->storage);
100 this->storage[pos] = value;
111 if (pos >= SIZE)
return 0;
113 return this->storage[pos];
118 if (this->prev_storage) {
119 this->storage = *this->prev_storage;
120 this->prev_storage.reset();
132template <
typename TYPE, u
int SIZE>
134 using StorageType = std::array<TYPE, SIZE>;
135 using StorageInitType = std::array<uint16_t, SIZE>;
149 if (pos >= SIZE)
return;
151 this->storage[pos] = value;
152 this->init[pos] = this->init_key;
163 if (pos >= SIZE)
return 0;
165 if (this->init[pos] != this->init_key) {
170 return this->storage[pos];
177 if (this->init_key == 0) {
194extern PersistentStoragePool _persistent_storage_pool;
199struct PersistentStorage :
PersistentStorageArray<int32_t, 256>, PersistentStoragePool::PoolItem<&_persistent_storage_pool> {
202 this->
grfid = new_grfid;
Base for the NewGRF implementation.
@ GSF_INVALID
An invalid spec feature.
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 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.
GrfSpecFeature 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.
void ClearChanges() override
Discard temporary changes.
std::unique_ptr< StorageType > prev_storage
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.
Templated helper to make a PoolID a single POD value.
const PersistentStorageID index
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.
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > > TileIndex
The index/ID of a Tile.
constexpr TileIndex INVALID_TILE
The very nice invalid tile marker.