/****************************************************************************** * \file SHEventManager.h * \author Loh Xiao Qi * \brief Class declaration for event manager. * * \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. ******************************************************************************/ #pragma once #include "SHEvent.h" #include "SHEventReceiver.h" #include #include /****************************************************************************** INSTRUCTIONS FOR USE: On broadcaster side: 1. Create a struct/class to contain the data that you would need to send in the event. 2. Create unique event identifier in SHEventDefines.h, follow the example provided. 3. When ready to send the event, call SHEventManager::BroadcastEvent(exampleClass, EVENT_IDENTIFIER); Headers required: SHEventManager.h On Receiver side: 1. Create a function with the signature: SHEventHandle FunctionName(SHEvent); 2. In the init function of the class, copy the below in and replace the necessary: std::shared_ptr> thisReceiver{ std::make_shared>(this, &ReceiverClass::ReceiveFunction) }; ReceiverPtr receiver = std::dynamic_pointer_cast(thisReceiver); SHEventManager::SubscribeTo(EVENT_IDENTIFIER, receiver); 3. Note: The EventIdentifier should match all that is defined in SHEventDefines.h so check there. When the receiver catches the event, it needs to know the struct that the broadcaster is using to cast the void* properly. Headers required: SHEventManager.h, SHEventReceiver.h If you have any questions/suggestions for improvement lmk. ******************************************************************************/ namespace SHADE { using ResponseFunction = std::function; using ReceiverPtr = std::shared_ptr; using ResponseVec = std::vector; using StaticResponseVec = std::vector; using EventManagerListener = std::function; class SHEventManager { public: /**************************************************************************** * \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 ****************************************************************************/ static void SubscribeTo(SHEventIdentifier, ReceiverPtr); template static T* BroadcastEvent(T data, SHEventIdentifier eventType); private: // Registry for broadcasters and subscribers static std::unordered_map packageReceiverRegistry; static std::unordered_map dataEventMap; static SHEventHandle handleCounter; /**************************************************************************** * \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. ****************************************************************************/ static 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. ****************************************************************************/ static void RegisterReceiverToType(SHEventIdentifier, ReceiverPtr); }; }