Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef EVENT_ROUTER_H
- #define EVENT_ROUTER_H
- #include "BaseInclude.h"
- namespace SF{
- namespace Core{
- template <typename Evt, typename EvtDispatcher, typename EvtSignature>
- class EventRouter{
- protected:
- typedef std::shared_ptr<EvtDispatcher> PEventDispatcher;
- //Declare types of Template (required! will fail during instantiation if they're not existent! see Event.h)
- typedef std::map<typename EvtSignature, PEventDispatcher> EventMappingContainer;
- typedef typename EvtDispatcher::Callback Callback;
- static EventMappingContainer EventMapping;
- public:
- static void addListener(typename const EvtSignature &signature, Callback callback){
- //Check if there already exists a dispatcher for this event
- EventMappingContainer::iterator iter = EventMapping.find(signature);
- //add if doesn't exist - otherwise just add callback to dispatcher
- if(iter == EventMapping.end()){
- EventMapping.insert(std::make_pair(signature, PEventDispatcher(new EvtDispatcher())));
- iter = EventMapping.find(signature);
- }
- iter->second->addListener(callback);
- }
- static void removeListener(typename const EvtSignature &signature, Callback callback){
- EventMappingContainer::iterator dispatcher = EventMapping.find(signature);
- //Can only delete if event is caught...
- if(dispatcher != EventMapping.end()){
- dispatcher->second->removeListener(callback);
- //If there's no one listening, remove dispatcher
- if(dispatcher->second->ListenersCount() == 0){
- EventMapping.erase(dispatcher);
- }
- }
- }
- static bool dispatch(typename const Evt evt){
- EvtSignature signature = createEventSignature(evt);
- EventMappingContainer::iterator iter = EventMapping.find(signature);
- if(iter != EventMapping.end()){
- iter->second->dispatch(evt);
- }
- return true;
- }
- //Declare only. Hope it works ;) (mad solution^^)
- static EvtSignature createEventSignature(typename const Evt evt);
- };
- }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement