Add support for event subscription (delegates)

This commit is contained in:
smallmodel 2025-02-02 00:51:38 +01:00
parent cfb343d262
commit 9cb593c9e4
No known key found for this signature in database
GPG key ID: 9F2D623CEDF08512
5 changed files with 523 additions and 0 deletions

View file

@ -0,0 +1,162 @@
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "scriptdelegate.h"
#include "../script/scriptexception.h"
ScriptDelegate *ScriptDelegate::root = NULL;
ScriptRegisteredDelegate_Script::ScriptRegisteredDelegate_Script(const ScriptThreadLabel& inLabel)
: label(inLabel)
{}
void ScriptRegisteredDelegate_Script::Execute(const Event& ev)
{
Event newev = ev;
label.Execute(NULL, newev);
}
bool ScriptRegisteredDelegate_Script::operator==(const ScriptRegisteredDelegate_Script& registeredDelegate) const
{
return label == registeredDelegate.label;
}
ScriptRegisteredDelegate_CodeMember::ScriptRegisteredDelegate_CodeMember(
Class *inObject, DelegateClassResponse inResponse
)
: object(inObject)
, response(inResponse)
{}
void ScriptRegisteredDelegate_CodeMember::Execute(const Event& ev)
{
if (!object) {
return;
}
(object->*response)(ev);
}
bool ScriptRegisteredDelegate_CodeMember::operator==(const ScriptRegisteredDelegate_CodeMember& registeredDelegate
) const
{
return object == registeredDelegate.object && response == registeredDelegate.response;
}
ScriptRegisteredDelegate_Code::ScriptRegisteredDelegate_Code(DelegateResponse inResponse)
: response(inResponse)
{}
void ScriptRegisteredDelegate_Code::Execute(const Event& ev)
{
(*response)(ev);
}
bool ScriptRegisteredDelegate_Code::operator==(const ScriptRegisteredDelegate_Code& registeredDelegate) const
{
return response == registeredDelegate.response;
}
ScriptDelegate::ScriptDelegate(const char *inName, const char *inDescription)
: name(inName)
, description(inDescription)
{
LL_SafeAdd(root, this, next, prev);
}
ScriptDelegate::~ScriptDelegate()
{
LL_SafeRemoveRoot(root, this, next, prev);
}
const ScriptDelegate *ScriptDelegate::GetRoot()
{
return root;
}
const ScriptDelegate *ScriptDelegate::GetNext() const
{
return next;
}
void ScriptDelegate::Register(const ScriptThreadLabel& label)
{
if (!label.IsSet()) {
ScriptError("Invalid label specified for the script delegate");
}
list_script.AddUniqueObject(label);
}
void ScriptDelegate::Unregister(const ScriptThreadLabel& label)
{
list_script.RemoveObject(label);
}
void ScriptDelegate::Register(ScriptRegisteredDelegate_Code::DelegateResponse response)
{
list_code.AddUniqueObject(ScriptRegisteredDelegate_Code(response));
}
void ScriptDelegate::Unregister(ScriptRegisteredDelegate_Code::DelegateResponse response)
{
list_code.RemoveObject(response);
}
void ScriptDelegate::Register(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response)
{
list_codeMember.AddUniqueObject(ScriptRegisteredDelegate_CodeMember(object, response));
}
void ScriptDelegate::Unregister(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response)
{
list_codeMember.RemoveObject(ScriptRegisteredDelegate_CodeMember(object, response));
}
void ScriptDelegate::Trigger(const Event& ev) const
{
size_t i;
for (i = 1; i <= list_script.NumObjects(); i++) {
list_script.ObjectAt(i).Execute(ev);
}
for (i = 1; i <= list_code.NumObjects(); i++) {
list_code.ObjectAt(i).Execute(ev);
}
for (i = 1; i <= list_codeMember.NumObjects(); i++) {
list_codeMember.ObjectAt(i).Execute(ev);
}
}
ScriptDelegate *ScriptDelegate::GetScriptDelegate(const char *name)
{
for (ScriptDelegate *delegate = root; delegate; delegate = delegate->next) {
if (!Q_stricmp(delegate->name, name)) {
return delegate;
}
}
return NULL;
}

183
code/fgame/scriptdelegate.h Normal file
View file

@ -0,0 +1,183 @@
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
// scriptdelegate -- manages function delegate
#include "../qcommon/listener.h"
#include "../qcommon/delegate.h"
#include "gamescript.h"
class ScriptRegisteredDelegate
{
public:
void Execute(const Event& ev);
};
/**
* Registered delegate, for scripts.
* It contains a ScriptThreadLabel with the game script and the label to execute.
*/
class ScriptRegisteredDelegate_Script : public ScriptRegisteredDelegate
{
public:
ScriptRegisteredDelegate_Script(const ScriptThreadLabel& inLabel);
void Execute(const Event& ev);
bool operator==(const ScriptRegisteredDelegate_Script& registeredDelegate) const;
private:
ScriptThreadLabel label;
};
/**
* Registered delegate, for code use.
* It contains the function to execute.
*/
class ScriptRegisteredDelegate_Code : public ScriptRegisteredDelegate
{
public:
using DelegateResponse = void (*)(const Event& ev);
public:
ScriptRegisteredDelegate_Code(DelegateResponse inResponse);
void Execute(const Event& ev);
bool operator==(const ScriptRegisteredDelegate_Code& registeredDelegate) const;
private:
DelegateResponse response;
};
/**
* Registered delegate, for code use.
* It contains the object along the member function to execute.
* The function will not be executed if the object is NULL.
*/
class ScriptRegisteredDelegate_CodeMember : public ScriptRegisteredDelegate
{
public:
using DelegateClassResponse = void (Class::*)(const Event& ev);
public:
ScriptRegisteredDelegate_CodeMember(Class *inObject, DelegateClassResponse inResponse);
void Execute(const Event& ev);
bool operator==(const ScriptRegisteredDelegate_CodeMember& registeredDelegate) const;
private:
SafePtr<Class> object;
DelegateClassResponse response;
};
/**
* A script delegate provides a way for code to subscribe for events.
* Scripts and code can register for a delegate and have their function executed
* when the delegate gets triggered.
*/
class ScriptDelegate
{
public:
ScriptDelegate(const char *name, const char *description);
~ScriptDelegate();
static const ScriptDelegate *GetRoot();
const ScriptDelegate *GetNext() const;
/**
* Register a script label.
*
* @param label The label to be executed
*/
void Register(const ScriptThreadLabel& label);
/**
* Unregistered the label.
*
* @param label The label to unregister
*/
void Unregister(const ScriptThreadLabel& label);
/**
* Register a function.
*
* @param response The function to be executed
*/
void Register(ScriptRegisteredDelegate_Code::DelegateResponse response);
/**
* Unregistered the function.
*
* @param response the function to unregister
*/
void Unregister(ScriptRegisteredDelegate_Code::DelegateResponse response);
/**
* Register with an object and a member function.
*
* @param object The object to notify
* @param response The member function of the object to be executed
*/
void Register(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response);
/**
* Unregistered the member function.
*
* @param object The object where the member function is
* @param response The member function to unregister
*/
void Unregister(Class *object, ScriptRegisteredDelegate_CodeMember::DelegateClassResponse response);
/**
* Executes all registered delegates with the specified event.
*
* @param ev Parameter list
*/
void Trigger(const Event& ev) const;
/**
* Search and return the specified script delegate by name.
*
* @param name The name to search for
*/
static ScriptDelegate *GetScriptDelegate(const char *name);
// non-movable and non-copyable
ScriptDelegate(ScriptDelegate&& other) = delete;
ScriptDelegate& operator=(ScriptDelegate&& other) = delete;
ScriptDelegate(const ScriptDelegate& other) = delete;
ScriptDelegate& operator=(const ScriptDelegate& other) = delete;
private:
// Linked-list
ScriptDelegate *next;
ScriptDelegate *prev;
static ScriptDelegate *root;
const char *name;
const char *description;
Container<ScriptRegisteredDelegate_Script> list_script;
Container<ScriptRegisteredDelegate_Code> list_code;
Container<ScriptRegisteredDelegate_CodeMember> list_codeMember;
};

View file

@ -13,6 +13,7 @@ set(SOURCES_SHARED_UBER
"${CMAKE_SOURCE_DIR}/code/qcommon/class.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/class.cpp"
"${CMAKE_SOURCE_DIR}/code/qcommon/con_set.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/con_set.cpp"
"${CMAKE_SOURCE_DIR}/code/qcommon/con_timer.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/con_timer.cpp"
"${CMAKE_SOURCE_DIR}/code/qcommon/delegate.cpp"
"${CMAKE_SOURCE_DIR}/code/qcommon/lightclass.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/lightclass.cpp"
"${CMAKE_SOURCE_DIR}/code/qcommon/listener.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/listener.cpp"
"${CMAKE_SOURCE_DIR}/code/qcommon/lz77.cpp" "${CMAKE_SOURCE_DIR}/code/qcommon/lz77.cpp"

52
code/qcommon/delegate.cpp Normal file
View file

@ -0,0 +1,52 @@
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "delegate.h"
uint64_t DelegateHandle::currentHandle = 0;
DelegateHandle::DelegateHandle()
: handle(GenerateDelegateID())
{}
bool DelegateHandle::operator==(const DelegateHandle& other) const
{
return handle == other.handle;
}
bool DelegateHandle::operator!=(const DelegateHandle& other) const
{
return handle != other.handle;
}
uint64_t DelegateHandle::GenerateDelegateID()
{
uint64_t handle;
handle = ++currentHandle;
if (handle == 0) {
handle = ++currentHandle;
}
return handle;
}

125
code/qcommon/delegate.h Normal file
View file

@ -0,0 +1,125 @@
/*
===========================================================================
Copyright (C) 2025 the OpenMoHAA team
This file is part of OpenMoHAA source code.
OpenMoHAA source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
OpenMoHAA source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenMoHAA source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include <functional>
#include "container.h"
template<typename T>
using Delegate = std::function<T>;
struct DelegateHandle {
public:
DelegateHandle();
bool operator==(const DelegateHandle& other) const;
bool operator!=(const DelegateHandle& other) const;
private:
static uint64_t GenerateDelegateID();
static uint64_t currentHandle;
uint64_t handle;
};
template<typename T>
class DelegateMultiElement
{
public:
DelegateMultiElement(Delegate<T>&& inFunction);
template<typename... Args>
void Execute(Args&&...args) const;
DelegateHandle GetHandle() const;
private:
DelegateHandle handle;
Delegate<T> func;
};
template<typename T>
DelegateMultiElement<T>::DelegateMultiElement(Delegate<T>&& inFunction)
: func(inFunction)
{}
template<typename T>
template<typename... Args>
void DelegateMultiElement<T>::Execute(Args&&...args) const
{
func(std::move(args)...);
}
template<typename T>
DelegateHandle DelegateMultiElement<T>::GetHandle() const
{
return handle;
}
template<typename T>
class MulticastDelegate
{
public:
DelegateHandle Add(Delegate<T>&& function);
void Remove(DelegateHandle handle);
template<typename... Args>
void Execute(Args&&...args);
private:
Container<DelegateMultiElement<T>> delegates;
};
template<typename T>
DelegateHandle MulticastDelegate<T>::Add(Delegate<T>&& function)
{
int index = delegates.AddObject(DelegateMultiElement<T>(std::move(function)));
return delegates.ObjectAt(index).GetHandle();
}
template<typename T>
void MulticastDelegate<T>::Remove(DelegateHandle handle)
{
size_t i;
for (i = delegates.NumObjects(); i > 0; i--) {
const DelegateMultiElement<T>& elem = delegates.ObjectAt(i);
if (elem.GetHandle() == handle) {
delegates.RemoveObjectAt(i);
break;
}
}
}
template<typename T>
template<typename... Args>
void MulticastDelegate<T>::Execute(Args&&...args)
{
size_t i;
for (i = 1; i <= delegates.NumObjects(); i++) {
const DelegateMultiElement<T>& element = delegates.ObjectAt(i);
element.Execute(std::move(args)...);
}
}