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