Advertisement
Guest User

Generic Signal

a guest
Apr 8th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. template <typename T>
  2. class Signal
  3. {
  4. public:
  5.     void dispatch(T * e)
  6.     {
  7.         //copy dispatch list in case a listener remove itself
  8.         std::vector<std::function<void(T*)>*> dispatch = mListeners;
  9.         std::vector<std::function<void(T*)>*>::iterator it;
  10.         for(it = dispatch.begin(); it != dispatch.end(); ++it)
  11.         {
  12.             (*(*it))(e);
  13.         }
  14.     };
  15.  
  16.     void addListener(std::function<void(T*)> * listener)
  17.     {  
  18.         mListeners.push_back(listener);
  19.     };
  20.  
  21.     void removeListener(std::function<void(T*)> * listener)
  22.     {
  23.         std::vector<std::function<void(T*)>*>::iterator it;
  24.         for(it = mListeners.begin(); it != mListeners.end(); ++it)
  25.         {
  26.             if((*it) == listener)
  27.             {
  28.                 it = mListeners.erase(it);
  29.                 break;
  30.             }
  31.         }
  32.     };
  33. private:
  34.     std::vector<std::function<void(T*)>*> mListeners;
  35. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement