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 8 /** @file script_event.cpp Implementation of ScriptEvent. */ 9 10 #include "../../stdafx.h" 11 #include "script_event_types.hpp" 12 13 #include <queue> 14 15 #include "../../safeguards.h" 16 17 /** The queue of events for a script. */ 18 struct ScriptEventData { 19 std::queue<ScriptEvent *> stack; ///< The actual queue. 20 }; 21 CreateEventPointer()22/* static */ void ScriptEventController::CreateEventPointer() 23 { 24 assert(ScriptObject::GetEventPointer() == nullptr); 25 26 ScriptObject::GetEventPointer() = new ScriptEventData(); 27 } 28 FreeEventPointer()29/* static */ void ScriptEventController::FreeEventPointer() 30 { 31 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); 32 33 /* Free all waiting events (if any) */ 34 while (!data->stack.empty()) { 35 ScriptEvent *e = data->stack.front(); 36 data->stack.pop(); 37 e->Release(); 38 } 39 40 /* Now kill our data pointer */ 41 delete data; 42 } 43 IsEventWaiting()44/* static */ bool ScriptEventController::IsEventWaiting() 45 { 46 if (ScriptObject::GetEventPointer() == nullptr) ScriptEventController::CreateEventPointer(); 47 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); 48 49 return !data->stack.empty(); 50 } 51 GetNextEvent()52/* static */ ScriptEvent *ScriptEventController::GetNextEvent() 53 { 54 if (ScriptObject::GetEventPointer() == nullptr) ScriptEventController::CreateEventPointer(); 55 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); 56 57 if (data->stack.empty()) return nullptr; 58 59 ScriptEvent *e = data->stack.front(); 60 data->stack.pop(); 61 return e; 62 } 63 InsertEvent(ScriptEvent * event)64/* static */ void ScriptEventController::InsertEvent(ScriptEvent *event) 65 { 66 if (ScriptObject::GetEventPointer() == nullptr) ScriptEventController::CreateEventPointer(); 67 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); 68 69 event->AddRef(); 70 data->stack.push(event); 71 } 72 73