Go to the documentation of this file.
10 #ifndef NEWGRF_STORAGE_H
11 #define NEWGRF_STORAGE_H
65 template <
typename TYPE, u
int SIZE>
73 memset(this->storage, 0,
sizeof(this->storage));
79 free(this->prev_storage);
85 memset(this->storage, 0,
sizeof(this->storage));
98 if (pos >= SIZE)
return;
102 if (this->storage[pos] == value)
return;
106 assert(this->prev_storage ==
nullptr);
107 }
else if (this->prev_storage ==
nullptr) {
108 this->prev_storage = MallocT<TYPE>(SIZE);
109 memcpy(this->prev_storage, this->storage,
sizeof(this->storage));
116 this->storage[pos] = value;
127 if (pos >= SIZE)
return 0;
129 return this->storage[pos];
134 if (this->prev_storage !=
nullptr) {
135 memcpy(this->storage, this->prev_storage,
sizeof(this->storage));
136 free(this->prev_storage);
137 this->prev_storage =
nullptr;
149 template <
typename TYPE, u
int SIZE>
158 memset(this->storage, 0,
sizeof(this->storage));
159 memset(this->init, 0,
sizeof(this->init));
171 if (pos >= SIZE)
return;
173 this->storage[pos] = value;
185 if (pos >= SIZE)
return 0;
187 if (this->init[pos] != this->init_key) {
192 return this->storage[pos];
199 if (this->init_key == 0) {
201 memset(this->init, 0,
sizeof(this->init));
211 typedef uint32 PersistentStorageID;
225 this->
grfid = new_grfid;
@ PSM_LEAVE_TESTMODE
Leave command test mode, revert to previous mode.
static bool AreChangesPersistent()
Check whether currently changes to the storage shall be persistent or temporary till the next call to...
TYPE storage[SIZE]
Memory to for the storage array.
~PersistentStorageArray()
And free all data related to it.
virtual ~BasePersistentStorageArray()
Remove references to use.
TYPE GetValue(uint pos) const
Gets the value from a given position.
Class for pooled persistent storage of data.
@ PSM_LEAVE_COMMAND
Leave command scope, revert to previous mode.
@ PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
TYPE storage[SIZE]
Memory to for the storage array.
virtual void ClearChanges()=0
Discard temporary changes.
TileIndex tile
NOSAVE: Used to identify in the owner of the array in debug output.
byte feature
NOSAVE: Used to identify in the owner of the array in debug output.
Base class for all persistent NewGRF storage arrays.
void StoreValue(uint pos, int32 value)
Stores some value at a given position.
Base class for all pools.
void ResetToZero()
Resets all values to zero.
uint16 init[SIZE]
Storage has been assigned, if this equals 'init_key'.
void ClearChanges()
Discard temporary changes.
Class for temporary storage of data.
@ PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
static void SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode=false)
Clear temporary changes made since the last call to SwitchMode, and set whether subsequent changes sh...
uint16 init_key
Magic key to 'init'.
@ PSM_ENTER_COMMAND
Enter command scope, changes will be permanent.
uint32 grfid
GRFID associated to this persistent storage. A value of zero means "default".
TemporaryStorageArray()
Simply construct the array.
TYPE * prev_storage
Memory to store "old" states so we can revert them on the performance of test cases for commands etc.
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
PersistentStorageArray()
Simply construct the array.
@ PSM_ENTER_TESTMODE
Enter command test mode, changes will be temporary.
Base class for all PoolItems.
#define cpp_lengthof(base, variable)
Gets the length of an array variable within a class.
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
TYPE GetValue(uint pos) const
Gets the value from a given position.
Class for persistent storage of data.
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
PersistentStorage(const uint32 new_grfid, byte feature, TileIndex tile)
We don't want GCC to zero our struct! It already is zeroed and has an index!