Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #ifndef NEWGRF_STORAGE_H
00013 #define NEWGRF_STORAGE_H
00014
00015 #include "core/pool_type.hpp"
00016
00021 struct BaseStorageArray {
00022 virtual ~BaseStorageArray();
00023
00031 virtual void ClearChanges(bool keep_changes) = 0;
00032
00038 virtual void StoreValue(uint pos, int32 value) = 0;
00039 };
00040
00047 template <typename TYPE, uint SIZE>
00048 struct PersistentStorageArray : BaseStorageArray {
00049 TYPE storage[SIZE];
00050 TYPE *prev_storage;
00051
00053 PersistentStorageArray() : prev_storage(NULL)
00054 {
00055 memset(this->storage, 0, sizeof(this->storage));
00056 }
00057
00059 ~PersistentStorageArray()
00060 {
00061 free(this->prev_storage);
00062 }
00063
00065 void ResetToZero()
00066 {
00067 memset(this->storage, 0, sizeof(this->storage));
00068 }
00069
00077 void StoreValue(uint pos, int32 value)
00078 {
00079
00080 if (pos >= SIZE) return;
00081
00082
00083
00084 if (this->storage[pos] == value) return;
00085
00086
00087 if (this->prev_storage == NULL) {
00088 this->prev_storage = MallocT<TYPE>(SIZE);
00089 memcpy(this->prev_storage, this->storage, sizeof(this->storage));
00090
00091
00092
00093 AddChangedStorage(this);
00094 }
00095
00096 this->storage[pos] = value;
00097 }
00098
00104 TYPE GetValue(uint pos) const
00105 {
00106
00107 if (pos >= SIZE) return 0;
00108
00109 return this->storage[pos];
00110 }
00111
00116 void ClearChanges(bool keep_changes)
00117 {
00118 assert(this->prev_storage != NULL);
00119
00120 if (!keep_changes) {
00121 memcpy(this->storage, this->prev_storage, sizeof(this->storage));
00122 }
00123 free(this->prev_storage);
00124 this->prev_storage = NULL;
00125 }
00126 };
00127
00128
00135 template <typename TYPE, uint SIZE>
00136 struct TemporaryStorageArray : BaseStorageArray {
00137 TYPE storage[SIZE];
00138
00140 TemporaryStorageArray()
00141 {
00142 memset(this->storage, 0, sizeof(this->storage));
00143 }
00144
00150 void StoreValue(uint pos, int32 value)
00151 {
00152
00153 if (pos >= SIZE) return;
00154
00155 this->storage[pos] = value;
00156 AddChangedStorage(this);
00157 }
00158
00164 TYPE GetValue(uint pos) const
00165 {
00166
00167 if (pos >= SIZE) return 0;
00168
00169 return this->storage[pos];
00170 }
00171
00172 void ClearChanges(bool keep_changes)
00173 {
00174 memset(this->storage, 0, sizeof(this->storage));
00175 }
00176 };
00177
00178 void AddChangedStorage(BaseStorageArray *storage);
00179 void ClearStorageChanges(bool keep_changes);
00180
00181
00182 typedef PersistentStorageArray<int32, 16> OldPersistentStorage;
00183
00184 typedef uint32 PersistentStorageID;
00185
00186 struct PersistentStorage;
00187 typedef Pool<PersistentStorage, PersistentStorageID, 1, 0xFF000> PersistentStoragePool;
00188
00189 extern PersistentStoragePool _persistent_storage_pool;
00190
00195 struct PersistentStorage : PersistentStorageArray<int32, 16>, PersistentStoragePool::PoolItem<&_persistent_storage_pool> {
00196 uint32 grfid;
00197
00199 PersistentStorage(const uint32 new_grfid) : grfid(new_grfid)
00200 {
00201 this->prev_storage = NULL;
00202 memset(this->storage, 0, sizeof(this->storage));
00203 }
00204
00206 ~PersistentStorage()
00207 {
00208 free(this->prev_storage);
00209 }
00210 };
00211
00212 assert_compile(cpp_lengthof(OldPersistentStorage, storage) == cpp_lengthof(PersistentStorage, storage));
00213
00214 #define FOR_ALL_STORAGES_FROM(var, start) FOR_ALL_ITEMS_FROM(PersistentStorage, storage_index, var, start)
00215 #define FOR_ALL_STORAGES(var) FOR_ALL_STORAGES_FROM(var, 0)
00216
00217 #endif