Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "event.h"
- ///////////////////////////////////////////////////////////////////////////////
- struct socket {
- public:
- event<socket&, int>::collector_type& on_some_event()
- {
- return m_some_event.on_signal();
- }
- void some_func_that_signals_event()
- {
- m_some_event(*this, 1337);
- }
- private:
- event<socket&, int> m_some_event;
- };
- ///////////////////////////////////////////////////////////////////////////////
- void my_free_handler(socket &sender, int param)
- {
- std::cout << "my_free_handler(" << &sender << ", " << param << ")" << std::endl;
- // detach after handle from event
- sender.on_some_event() -= my_free_handler;
- }
- struct my_handler
- {
- void handle(socket &sender, int param)
- {
- std::cout << "my_handler::handle(" << &sender << ", " << param << ")" << std::endl;
- m_event_counter++;
- if (m_event_counter == 2)
- sender.on_some_event() -= mem_fn(this, &my_handler::handle);
- }
- private:
- int m_event_counter = 0;
- };
- ///////////////////////////////////////////////////////////////////////////////
- int main()
- {
- socket sock;
- // Add a free handler function
- sock.on_some_event() += my_free_handler;
- // Add an object member method as an handler
- my_handler handler;
- sock.on_some_event() += mem_fn(&handler, &my_handler::handle);
- // Do something to signal the event
- sock.some_func_that_signals_event();
- // ... and again
- sock.some_func_that_signals_event();
- sock.some_func_that_signals_event();
- sock.some_func_that_signals_event();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement