2007-09-22 12:59:43 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
/** @file newgrf_storage.h Functionality related to the temporary and persistent storage arrays for NewGRFs. */
|
|
|
|
|
|
|
|
#ifndef NEWGRF_STORAGE_H
|
|
|
|
#define NEWGRF_STORAGE_H
|
|
|
|
|
2011-06-12 20:47:45 +00:00
|
|
|
#include "core/pool_type.hpp"
|
2007-12-25 09:48:53 +00:00
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
/**
|
|
|
|
* Base class for all NewGRF storage arrays. Nothing fancy, only here
|
|
|
|
* so we have a generalised class to use.
|
|
|
|
*/
|
2012-01-01 17:22:32 +00:00
|
|
|
struct BaseStorageArray {
|
2011-06-11 20:40:46 +00:00
|
|
|
virtual ~BaseStorageArray();
|
2007-09-22 12:59:43 +00:00
|
|
|
|
|
|
|
/**
|
2012-01-01 17:22:32 +00:00
|
|
|
* Clear the changes made since the last #ClearChanges.
|
2007-09-22 12:59:43 +00:00
|
|
|
* This can be done in two ways:
|
|
|
|
* - saving the changes permanently
|
|
|
|
* - reverting to the previous version
|
2012-01-01 17:22:32 +00:00
|
|
|
* @param keep_changes do we save or revert the changes since the last #ClearChanges?
|
2007-09-22 12:59:43 +00:00
|
|
|
*/
|
2007-09-22 13:56:38 +00:00
|
|
|
virtual void ClearChanges(bool keep_changes) = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores some value at a given position.
|
|
|
|
* @param pos the position to write at
|
|
|
|
* @param value the value to write
|
|
|
|
*/
|
2011-06-12 20:40:21 +00:00
|
|
|
virtual void StoreValue(uint pos, int32 value) = 0;
|
2007-09-22 12:59:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for persistent storage of data.
|
2012-01-01 17:22:32 +00:00
|
|
|
* On #ClearChanges that data is either reverted or saved.
|
|
|
|
* @tparam TYPE the type of variable to store.
|
|
|
|
* @tparam SIZE the size of the array.
|
2007-09-22 12:59:43 +00:00
|
|
|
*/
|
|
|
|
template <typename TYPE, uint SIZE>
|
|
|
|
struct PersistentStorageArray : BaseStorageArray {
|
|
|
|
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.
|
|
|
|
|
|
|
|
/** Simply construct the array */
|
|
|
|
PersistentStorageArray() : prev_storage(NULL)
|
|
|
|
{
|
|
|
|
memset(this->storage, 0, sizeof(this->storage));
|
|
|
|
}
|
|
|
|
|
|
|
|
/** And free all data related to it */
|
|
|
|
~PersistentStorageArray()
|
|
|
|
{
|
|
|
|
free(this->prev_storage);
|
|
|
|
}
|
|
|
|
|
2011-06-11 20:47:31 +00:00
|
|
|
/** Resets all values to zero. */
|
|
|
|
void ResetToZero()
|
|
|
|
{
|
|
|
|
memset(this->storage, 0, sizeof(this->storage));
|
|
|
|
}
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
/**
|
|
|
|
* Stores some value at a given position.
|
|
|
|
* If there is no backup of the data that backup is made and then
|
|
|
|
* we write the data.
|
|
|
|
* @param pos the position to write at
|
|
|
|
* @param value the value to write
|
|
|
|
*/
|
2011-06-12 20:40:21 +00:00
|
|
|
void StoreValue(uint pos, int32 value)
|
2007-09-22 12:59:43 +00:00
|
|
|
{
|
|
|
|
/* Out of the scope of the array */
|
|
|
|
if (pos >= SIZE) return;
|
|
|
|
|
|
|
|
/* The value hasn't changed, so we pretend nothing happened.
|
|
|
|
* Saves a few cycles and such and it's pretty easy to check. */
|
|
|
|
if (this->storage[pos] == value) return;
|
|
|
|
|
|
|
|
/* We do not have made a backup; lets do so */
|
2013-11-08 22:28:57 +00:00
|
|
|
if (this->prev_storage == NULL) {
|
2007-09-22 12:59:43 +00:00
|
|
|
this->prev_storage = MallocT<TYPE>(SIZE);
|
|
|
|
memcpy(this->prev_storage, this->storage, sizeof(this->storage));
|
|
|
|
|
|
|
|
/* We only need to register ourselves when we made the backup
|
|
|
|
* as that is the only time something will have changed */
|
|
|
|
AddChangedStorage(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
this->storage[pos] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value from a given position.
|
|
|
|
* @param pos the position to get the data from
|
|
|
|
* @return the data from that position
|
|
|
|
*/
|
2011-06-12 20:40:21 +00:00
|
|
|
TYPE GetValue(uint pos) const
|
2007-09-22 12:59:43 +00:00
|
|
|
{
|
|
|
|
/* Out of the scope of the array */
|
|
|
|
if (pos >= SIZE) return 0;
|
|
|
|
|
|
|
|
return this->storage[pos];
|
|
|
|
}
|
|
|
|
|
2011-01-22 09:53:15 +00:00
|
|
|
/**
|
|
|
|
* Clear the changes, or assign them permanently to the storage.
|
|
|
|
* @param keep_changes Whether to assign or ditch the changes.
|
|
|
|
*/
|
2007-09-22 12:59:43 +00:00
|
|
|
void ClearChanges(bool keep_changes)
|
|
|
|
{
|
|
|
|
assert(this->prev_storage != NULL);
|
|
|
|
|
|
|
|
if (!keep_changes) {
|
|
|
|
memcpy(this->storage, this->prev_storage, sizeof(this->storage));
|
|
|
|
}
|
|
|
|
free(this->prev_storage);
|
2013-11-08 22:28:57 +00:00
|
|
|
this->prev_storage = NULL;
|
2007-09-22 12:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for temporary storage of data.
|
2012-01-01 17:22:32 +00:00
|
|
|
* On #ClearChanges that data is always zero-ed.
|
|
|
|
* @tparam TYPE the type of variable to store.
|
|
|
|
* @tparam SIZE the size of the array.
|
2007-09-22 12:59:43 +00:00
|
|
|
*/
|
|
|
|
template <typename TYPE, uint SIZE>
|
|
|
|
struct TemporaryStorageArray : BaseStorageArray {
|
|
|
|
TYPE storage[SIZE]; ///< Memory to for the storage array
|
2013-12-23 18:07:57 +00:00
|
|
|
uint16 init[SIZE]; ///< Storage has been assigned, if this equals 'init_key'.
|
|
|
|
uint16 init_key; ///< Magic key to 'init'.
|
2007-09-22 12:59:43 +00:00
|
|
|
|
|
|
|
/** Simply construct the array */
|
|
|
|
TemporaryStorageArray()
|
|
|
|
{
|
2013-12-23 18:07:57 +00:00
|
|
|
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;
|
2007-09-22 12:59:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stores some value at a given position.
|
|
|
|
* @param pos the position to write at
|
|
|
|
* @param value the value to write
|
|
|
|
*/
|
2011-06-12 20:40:21 +00:00
|
|
|
void StoreValue(uint pos, int32 value)
|
2007-09-22 12:59:43 +00:00
|
|
|
{
|
|
|
|
/* Out of the scope of the array */
|
|
|
|
if (pos >= SIZE) return;
|
|
|
|
|
|
|
|
this->storage[pos] = value;
|
2013-12-23 18:07:57 +00:00
|
|
|
this->init[pos] = this->init_key;
|
2007-09-22 12:59:43 +00:00
|
|
|
AddChangedStorage(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the value from a given position.
|
|
|
|
* @param pos the position to get the data from
|
|
|
|
* @return the data from that position
|
|
|
|
*/
|
2011-06-12 20:40:21 +00:00
|
|
|
TYPE GetValue(uint pos) const
|
2007-09-22 12:59:43 +00:00
|
|
|
{
|
|
|
|
/* Out of the scope of the array */
|
|
|
|
if (pos >= SIZE) return 0;
|
|
|
|
|
2013-12-23 18:07:57 +00:00
|
|
|
if (this->init[pos] != this->init_key) {
|
|
|
|
/* Unassigned since last call to ClearChanges */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
return this->storage[pos];
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearChanges(bool keep_changes)
|
|
|
|
{
|
2013-12-23 18:07:57 +00:00
|
|
|
/* Increment init_key to invalidate all storage */
|
|
|
|
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_key = 1;
|
|
|
|
}
|
2007-09-22 12:59:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void AddChangedStorage(BaseStorageArray *storage);
|
|
|
|
void ClearStorageChanges(bool keep_changes);
|
|
|
|
|
2011-06-12 20:47:45 +00:00
|
|
|
|
|
|
|
typedef PersistentStorageArray<int32, 16> OldPersistentStorage;
|
|
|
|
|
|
|
|
typedef uint32 PersistentStorageID;
|
|
|
|
|
|
|
|
struct PersistentStorage;
|
|
|
|
typedef Pool<PersistentStorage, PersistentStorageID, 1, 0xFF000> PersistentStoragePool;
|
|
|
|
|
|
|
|
extern PersistentStoragePool _persistent_storage_pool;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for pooled persistent storage of data.
|
2012-01-01 17:22:32 +00:00
|
|
|
* On #ClearChanges that data is always zero-ed.
|
2011-06-12 20:47:45 +00:00
|
|
|
*/
|
|
|
|
struct PersistentStorage : PersistentStorageArray<int32, 16>, PersistentStoragePool::PoolItem<&_persistent_storage_pool> {
|
|
|
|
uint32 grfid; ///< GRFID associated to this persistent storage. A value of zero means "default".
|
|
|
|
|
|
|
|
/** We don't want GCC to zero our struct! It already is zeroed and has an index! */
|
|
|
|
PersistentStorage(const uint32 new_grfid) : grfid(new_grfid)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_compile(cpp_lengthof(OldPersistentStorage, storage) == cpp_lengthof(PersistentStorage, storage));
|
|
|
|
|
|
|
|
#define FOR_ALL_STORAGES_FROM(var, start) FOR_ALL_ITEMS_FROM(PersistentStorage, storage_index, var, start)
|
|
|
|
#define FOR_ALL_STORAGES(var) FOR_ALL_STORAGES_FROM(var, 0)
|
|
|
|
|
2007-09-22 12:59:43 +00:00
|
|
|
#endif /* NEWGRF_STORAGE_H */
|