newgrf_storage.cpp

Go to the documentation of this file.
00001 /* $Id: newgrf_storage.cpp 26371 2014-02-23 22:03:08Z frosch $ */
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 #include "stdafx.h"
00013 #include "newgrf_storage.h"
00014 #include "core/pool_func.hpp"
00015 #include "core/endian_func.hpp"
00016 #include "debug.h"
00017 #include <set>
00018 
00019 PersistentStoragePool _persistent_storage_pool("PersistentStorage");
00020 INSTANTIATE_POOL_METHODS(PersistentStorage)
00021 
00022 
00023 static std::set<BasePersistentStorageArray*> *_changed_storage_arrays = new std::set<BasePersistentStorageArray*>;
00024 
00025 bool BasePersistentStorageArray::gameloop;
00026 bool BasePersistentStorageArray::command;
00027 bool BasePersistentStorageArray::testmode;
00028 
00032 BasePersistentStorageArray::~BasePersistentStorageArray()
00033 {
00034   _changed_storage_arrays->erase(this);
00035 }
00036 
00043 void AddChangedPersistentStorage(BasePersistentStorageArray *storage)
00044 {
00045   _changed_storage_arrays->insert(storage);
00046 }
00047 
00055 /* static */ void BasePersistentStorageArray::SwitchMode(PersistentStorageMode mode, bool ignore_prev_mode)
00056 {
00057   switch (mode) {
00058     case PSM_ENTER_GAMELOOP:
00059       assert(ignore_prev_mode || !gameloop);
00060       assert(!command && !testmode);
00061       gameloop = true;
00062       break;
00063 
00064     case PSM_LEAVE_GAMELOOP:
00065       assert(ignore_prev_mode || gameloop);
00066       assert(!command && !testmode);
00067       gameloop = false;
00068       break;
00069 
00070     case PSM_ENTER_COMMAND:
00071       assert((ignore_prev_mode || !command) && !testmode);
00072       command = true;
00073       break;
00074 
00075     case PSM_LEAVE_COMMAND:
00076       assert(ignore_prev_mode || command);
00077       command = false;
00078       break;
00079 
00080     case PSM_ENTER_TESTMODE:
00081       assert(!command && (ignore_prev_mode || !testmode));
00082       testmode = true;
00083       break;
00084 
00085     case PSM_LEAVE_TESTMODE:
00086       assert(ignore_prev_mode || testmode);
00087       testmode = false;
00088       break;
00089 
00090     default: NOT_REACHED();
00091   }
00092 
00093   /* Discard all temporary changes */
00094   for (std::set<BasePersistentStorageArray*>::iterator it = _changed_storage_arrays->begin(); it != _changed_storage_arrays->end(); it++) {
00095     DEBUG(desync, 1, "Discarding persistent storage changes: Feature %d, GrfID %08X, Tile %d", (*it)->feature, BSWAP32((*it)->grfid), (*it)->tile);
00096     (*it)->ClearChanges();
00097   }
00098   _changed_storage_arrays->clear();
00099 }