From f54271a4fd6297d96cd629184560dd70b07536d0 Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Fri, 4 Nov 2016 01:31:49 +0200 Subject: [PATCH] Implement arbitrary user data for signals. --- source/Misc/Signal.cpp | 5 +++++ source/Misc/Signal.hpp | 21 +++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/source/Misc/Signal.cpp b/source/Misc/Signal.cpp index 365a8161..5511b057 100644 --- a/source/Misc/Signal.cpp +++ b/source/Misc/Signal.cpp @@ -26,6 +26,8 @@ void Signal::Terminate() s.second.mPtr->Clear(); // Release the name s.second.mPtr->m_Name.clear(); + // Release whatever is in the user data + s.second.mPtr->m_Data.Release(); } // Finally clear the container itself s_Signals.clear(); @@ -36,6 +38,8 @@ void Signal::Terminate() { // Clear slots s->Clear(); + // Release whatever is in the user data + s->m_Data.Release(); } // Finally clear the container itself s_FreeSignals.clear(); @@ -246,6 +250,7 @@ void Register_Signal(HSQUIRRELVM vm) .SquirrelFunc(_SC("_typename"), &Signal::Typename) .Func(_SC("_tostring"), &Signal::ToString) // Core Properties + .Prop(_SC("Data"), &Signal::GetData, &Signal::SetData) .Prop(_SC("Slots"), &Signal::Count) // Core Methods .Func(_SC("Clear"), &Signal::Clear) diff --git a/source/Misc/Signal.hpp b/source/Misc/Signal.hpp index 534ef247..6b60b4a0 100644 --- a/source/Misc/Signal.hpp +++ b/source/Misc/Signal.hpp @@ -341,13 +341,14 @@ private: // -------------------------------------------------------------------------------------------- Slot * m_Head; // The chain of slots bound to this signal. + Object m_Data; // User data associated with this instance. String m_Name; // The name that identifies this signal. /* -------------------------------------------------------------------------------------------- * Default constructor. */ Signal() - : m_Head(nullptr), m_Name() + : m_Head(nullptr), m_Data(), m_Name() { s_FreeSignals.push_back(this); } @@ -374,7 +375,7 @@ private: * Base constructor. */ Signal(String && name) - : m_Head(nullptr), m_Name(std::move(name)) + : m_Head(nullptr), m_Data(), m_Name(std::move(name)) { if (m_Name.empty()) { @@ -449,6 +450,22 @@ public: */ static SQInteger Typename(HSQUIRRELVM vm); + /* -------------------------------------------------------------------------------------------- + * Retrieve the associated user data. + */ + Object & GetData() + { + return m_Data; + } + + /* -------------------------------------------------------------------------------------------- + * Modify the associated user data. + */ + void SetData(Object & data) + { + m_Data = data; + } + /* -------------------------------------------------------------------------------------------- * The number of slots connected to the signal. */