OpenTTD Source  1.11.2
newgrf_storage.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "stdafx.h"
11 #include "newgrf_storage.h"
12 #include "core/pool_func.hpp"
13 #include "core/endian_func.hpp"
14 #include "debug.h"
15 #include <set>
16 
17 #include "safeguards.h"
18 
19 PersistentStoragePool _persistent_storage_pool("PersistentStorage");
21 
22 
24 
25 bool BasePersistentStorageArray::gameloop;
26 bool BasePersistentStorageArray::command;
27 bool BasePersistentStorageArray::testmode;
28 
33 {
34  _changed_storage_arrays->erase(this);
35 }
36 
44 {
45  _changed_storage_arrays->insert(storage);
46 }
47 
55 /* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode)
56 {
57  switch (mode) {
58  case PSM_ENTER_GAMELOOP:
59  assert(ignore_prev_mode || !gameloop);
60  assert(!command && !testmode);
61  gameloop = true;
62  break;
63 
64  case PSM_LEAVE_GAMELOOP:
65  assert(ignore_prev_mode || gameloop);
66  assert(!command && !testmode);
67  gameloop = false;
68  break;
69 
70  case PSM_ENTER_COMMAND:
71  assert((ignore_prev_mode || !command) && !testmode);
72  command = true;
73  break;
74 
75  case PSM_LEAVE_COMMAND:
76  assert(ignore_prev_mode || command);
77  command = false;
78  break;
79 
80  case PSM_ENTER_TESTMODE:
81  assert(!command && (ignore_prev_mode || !testmode));
82  testmode = true;
83  break;
84 
85  case PSM_LEAVE_TESTMODE:
86  assert(ignore_prev_mode || testmode);
87  testmode = false;
88  break;
89 
90  default: NOT_REACHED();
91  }
92 
93  /* Discard all temporary changes */
94  for (std::set<BasePersistentStorageArray*>::iterator it = _changed_storage_arrays->begin(); it != _changed_storage_arrays->end(); it++) {
95  DEBUG(desync, 1, "Discarding persistent storage changes: Feature %d, GrfID %08X, Tile %d", (*it)->feature, BSWAP32((*it)->grfid), (*it)->tile);
96  (*it)->ClearChanges();
97  }
98  _changed_storage_arrays->clear();
99 }
PSM_LEAVE_TESTMODE
@ PSM_LEAVE_TESTMODE
Leave command test mode, revert to previous mode.
Definition: newgrf_storage.h:25
endian_func.hpp
AddChangedPersistentStorage
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
Definition: newgrf_storage.cpp:43
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
PersistentStorage
Class for pooled persistent storage of data.
Definition: newgrf_storage.h:221
PSM_LEAVE_COMMAND
@ PSM_LEAVE_COMMAND
Leave command scope, revert to previous mode.
Definition: newgrf_storage.h:23
PSM_ENTER_GAMELOOP
@ PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
Definition: newgrf_storage.h:20
_changed_storage_arrays
static std::set< BasePersistentStorageArray * > * _changed_storage_arrays
The changed storage arrays.
Definition: newgrf_storage.cpp:23
safeguards.h
stdafx.h
BSWAP32
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.
Definition: bitmath_func.hpp:380
BasePersistentStorageArray
Base class for all persistent NewGRF storage arrays.
Definition: newgrf_storage.h:32
Pool
Base class for all pools.
Definition: pool_type.hpp:81
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition: pool_func.hpp:224
PSM_LEAVE_GAMELOOP
@ PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
Definition: newgrf_storage.h:21
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:55
PSM_ENTER_COMMAND
@ PSM_ENTER_COMMAND
Enter command scope, changes will be permanent.
Definition: newgrf_storage.h:22
pool_func.hpp
PSM_ENTER_TESTMODE
@ PSM_ENTER_TESTMODE
Enter command test mode, changes will be tempoary.
Definition: newgrf_storage.h:24
newgrf_storage.h
debug.h
PersistentStorageMode
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
Definition: newgrf_storage.h:19