# newgrf_storage.h

Go to the documentation of this file.

    00001 /* $Id: newgrf_storage.h 25991 2013-11-13 22:00:46Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    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     /* Out of the scope of the array */
    00080     if (pos >= SIZE) return;
    00081
    00082     /* The value hasn't changed, so we pretend nothing happened.
    00083      * Saves a few cycles and such and it's pretty easy to check. */
    00084     if (this->storage[pos] == value) return;
    00085
    00086     /* We do not have made a backup; lets do so */
    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       /* We only need to register ourselves when we made the backup
    00092        * as that is the only time something will have changed */
    00093       AddChangedStorage(this);
    00094     }
    00095
    00096     this->storage[pos] = value;
    00097   }
    00098
    00104   TYPE GetValue(uint pos) const
    00105   {
    00106     /* Out of the scope of the array */
    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     /* Out of the scope of the array */
    00153     if (pos >= SIZE) return;
    00154
    00155     this->storage[pos] = value;
    00156     AddChangedStorage(this);
    00157   }
    00158
    00164   TYPE GetValue(uint pos) const
    00165   {
    00166     /* Out of the scope of the array */
    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 /* NEWGRF_STORAGE_H */