113 lines
4.4 KiB
C
113 lines
4.4 KiB
C
|
/******************************************************************************
|
||
|
* \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 <list>
|
||
|
#include <unordered_map>
|
||
|
#include <functional>
|
||
|
|
||
|
namespace SHADE
|
||
|
{
|
||
|
using ResponseFunction = std::function<void(ConstPackagePtr&)>;
|
||
|
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
|
||
|
****************************************************************************/
|
||
|
void SubscribeTo(SHPackageType, ReceiverPtr);
|
||
|
|
||
|
void StaticSubscribeTo(SHPackageType, ResponseFunction);
|
||
|
private:
|
||
|
// Singleton instance
|
||
|
static SHEventManager* instance;
|
||
|
|
||
|
// Registry for broadcasters and subscribers
|
||
|
std::unordered_map<
|
||
|
SHPackageType,
|
||
|
ResponseVec> packageReceiverRegistry;
|
||
|
|
||
|
// Registry for static broadcasters and subscribers
|
||
|
std::unordered_map<
|
||
|
SHPackageType,
|
||
|
StaticResponseVec> staticPackageReceiverRegistry;
|
||
|
|
||
|
/****************************************************************************
|
||
|
* \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.
|
||
|
****************************************************************************/
|
||
|
void RegisterReceiverToType(SHPackageType, ReceiverPtr);
|
||
|
|
||
|
void RegisterStaticReceiverToType(SHPackageType, ResponseFunction);
|
||
|
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
#endif // !SH_EVENTS_MANAGER_H
|