Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.00 KB | None | 0 0
  1. #ifndef EVENT_ROUTER_H
  2. #define EVENT_ROUTER_H
  3.  
  4. #include "BaseInclude.h"
  5.  
  6. namespace SF{
  7.     namespace Core{
  8. template <typename Evt, typename EvtDispatcher, typename EvtSignature>
  9.  
  10.     class EventRouter{
  11.     protected:
  12.         typedef std::shared_ptr<EvtDispatcher> PEventDispatcher;
  13.        
  14.         //Declare types of Template (required! will fail during instantiation if they're not existent! see Event.h)
  15.         typedef std::map<typename EvtSignature, PEventDispatcher> EventMappingContainer;
  16.         typedef typename EvtDispatcher::Callback Callback;
  17.  
  18.         static EventMappingContainer EventMapping;
  19.  
  20.     public:
  21.  
  22.         static void addListener(typename const EvtSignature &signature, Callback callback){
  23.  
  24.                 //Check if there already exists a dispatcher for this event
  25.                 EventMappingContainer::iterator iter = EventMapping.find(signature);
  26.  
  27.                 //add if doesn't exist - otherwise just add callback to dispatcher
  28.                 if(iter == EventMapping.end()){
  29.                     EventMapping.insert(std::make_pair(signature,  PEventDispatcher(new EvtDispatcher())));
  30.                     iter = EventMapping.find(signature);
  31.                 }
  32.                
  33.                 iter->second->addListener(callback);
  34.             }
  35.         static void removeListener(typename const EvtSignature &signature, Callback callback){
  36.                 EventMappingContainer::iterator dispatcher = EventMapping.find(signature);
  37.  
  38.                 //Can only delete if event is caught...
  39.                 if(dispatcher != EventMapping.end()){
  40.                     dispatcher->second->removeListener(callback);
  41.  
  42.                     //If there's no one listening, remove dispatcher
  43.                     if(dispatcher->second->ListenersCount() == 0){
  44.                         EventMapping.erase(dispatcher);
  45.                     }
  46.                 }
  47.             }
  48.         static bool dispatch(typename const Evt evt){
  49.                 EvtSignature signature = createEventSignature(evt);
  50.                 EventMappingContainer::iterator iter = EventMapping.find(signature);
  51.  
  52.                 if(iter != EventMapping.end()){
  53.                     iter->second->dispatch(evt);
  54.                 }
  55.  
  56.                 return true;
  57.             }
  58.  
  59.         //Declare only. Hope it works ;) (mad solution^^)
  60.         static EvtSignature createEventSignature(typename const Evt evt);
  61.  
  62.     };
  63.  
  64.     }
  65. }
  66. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement