2022-09-13 16:34:05 +08:00
|
|
|
/******************************************************************************
|
|
|
|
* \file SHEventManager.h
|
|
|
|
* \author Loh Xiao Qi
|
|
|
|
* \brief Class declaration for event manager.
|
|
|
|
*
|
|
|
|
* \copyright Copyright (c) 2021 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
|
|
|
|
|
|
|
|
#include "SHEvent.h"
|
|
|
|
#include "SHEventReceiver.h"
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
namespace SHADE
|
|
|
|
{
|
2022-09-15 01:46:11 +08:00
|
|
|
using ResponseFunction = std::function<void(SHEvent)>;
|
2022-09-13 16:34:05 +08:00
|
|
|
using ReceiverPtr = std::shared_ptr<SHEventReceiver>;
|
|
|
|
using ResponseVec = std::vector<ReceiverPtr>;
|
|
|
|
using StaticResponseVec = std::vector<ResponseFunction>;
|
|
|
|
|
|
|
|
using EventManagerListener = std::function<void(SHEvent)>;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
* \brief Receives event from the listeners.
|
|
|
|
****************************************************************************/
|
|
|
|
static void CatchEvent(SHEvent);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* \param ResponseFunction - function pointer from receiver to be passed
|
|
|
|
* into event manager to be called when events are broadcasted.
|
|
|
|
* \param SHPackageType - package type that corresponding subscriber is
|
|
|
|
* subscribing to.
|
|
|
|
|
|
|
|
* \brief Links a function pointer from a subscriber to a particular
|
|
|
|
* package type
|
|
|
|
****************************************************************************/
|
2022-09-15 01:46:11 +08:00
|
|
|
void SubscribeTo(SHEventIdentifier, ReceiverPtr);
|
2022-09-13 16:34:05 +08:00
|
|
|
|
2022-09-15 01:46:11 +08:00
|
|
|
template<typename T>
|
|
|
|
T* BroadcastData(T data, SHEventIdentifier eventType);
|
2022-09-13 16:34:05 +08:00
|
|
|
private:
|
|
|
|
// Singleton instance
|
|
|
|
static SHEventManager* instance;
|
|
|
|
|
|
|
|
// Registry for broadcasters and subscribers
|
2022-09-15 01:46:11 +08:00
|
|
|
std::unordered_map<SHEventIdentifier, ResponseVec> packageReceiverRegistry;
|
|
|
|
std::unordered_map<SHEventHandle, SHEventDataPtr> dataEventMap;
|
2022-09-13 16:34:05 +08:00
|
|
|
|
2022-09-15 01:46:11 +08:00
|
|
|
SHEventHandle handleCounter;
|
2022-09-13 16:34:05 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* \param ListenerConstPtr - Const pointer to listener that sent event.
|
|
|
|
* \param EventType - Event data
|
|
|
|
|
|
|
|
* \brief Broadcast event to all receivers that are subscribed to this
|
|
|
|
* listener.
|
|
|
|
****************************************************************************/
|
|
|
|
void Broadcast(SHEvent const&);
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* \param ReceiverPtr - Pointer to receiver
|
|
|
|
* \param ListenerConstPtr - Const pointer to listener that receiver is
|
|
|
|
* subscribing to.
|
|
|
|
|
|
|
|
* \brief Registers receiver as a subscriber to listener in the registry.
|
|
|
|
****************************************************************************/
|
2022-09-15 01:46:11 +08:00
|
|
|
void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr);
|
2022-09-13 16:34:05 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !SH_EVENTS_MANAGER_H
|