diff --git a/src/script/api/script_event.cpp b/src/script/api/script_event.cpp index efdc720cf3..758794a729 100644 --- a/src/script/api/script_event.cpp +++ b/src/script/api/script_event.cpp @@ -9,64 +9,27 @@ #include "../../stdafx.h" #include "script_event_types.hpp" - -#include +#include "../script_storage.hpp" #include "../../safeguards.h" -/** The queue of events for a script. */ -struct ScriptEventData { - std::queue stack; ///< The actual queue. -}; - -/* static */ void ScriptEventController::CreateEventPointer() -{ - assert(ScriptObject::GetEventPointer() == nullptr); - - ScriptObject::GetEventPointer() = new ScriptEventData(); -} - -/* static */ void ScriptEventController::FreeEventPointer() -{ - ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); - - /* Free all waiting events (if any) */ - while (!data->stack.empty()) { - ScriptEvent *e = data->stack.front(); - data->stack.pop(); - e->Release(); - } - - /* Now kill our data pointer */ - delete data; -} - /* static */ bool ScriptEventController::IsEventWaiting() { - if (ScriptObject::GetEventPointer() == nullptr) ScriptEventController::CreateEventPointer(); - ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); - - return !data->stack.empty(); + return !ScriptObject::GetEventQueue().empty(); } /* static */ ScriptEvent *ScriptEventController::GetNextEvent() { - if (ScriptObject::GetEventPointer() == nullptr) ScriptEventController::CreateEventPointer(); - ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); + auto &queue = ScriptObject::GetEventQueue(); + if (queue.empty()) return nullptr; - if (data->stack.empty()) return nullptr; - - ScriptEvent *e = data->stack.front(); - data->stack.pop(); - return e; + auto *result = queue.front().release(); + queue.pop(); + return result; } /* static */ void ScriptEventController::InsertEvent(ScriptEvent *event) { - if (ScriptObject::GetEventPointer() == nullptr) ScriptEventController::CreateEventPointer(); - ScriptEventData *data = (ScriptEventData *)ScriptObject::GetEventPointer(); - - event->AddRef(); - data->stack.push(event); + ScriptObject::GetEventQueue().push(event); } diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index a7caf017b9..a423f450a2 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -239,9 +239,9 @@ ScriptObject::ActiveInstance::~ActiveInstance() return GetStorage()->allow_do_command && squirrel->CanSuspend(); } -/* static */ void *&ScriptObject::GetEventPointer() +/* static */ ScriptEventQueue &ScriptObject::GetEventQueue() { - return GetStorage()->event_data; + return GetStorage()->event_queue; } /* static */ ScriptLogTypes::LogData &ScriptObject::GetLogData() diff --git a/src/script/api/script_object.hpp b/src/script/api/script_object.hpp index 9a8cf7b226..9f1ccd8227 100644 --- a/src/script/api/script_object.hpp +++ b/src/script/api/script_object.hpp @@ -327,12 +327,12 @@ protected: static bool CanSuspend(); /** - * Get the pointer to store event data in. + * Get the reference to the event queue. */ - static void *&GetEventPointer(); + static struct ScriptEventQueue &GetEventQueue(); /** - * Get the pointer to store log message in. + * Get the reference to the log message storage. */ static ScriptLogTypes::LogData &GetLogData(); @@ -467,6 +467,14 @@ public: if (this->data != nullptr) this->data->Release(); } + /** + * Transfer ownership to the caller. + */ + [[nodiscard]] T *release() + { + return std::exchange(this->data, nullptr); + } + /** * Dereferencing this reference returns a reference to the reference * counted object diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index 4a1dc499ac..a944651d10 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -33,11 +33,7 @@ #include "../safeguards.h" ScriptStorage::ScriptStorage() = default; -ScriptStorage::~ScriptStorage() -{ - /* Free our pointers */ - if (event_data != nullptr) ScriptEventController::FreeEventPointer(); -} +ScriptStorage::~ScriptStorage() = default; /** * Callback called by squirrel when a script uses "print" and for error messages. diff --git a/src/script/script_storage.hpp b/src/script/script_storage.hpp index d77cbb8594..554a3cb9a5 100644 --- a/src/script/script_storage.hpp +++ b/src/script/script_storage.hpp @@ -10,6 +10,8 @@ #ifndef SCRIPT_STORAGE_HPP #define SCRIPT_STORAGE_HPP +#include + #include "../signs_func.h" #include "../vehicle_func.h" #include "../road_type.h" @@ -19,6 +21,13 @@ #include "script_types.hpp" #include "script_log_types.hpp" +#include "script_object.hpp" + +class ScriptEvent; + +/* This is a "struct", so we can forward declare it, and use as incomplete type. */ +struct ScriptEventQueue : std::queue> { +}; /** * The callback function for Mode-classes. @@ -60,7 +69,7 @@ private: RoadType road_type = INVALID_ROADTYPE; ///< The current roadtype we build. RailType rail_type = INVALID_RAILTYPE; ///< The current railtype we build. - void *event_data = nullptr; ///< Event queue for this script. + ScriptEventQueue event_queue; ///< Event queue for this script. ScriptLogTypes::LogData log_data; ///< Log data storage. public: