mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-12 10:30:28 +00:00
Codechange: Use std::array and std::unique_ptr for PersistentStorageArrays.
This (mostly) avoids the need for manual memory management and copying.
This commit is contained in:
parent
c3918838f6
commit
9040d7813d
@ -65,26 +65,10 @@ private:
|
||||
*/
|
||||
template <typename TYPE, uint SIZE>
|
||||
struct PersistentStorageArray : BasePersistentStorageArray {
|
||||
TYPE storage[SIZE]; ///< Memory to for the storage array
|
||||
TYPE *prev_storage; ///< Memory to store "old" states so we can revert them on the performance of test cases for commands etc.
|
||||
using StorageType = std::array<TYPE, SIZE>;
|
||||
|
||||
/** Simply construct the array */
|
||||
PersistentStorageArray() : prev_storage(nullptr)
|
||||
{
|
||||
memset(this->storage, 0, sizeof(this->storage));
|
||||
}
|
||||
|
||||
/** And free all data related to it */
|
||||
~PersistentStorageArray()
|
||||
{
|
||||
free(this->prev_storage);
|
||||
}
|
||||
|
||||
/** Resets all values to zero. */
|
||||
void ResetToZero()
|
||||
{
|
||||
memset(this->storage, 0, sizeof(this->storage));
|
||||
}
|
||||
StorageType storage{}; ///< Memory for the storage array
|
||||
std::unique_ptr<StorageType> prev_storage{}; ///< Temporary memory to store previous state so it can be reverted, e.g. for command tests.
|
||||
|
||||
/**
|
||||
* Stores some value at a given position.
|
||||
@ -104,10 +88,9 @@ struct PersistentStorageArray : BasePersistentStorageArray {
|
||||
|
||||
/* We do not have made a backup; lets do so */
|
||||
if (AreChangesPersistent()) {
|
||||
assert(this->prev_storage == nullptr);
|
||||
} else if (this->prev_storage == nullptr) {
|
||||
this->prev_storage = MallocT<TYPE>(SIZE);
|
||||
memcpy(this->prev_storage, this->storage, sizeof(this->storage));
|
||||
assert(!this->prev_storage);
|
||||
} else if (!this->prev_storage) {
|
||||
this->prev_storage = std::make_unique<StorageType>(this->storage);
|
||||
|
||||
/* We only need to register ourselves when we made the backup
|
||||
* as that is the only time something will have changed */
|
||||
@ -132,10 +115,9 @@ struct PersistentStorageArray : BasePersistentStorageArray {
|
||||
|
||||
void ClearChanges()
|
||||
{
|
||||
if (this->prev_storage != nullptr) {
|
||||
memcpy(this->storage, this->prev_storage, sizeof(this->storage));
|
||||
free(this->prev_storage);
|
||||
this->prev_storage = nullptr;
|
||||
if (this->prev_storage) {
|
||||
this->storage = *this->prev_storage;
|
||||
this->prev_storage.reset();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -246,7 +246,7 @@ struct INDYChunkHandler : ChunkHandler {
|
||||
/* Store the old persistent storage. The GRFID will be added later. */
|
||||
assert(PersistentStorage::CanAllocateItem());
|
||||
i->psa = new PersistentStorage(0, 0, 0);
|
||||
memcpy(i->psa->storage, _old_ind_persistent_storage.storage, sizeof(_old_ind_persistent_storage.storage));
|
||||
std::copy(std::begin(_old_ind_persistent_storage.storage), std::end(_old_ind_persistent_storage.storage), std::begin(i->psa->storage));
|
||||
}
|
||||
if (IsSavegameVersionBefore(SLV_INDUSTRY_CARGO_REORGANISE)) LoadMoveAcceptsProduced(i);
|
||||
Industry::IncIndustryTypeCount(i->type);
|
||||
|
@ -426,7 +426,7 @@ public:
|
||||
/* Store the old persistent storage. The GRFID will be added later. */
|
||||
assert(PersistentStorage::CanAllocateItem());
|
||||
st->airport.psa = new PersistentStorage(0, 0, 0);
|
||||
memcpy(st->airport.psa->storage, _old_st_persistent_storage.storage, sizeof(_old_st_persistent_storage.storage));
|
||||
std::copy(std::begin(_old_st_persistent_storage.storage), std::end(_old_st_persistent_storage.storage), std::begin(st->airport.psa->storage));
|
||||
}
|
||||
|
||||
size_t num_cargo = this->GetNumCargo();
|
||||
|
Loading…
Reference in New Issue
Block a user