Advertisement
bueddl

Untitled

Oct 1st, 2015
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include "event.h"
  3.  
  4. ///////////////////////////////////////////////////////////////////////////////
  5.  
  6. struct socket {
  7. public:
  8.     event<socket&, int>::collector_type& on_some_event()
  9.     {
  10.         return m_some_event.on_signal();
  11.     }
  12.  
  13.     void some_func_that_signals_event()
  14.     {
  15.         m_some_event(*this, 1337);
  16.     }
  17.  
  18. private:
  19.     event<socket&, int> m_some_event;
  20. };
  21.  
  22. ///////////////////////////////////////////////////////////////////////////////
  23.  
  24. void my_free_handler(socket &sender, int param)
  25. {
  26.     std::cout << "my_free_handler(" << &sender << ", " << param << ")" << std::endl;
  27.  
  28.     // detach after handle from event
  29.     sender.on_some_event() -= my_free_handler;
  30. }
  31.  
  32. struct my_handler
  33. {
  34.     void handle(socket &sender, int param)
  35.     {
  36.         std::cout << "my_handler::handle(" << &sender << ", " << param << ")" << std::endl;
  37.         m_event_counter++;
  38.  
  39.         if (m_event_counter == 2)
  40.             sender.on_some_event() -= mem_fn(this, &my_handler::handle);
  41.     }
  42.  
  43. private:
  44.     int m_event_counter = 0;
  45. };
  46.  
  47. ///////////////////////////////////////////////////////////////////////////////
  48.  
  49. int main()
  50. {
  51.     socket sock;
  52.  
  53.     // Add a free handler function
  54.     sock.on_some_event() += my_free_handler;
  55.  
  56.     // Add an object member method as an handler
  57.     my_handler handler;
  58.     sock.on_some_event() += mem_fn(&handler, &my_handler::handle);
  59.  
  60.     // Do something to signal the event
  61.     sock.some_func_that_signals_event();
  62.     // ... and again
  63.     sock.some_func_that_signals_event();
  64.     sock.some_func_that_signals_event();
  65.     sock.some_func_that_signals_event();
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement