Advertisement
bueddl

Untitled

Sep 23rd, 2015
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.64 KB | None | 0 0
  1. // This file is part of w3c - wwwcam.
  2.  
  3. // w3c is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7.  
  8. // w3c is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12.  
  13. // You should have received a copy of the GNU General Public License
  14. // along with w3c. If not, see <http://www.gnu.org/licenses/>.
  15. //
  16. // (C) Copyright 2015 by Sebastian Büttner <sebastian.buettner@iem.thm.de>
  17.  
  18. #ifndef HELPER_EVENT_H
  19. #define HELPER_EVENT_H
  20.  
  21. #include <forward_list>
  22.  
  23. template<typename... event_arg_types>
  24. class event;
  25.  
  26. template<typename... event_arg_types>
  27. class event_handlers {
  28.     friend class event;
  29. public:
  30.     using handler_type = void()(event_arg_types...);
  31.  
  32.     event_handlers& operator+=(handler_type &&handler)
  33.     {
  34.         add(std::forward<handler_type>(handler));
  35.     }
  36.  
  37.     event_handlers& add(handler_type &&handler)
  38.     {
  39.         m_handlers.push_front(handler);
  40.     }
  41.  
  42. private:
  43.     std::forward_list<handler_type> m_handlers;
  44. };
  45.  
  46. template<typename... event_arg_types>
  47. class event {
  48. public:
  49.     void operator()(event_arg_types... event_args)
  50.     {
  51.         for (auto handler : m_handlers.m_handlers)
  52.             handler(std::forward<event_arg_types...>(event_args...));
  53.     }
  54.  
  55.     event_handlers<event_arg_types...> handlers() const
  56.     {
  57.         return m_handlers;
  58.     }
  59.  
  60. private:
  61.     event_handlers<event_arg_types...> m_handlers;
  62. };
  63.  
  64. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement