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");
23 
24 
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 
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 }
Leave command test mode, revert to previous mode.
Enter command test mode, changes will be tempoary.
Functionality related to the temporary and persistent storage arrays for NewGRFs. ...
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
Functions related to debugging.
Function to handling different endian machines.
Some methods of Pool are placed here in order to reduce compilation time and binary size...
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...
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
Definition of base types and functions in a cross-platform compatible way.
Enter the gameloop, changes will be permanent.
A number of safeguards to prevent using unsafe methods.
Class for pooled persistent storage of data.
static std::set< BasePersistentStorageArray * > * _changed_storage_arrays
The changed storage arrays.
Base class for all persistent NewGRF storage arrays.
Enter command scope, changes will be permanent.
Leave command scope, revert to previous mode.
Base class for all pools.
Definition: pool_type.hpp:83
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:39
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
Leave the gameloop, changes will be temporary.
static uint32 BSWAP32(uint32 x)
Perform a 32 bits endianness bitswap on x.