OpenTTD

newgrf_storage.cpp
Go to the documentation of this file.
1 /* $Id: newgrf_storage.cpp 26482 2014-04-23 20:13:33Z rubidium $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #include "stdafx.h"
13 #include "newgrf_storage.h"
14 #include "core/pool_func.hpp"
15 #include "core/endian_func.hpp"
16 #include "debug.h"
17 #include <set>
18
19 #include "safeguards.h"
20
21 PersistentStoragePool _persistent_storage_pool("PersistentStorage");
22 INSTANTIATE_POOL_METHODS(PersistentStorage)
23
24
25 static std::set<BasePersistentStorageArray*> *_changed_storage_arrays = new std::set<BasePersistentStorageArray*>;
26
27 bool BasePersistentStorageArray::gameloop;
28 bool BasePersistentStorageArray::command;
29 bool BasePersistentStorageArray::testmode;
30
34 BasePersistentStorageArray::~BasePersistentStorageArray()
35 {
36 _changed_storage_arrays->erase(this);
37 }
38
45 void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
46 {
47 _changed_storage_arrays->insert(storage);
48 }
49
57 /* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode)
58 {
59 switch (mode) {
60 case PSM_ENTER_GAMELOOP:
61 assert(ignore_prev_mode || !gameloop);
62 assert(!command && !testmode);
63 gameloop = true;
64 break;
65
66 case PSM_LEAVE_GAMELOOP:
67 assert(ignore_prev_mode || gameloop);
68 assert(!command && !testmode);
69 gameloop = false;
70 break;
71
72 case PSM_ENTER_COMMAND:
73 assert((ignore_prev_mode || !command) && !testmode);
74 command = true;
75 break;
76
77 case PSM_LEAVE_COMMAND:
78 assert(ignore_prev_mode || command);
79 command = false;
80 break;
81
82 case PSM_ENTER_TESTMODE:
83 assert(!command && (ignore_prev_mode || !testmode));
84 testmode = true;
85 break;
86
87 case PSM_LEAVE_TESTMODE:
88 assert(ignore_prev_mode || testmode);
89 testmode = false;
90 break;
91
92 default: NOT_REACHED();
93 }
94
95 /* Discard all temporary changes */
96 for (std::set<BasePersistentStorageArray*>::iterator it = _changed_storage_arrays->begin(); it != _changed_storage_arrays->end(); it++) {
97 DEBUG(desync, 1, "Discarding persistent storage changes: Feature %d, GrfID %08X, Tile %d", (*it)->feature, BSWAP32((*it)->grfid), (*it)->tile);
98 (*it)->ClearChanges();
99 }
100 _changed_storage_arrays->clear();
101 }
PSM_LEAVE_TESTMODE
Leave command test mode, revert to previous mode.
Definition: newgrf_storage.h:27
PSM_ENTER_TESTMODE
Enter command test mode, changes will be tempoary.
Definition: newgrf_storage.h:26
newgrf_storage.h
Functionality related to the temporary and persistent storage arrays for NewGRFs. ...
AddChangedPersistentStorage
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
Definition: newgrf_storage.cpp:45
debug.h
Functions related to debugging.
endian_func.hpp
Function to handling different endian machines.
pool_func.hpp
Some methods of Pool are placed here in order to reduce compilation time and binary size...
BasePersistentStorageArray::SwitchMode
static void SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode=false)
Clear temporary changes made since the last call to SwitchMode, and set whether subsequent changes sh...
Definition: newgrf_storage.cpp:57
PersistentStorageMode
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
Definition: newgrf_storage.h:21
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
Definition: newgrf_storage.h:22
safeguards.h
A number of safeguards to prevent using unsafe methods.
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:223
_changed_storage_arrays
static std::set< BasePersistentStorageArray * > * _changed_storage_arrays
The changed storage arrays.
Definition: newgrf_storage.cpp:25
BasePersistentStorageArray
Base class for all persistent NewGRF storage arrays.
Definition: newgrf_storage.h:34
PSM_ENTER_COMMAND
Enter command scope, changes will be permanent.
Definition: newgrf_storage.h:24
PSM_LEAVE_COMMAND
Leave command scope, revert to previous mode.
Definition: newgrf_storage.h:25
Pool
Base class for all pools.
Definition: pool_type.hpp:83
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
Definition: newgrf_storage.h:23
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:380