1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-28 06:34:33 +01:00

Codechange: Manage script event queue using smart pointers.

This commit is contained in:
frosch
2025-04-18 18:03:14 +02:00
committed by frosch
parent b9f4ef3d78
commit 0d4588688f
5 changed files with 32 additions and 56 deletions

View File

@@ -9,64 +9,27 @@
#include "../../stdafx.h"
#include "script_event_types.hpp"
#include <queue>
#include "../script_storage.hpp"
#include "../../safeguards.h"
/** The queue of events for a script. */
struct ScriptEventData {
std::queue<ScriptEvent *> 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);
}

View File

@@ -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()

View File

@@ -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