Merge branch 'master' into jgrpp
# Conflicts: # cmake/SourceList.cmake # src/build_vehicle_gui.cpp # src/company_gui.cpp # src/console_cmds.cpp # src/depot_base.h # src/elrail.cpp # src/network/core/udp.cpp # src/network/network_admin.cpp # src/network/network_chat_gui.cpp # src/network/network_gui.cpp # src/network/network_server.cpp # src/newgrf.cpp # src/newgrf_engine.cpp # src/newgrf_railtype.cpp # src/newgrf_railtype.h # src/newgrf_storage.h # src/os/unix/crashlog_unix.cpp # src/rail.h # src/rail_cmd.cpp # src/rail_gui.cpp # src/road_cmd.cpp # src/road_map.h # src/saveload/labelmaps_sl.cpp # src/settings_gui.cpp # src/settings_type.h # src/sl/oldloader_sl.cpp # src/station_cmd.cpp # src/station_gui.cpp # src/table/settings/world_settings.ini # src/tests/test_script_admin.cpp # src/textfile_gui.cpp # src/toolbar_gui.cpp # src/train_cmd.cpp # src/tunnelbridge_cmd.cpp # src/vehicle_gui.cpp # src/widget.cpp # src/window.cpp # src/window_gui.h # src/window_type.h
This commit is contained in:
@@ -13,6 +13,7 @@
|
||||
#include "core/alloc_func.hpp"
|
||||
#include "core/pool_type.hpp"
|
||||
#include "tile_type.h"
|
||||
#include <array>
|
||||
|
||||
/**
|
||||
* Mode switches to the behaviour of persistent storage array.
|
||||
@@ -65,26 +66,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 +89,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 +116,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();
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -149,17 +132,12 @@ struct PersistentStorageArray : BasePersistentStorageArray {
|
||||
*/
|
||||
template <typename TYPE, uint SIZE>
|
||||
struct TemporaryStorageArray {
|
||||
TYPE storage[SIZE]; ///< Memory to for the storage array
|
||||
uint16 init[SIZE]; ///< Storage has been assigned, if this equals 'init_key'.
|
||||
uint16 init_key; ///< Magic key to 'init'.
|
||||
using StorageType = std::array<TYPE, SIZE>;
|
||||
using StorageInitType = std::array<uint16_t, SIZE>;
|
||||
|
||||
/** Simply construct the array */
|
||||
TemporaryStorageArray()
|
||||
{
|
||||
memset(this->storage, 0, sizeof(this->storage)); // not exactly needed, but makes code analysers happy
|
||||
memset(this->init, 0, sizeof(this->init));
|
||||
this->init_key = 1;
|
||||
}
|
||||
StorageType storage{}; ///< Memory for the storage array
|
||||
StorageInitType init{}; ///< Storage has been assigned, if this equals 'init_key'.
|
||||
uint16_t init_key{1}; ///< Magic key to 'init'.
|
||||
|
||||
/**
|
||||
* Stores some value at a given position.
|
||||
@@ -199,7 +177,7 @@ struct TemporaryStorageArray {
|
||||
this->init_key++;
|
||||
if (this->init_key == 0) {
|
||||
/* When init_key wraps around, we need to reset everything */
|
||||
memset(this->init, 0, sizeof(this->init));
|
||||
this->init = {};
|
||||
this->init_key = 1;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user