(svn r27985) -Codechange: Convert VA2 switches into ones with non-overlapping ranges...
[openttd.git] / src / script / api / script_event.cpp
blob0e711ca416189a61c9581fb7c8849a16dd44317b
1 /* $Id$ */
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 */
10 /** @file script_event.cpp Implementation of ScriptEvent. */
12 #include "../../stdafx.h"
13 #include "script_event_types.hpp"
15 #include <queue>
17 #include "../../safeguards.h"
19 /** The queue of events for a script. */
20 struct ScriptEventData {
21 std::queue<ScriptEvent *> stack; ///< The actual queue.
24 /* static */ void ScriptEventController::CreateEventPointer()
26 assert(ScriptObject::GetEventPointer() == NULL);
28 ScriptObject::GetEventPointer() = new ScriptEventData();
31 /* static */ void ScriptEventController::FreeEventPointer()
33 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
35 /* Free all waiting events (if any) */
36 while (!data->stack.empty()) {
37 ScriptEvent *e = data->stack.front();
38 data->stack.pop();
39 e->Release();
42 /* Now kill our data pointer */
43 delete data;
46 /* static */ bool ScriptEventController::IsEventWaiting()
48 if (ScriptObject::GetEventPointer() == NULL) ScriptEventController::CreateEventPointer();
49 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
51 return !data->stack.empty();
54 /* static */ ScriptEvent *ScriptEventController::GetNextEvent()
56 if (ScriptObject::GetEventPointer() == NULL) ScriptEventController::CreateEventPointer();
57 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
59 if (data->stack.empty()) return NULL;
61 ScriptEvent *e = data->stack.front();
62 data->stack.pop();
63 return e;
66 /* static */ void ScriptEventController::InsertEvent(ScriptEvent *event)
68 if (ScriptObject::GetEventPointer() == NULL) ScriptEventController::CreateEventPointer();
69 ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
71 event->AddRef();
72 data->stack.push(event);