# ai_event.cpp

Go to the documentation of this file.

    00001 /* $Id: ai_event.cpp 15299 2009-01-31 20:16:06Z smatz $ */
    00002
    00005 #include "ai_event_types.hpp"
    00006
    00007 #include <queue>
    00008
    00009 struct AIEventData {
    00010   std::queue<AIEvent *> stack;
    00011 };
    00012
    00013 /* static */ void AIEventController::CreateEventPointer()
    00014 {
    00015   assert(AIObject::GetEventPointer() == NULL);
    00016
    00017   AIObject::GetEventPointer() = new AIEventData();
    00018 }
    00019
    00020 /* static */ void AIEventController::FreeEventPointer()
    00021 {
    00022   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00023
    00024   /* Free all waiting events (if any) */
    00025   while (!data->stack.empty()) {
    00026     AIEvent *e = data->stack.front();
    00027     data->stack.pop();
    00028     e->Release();
    00029   }
    00030
    00031   /* Now kill our data pointer */
    00032   delete data;
    00033 }
    00034
    00035 /* static */ bool AIEventController::IsEventWaiting()
    00036 {
    00037   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00038   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00039
    00040   return !data->stack.empty();
    00041 }
    00042
    00043 /* static */ AIEvent *AIEventController::GetNextEvent()
    00044 {
    00045   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00046   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00047
    00048   if (data->stack.empty()) return NULL;
    00049
    00050   AIEvent *e = data->stack.front();
    00051   data->stack.pop();
    00052   return e;
    00053 }
    00054
    00055 /* static */ void AIEventController::InsertEvent(AIEvent *event)
    00056 {
    00057   if (AIObject::GetEventPointer() == NULL) AIEventController::CreateEventPointer();
    00058   AIEventData *data = (AIEventData *)AIObject::GetEventPointer();
    00059
    00060   event->AddRef();
    00061   data->stack.push(event);
    00062 }
    00063