OpenTTD Source: src/newgrf_storage.cpp Source File

OpenTTD Source 15.0-beta1

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
16#include "safeguards.h"
17
18 PersistentStoragePool _persistent_storage_pool("PersistentStorage");
19 INSTANTIATE_POOL_METHODS(PersistentStorage)
20
21
22static std::set<BasePersistentStorageArray*> *_changed_storage_arrays = new std::set<BasePersistentStorageArray*>;
23
24bool BasePersistentStorageArray::gameloop;
25bool BasePersistentStorageArray::command;
26bool BasePersistentStorageArray::testmode;
27
31 BasePersistentStorageArray::~BasePersistentStorageArray()
32{
33 _changed_storage_arrays->erase(this);
34}
35
42 void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
43{
44 _changed_storage_arrays->insert(storage);
45}
46
54/* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, [[maybe_unused]] bool ignore_prev_mode)
55{
56 switch (mode) {
57 case PSM_ENTER_GAMELOOP:
58 assert(ignore_prev_mode || !gameloop);
59 assert(!command && !testmode);
60 gameloop = true;
61 break;
62
63 case PSM_LEAVE_GAMELOOP:
64 assert(ignore_prev_mode || gameloop);
65 assert(!command && !testmode);
66 gameloop = false;
67 break;
68
69 case PSM_ENTER_COMMAND:
70 assert((ignore_prev_mode || !command) && !testmode);
71 command = true;
72 break;
73
74 case PSM_LEAVE_COMMAND:
75 assert(ignore_prev_mode || command);
76 command = false;
77 break;
78
79 case PSM_ENTER_TESTMODE:
80 assert(!command && (ignore_prev_mode || !testmode));
81 testmode = true;
82 break;
83
84 case PSM_LEAVE_TESTMODE:
85 assert(ignore_prev_mode || testmode);
86 testmode = false;
87 break;
88
89 default: NOT_REACHED();
90 }
91
92 /* Discard all temporary changes */
93 for (auto &it : *_changed_storage_arrays) {
94 Debug(desync, 2, "warning: discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", it->feature, BSWAP32(it->grfid), it->tile);
95 it->ClearChanges();
96 }
97 _changed_storage_arrays->clear();
98}
BSWAP32
static uint32_t BSWAP32(uint32_t x)
Perform a 32 bits endianness bitswap on x.
Definition bitmath_func.hpp:364
debug.h
Functions related to debugging.
Debug
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition debug.h:37
endian_func.hpp
Function to handling different endian machines.
AddChangedPersistentStorage
void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
Add the changed storage array to the list of changed arrays.
Definition newgrf_storage.cpp:42
_changed_storage_arrays
static std::set< BasePersistentStorageArray * > * _changed_storage_arrays
The changed storage arrays.
Definition newgrf_storage.cpp:22
newgrf_storage.h
Functionality related to the temporary and persistent storage arrays for NewGRFs.
PersistentStorageMode
PersistentStorageMode
Mode switches to the behaviour of persistent storage array.
Definition newgrf_storage.h:20
PSM_ENTER_GAMELOOP
@ PSM_ENTER_GAMELOOP
Enter the gameloop, changes will be permanent.
Definition newgrf_storage.h:21
PSM_LEAVE_TESTMODE
@ PSM_LEAVE_TESTMODE
Leave command test mode, revert to previous mode.
Definition newgrf_storage.h:26
PSM_LEAVE_COMMAND
@ PSM_LEAVE_COMMAND
Leave command scope, revert to previous mode.
Definition newgrf_storage.h:24
PSM_LEAVE_GAMELOOP
@ PSM_LEAVE_GAMELOOP
Leave the gameloop, changes will be temporary.
Definition newgrf_storage.h:22
PSM_ENTER_COMMAND
@ PSM_ENTER_COMMAND
Enter command scope, changes will be permanent.
Definition newgrf_storage.h:23
PSM_ENTER_TESTMODE
@ PSM_ENTER_TESTMODE
Enter command test mode, changes will be temporary.
Definition newgrf_storage.h:25
pool_func.hpp
Some methods of Pool are placed here in order to reduce compilation time and binary size.
INSTANTIATE_POOL_METHODS
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
Definition pool_func.hpp:237
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
BasePersistentStorageArray
Base class for all persistent NewGRF storage arrays.
Definition newgrf_storage.h:33
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:54
PersistentStorage
Class for pooled persistent storage of data.
Definition newgrf_storage.h:199
Pool
Base class for all pools.
Definition pool_type.hpp:80