script_event.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "script_event_types.hpp"
00014 
00015 #include <queue>
00016 
00018 struct ScriptEventData {
00019   std::queue<ScriptEvent *> stack; 
00020 };
00021 
00022 /* static */ void ScriptEventController::CreateEventPointer()
00023 {
00024   assert(ScriptObject::GetEventPointer() == NULL);
00025 
00026   ScriptObject::GetEventPointer() = new ScriptEventData();
00027 }
00028 
00029 /* static */ void ScriptEventController::FreeEventPointer()
00030 {
00031   ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
00032 
00033   /* Free all waiting events (if any) */
00034   while (!data->stack.empty()) {
00035     ScriptEvent *e = data->stack.front();
00036     data->stack.pop();
00037     e->Release();
00038   }
00039 
00040   /* Now kill our data pointer */
00041   delete data;
00042 }
00043 
00044 /* static */ bool ScriptEventController::IsEventWaiting()
00045 {
00046   if (ScriptObject::GetEventPointer() == NULL) ScriptEventController::CreateEventPointer();
00047   ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
00048 
00049   return !data->stack.empty();
00050 }
00051 
00052 /* static */ ScriptEvent *ScriptEventController::GetNextEvent()
00053 {
00054   if (ScriptObject::GetEventPointer() == NULL) ScriptEventController::CreateEventPointer();
00055   ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
00056 
00057   if (data->stack.empty()) return NULL;
00058 
00059   ScriptEvent *e = data->stack.front();
00060   data->stack.pop();
00061   return e;
00062 }
00063 
00064 /* static */ void ScriptEventController::InsertEvent(ScriptEvent *event)
00065 {
00066   if (ScriptObject::GetEventPointer() == NULL) ScriptEventController::CreateEventPointer();
00067   ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer();
00068 
00069   event->AddRef();
00070   data->stack.push(event);
00071 }
00072