SP3-108
Removed singleton instance from event manager and changed it to static class instead. WARNING: Might need to change back to singleton next time, I had to do it previously for some reason but I don't remember why
This commit is contained in:
parent
d62201367a
commit
ddf6a7bee5
|
@ -171,6 +171,9 @@
|
|||
<ClInclude Include="src\Events\SHEvent.h">
|
||||
<Filter>Events</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Events\SHEventDefines.h">
|
||||
<Filter>Events</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Events\SHEventManager.h">
|
||||
<Filter>Events</Filter>
|
||||
</ClInclude>
|
||||
|
@ -405,7 +408,6 @@
|
|||
<ClInclude Include="src\Tools\SHUtilities.h">
|
||||
<Filter>Tools</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="src\Events\SHEventDefines.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="src\ECS_Base\Components\SHComponent.cpp">
|
||||
|
|
|
@ -16,7 +16,7 @@ namespace SHADE
|
|||
struct SHEvent
|
||||
{
|
||||
SHEventIdentifier type;
|
||||
SHEventDataPtr data;
|
||||
SHEventDataPtr dataPtr;
|
||||
SHEventHandle handle;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -12,47 +12,18 @@
|
|||
|
||||
namespace SHADE
|
||||
{
|
||||
SHEventManager* SHEventManager::instance;
|
||||
std::unordered_map<SHEventIdentifier, ResponseVec> SHEventManager::packageReceiverRegistry;
|
||||
std::unordered_map<SHEventHandle, SHEventDataPtr> SHEventManager::dataEventMap;
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Ctor and Dtor. Dtor will delete instance of self stored at
|
||||
* data member instance.
|
||||
****************************************************************************/
|
||||
SHEventManager::SHEventManager()
|
||||
{
|
||||
SHEventHandle SHEventManager::handleCounter{ 0 };
|
||||
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Exit function to terminate the manager properly and deallocate
|
||||
* memory.
|
||||
****************************************************************************/
|
||||
void SHEventManager::Exit()
|
||||
{
|
||||
delete instance;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns singleton instance of event manager in heap memory. If
|
||||
* instance does not exist, one is created immediately.
|
||||
****************************************************************************/
|
||||
SHEventManager& SHEventManager::GetEventManagerInstance()
|
||||
{
|
||||
// Create instance of event manager and return
|
||||
if (!instance)
|
||||
{
|
||||
instance = new SHEventManager();
|
||||
}
|
||||
|
||||
return *instance;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns function pointer to entry point for events.
|
||||
****************************************************************************/
|
||||
EventManagerListener SHEventManager::GetListenerFunction()
|
||||
{
|
||||
return &(instance->CatchEvent);
|
||||
return &CatchEvent;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -66,7 +37,7 @@ namespace SHADE
|
|||
|
||||
// Do something with the event
|
||||
|
||||
instance->Broadcast(event);
|
||||
Broadcast(event);
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
|
@ -80,11 +51,11 @@ namespace SHADE
|
|||
****************************************************************************/
|
||||
void SHEventManager::SubscribeTo(SHEventIdentifier pkgType, ReceiverPtr receiver)
|
||||
{
|
||||
SHEventManager::GetEventManagerInstance().RegisterReceiverToType(pkgType, receiver);
|
||||
RegisterReceiverToType(pkgType, receiver);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T* SHEventManager::BroadcastData(T data, SHEventIdentifier eventType)
|
||||
T* SHEventManager::BroadcastEvent(T data, SHEventIdentifier eventType)
|
||||
{
|
||||
SHEventDataPtr ptr = new std::byte[sizeof(data)];
|
||||
|
||||
|
@ -133,10 +104,10 @@ namespace SHADE
|
|||
ResponseVec& receivers{ packageReceiverRegistry[event.type] };
|
||||
for (auto& receiver : receivers)
|
||||
{
|
||||
receiver->Receive(event.data);
|
||||
receiver->Receive(event);
|
||||
}
|
||||
|
||||
//auto& funcs{ staticPackageReceiverRegistry[event.GetType()] };
|
||||
//auto& funcs{ ackageReceiverRegistry[event.GetType()] };
|
||||
//for (auto func : funcs)
|
||||
//{
|
||||
// func(event.GetData());
|
||||
|
|
|
@ -3,21 +3,26 @@
|
|||
* \author Loh Xiao Qi
|
||||
* \brief Class declaration for event manager.
|
||||
*
|
||||
* \copyright Copyright (c) 2021 Digipen Institute of Technology. Reproduction
|
||||
* \copyright Copyright (c) 2022 Digipen Institute of Technology. Reproduction
|
||||
* or disclosure of this file or its contents without the prior written consent
|
||||
* of Digipen Institute of Technology is prohibited.
|
||||
******************************************************************************/
|
||||
#ifndef SH_EVENT_MANAGER_H
|
||||
#define SH_EVENT_MANAGER_H
|
||||
#pragma once
|
||||
|
||||
#include "SHEvent.h"
|
||||
#include "SHEventReceiver.h"
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
|
||||
/******************************************************************************
|
||||
INSTRUCTIONS FOR USE:
|
||||
On broadcaster side:
|
||||
1.
|
||||
******************************************************************************/
|
||||
|
||||
namespace SHADE
|
||||
{
|
||||
using ResponseFunction = std::function<void(SHEvent)>;
|
||||
using ResponseFunction = std::function<SHEventHandle(SHEvent)>;
|
||||
using ReceiverPtr = std::shared_ptr<SHEventReceiver>;
|
||||
using ResponseVec = std::vector<ReceiverPtr>;
|
||||
using StaticResponseVec = std::vector<ResponseFunction>;
|
||||
|
@ -27,29 +32,11 @@ namespace SHADE
|
|||
class SHEventManager
|
||||
{
|
||||
public:
|
||||
/****************************************************************************
|
||||
* \brief Ctor and Dtor. Dtor will delete instance of self stored at
|
||||
* data member instance.
|
||||
****************************************************************************/
|
||||
SHEventManager();
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns singleton instance of event manager in heap memory. If
|
||||
* instance does not exist, one is created immediately.
|
||||
****************************************************************************/
|
||||
static SHEventManager& GetEventManagerInstance();
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Returns function pointer to entry point for events.
|
||||
****************************************************************************/
|
||||
static EventManagerListener GetListenerFunction();
|
||||
|
||||
/****************************************************************************
|
||||
* \brief Exit function to terminate the manager properly and deallocate
|
||||
* memory.
|
||||
****************************************************************************/
|
||||
static void Exit();
|
||||
|
||||
/****************************************************************************
|
||||
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
||||
* \param EventType - Templated type for every type of event
|
||||
|
@ -67,19 +54,18 @@ namespace SHADE
|
|||
* \brief Links a function pointer from a subscriber to a particular
|
||||
* package type
|
||||
****************************************************************************/
|
||||
void SubscribeTo(SHEventIdentifier, ReceiverPtr);
|
||||
static void SubscribeTo(SHEventIdentifier, ReceiverPtr);
|
||||
|
||||
template<typename T>
|
||||
T* BroadcastData(T data, SHEventIdentifier eventType);
|
||||
static T* BroadcastEvent(T data, SHEventIdentifier eventType);
|
||||
|
||||
private:
|
||||
// Singleton instance
|
||||
static SHEventManager* instance;
|
||||
|
||||
// Registry for broadcasters and subscribers
|
||||
std::unordered_map<SHEventIdentifier, ResponseVec> packageReceiverRegistry;
|
||||
std::unordered_map<SHEventHandle, SHEventDataPtr> dataEventMap;
|
||||
static std::unordered_map<SHEventIdentifier, ResponseVec> packageReceiverRegistry;
|
||||
static std::unordered_map<SHEventHandle, SHEventDataPtr> dataEventMap;
|
||||
|
||||
SHEventHandle handleCounter;
|
||||
static SHEventHandle handleCounter;
|
||||
|
||||
/****************************************************************************
|
||||
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
||||
|
@ -88,7 +74,7 @@ namespace SHADE
|
|||
* \brief Broadcast event to all receivers that are subscribed to this
|
||||
* listener.
|
||||
****************************************************************************/
|
||||
void Broadcast(SHEvent const&);
|
||||
static void Broadcast(SHEvent const&);
|
||||
|
||||
/****************************************************************************
|
||||
* \param ReceiverPtr - Pointer to receiver
|
||||
|
@ -97,9 +83,7 @@ namespace SHADE
|
|||
|
||||
* \brief Registers receiver as a subscriber to listener in the registry.
|
||||
****************************************************************************/
|
||||
void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr);
|
||||
static void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr);
|
||||
|
||||
};
|
||||
}
|
||||
|
||||
#endif // !SH_EVENTS_MANAGER_H
|
||||
|
|
|
@ -1,5 +1,4 @@
|
|||
#ifndef SH_EVENT_RECEIVER_H
|
||||
#define SH_EVENT_RECEIVER_H
|
||||
#pragma once
|
||||
|
||||
#include "SHEvent.h"
|
||||
|
||||
|
@ -9,7 +8,7 @@ namespace SHADE
|
|||
{
|
||||
private:
|
||||
public:
|
||||
virtual void Receive(SHEventDataPtr) = 0;
|
||||
virtual void Receive(SHEvent) = 0;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
|
@ -17,7 +16,7 @@ namespace SHADE
|
|||
{
|
||||
private:
|
||||
T* object;
|
||||
void(T::*callback)(SHEventDataPtr);
|
||||
SHEventHandle(T::*callback)(SHEvent);
|
||||
|
||||
public:
|
||||
SHEventReceiverSpec(T* obj, void(T::* cb)(SHEventDataPtr))
|
||||
|
@ -26,10 +25,9 @@ namespace SHADE
|
|||
|
||||
}
|
||||
|
||||
void Receive(SHEventDataPtr evt) override
|
||||
void Receive(SHEvent evt) override
|
||||
{
|
||||
(object->*callback)(evt);
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif // !SH_EVENT_RECEIVER_H
|
||||
|
|
Loading…
Reference in New Issue