1
0
Fork 0

Codechange: Manage script event queue using smart pointers.

pull/14037/head
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

View File

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

View File

@ -10,6 +10,8 @@
#ifndef SCRIPT_STORAGE_HPP
#define SCRIPT_STORAGE_HPP
#include <queue>
#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<ScriptObjectRef<ScriptEvent>> {
};
/**
* 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: