tiny_signal.hpp 935 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #ifndef SOPHIAR2_TINY_SIGNAL_HPP
  2. #define SOPHIAR2_TINY_SIGNAL_HPP
  3. #include <boost/intrusive/list.hpp>
  4. #include <type_traits>
  5. template<typename... Args>
  6. class tiny_signal {
  7. public:
  8. using hook_type = boost::intrusive::list_base_hook<
  9. boost::intrusive::link_mode<
  10. boost::intrusive::auto_unlink>>;
  11. struct listener_type : public hook_type {
  12. virtual void on_signal_received(Args... args) = 0;
  13. };
  14. using list_type = boost::intrusive::list<
  15. listener_type, boost::intrusive::constant_time_size<false>>;
  16. void add_listener(listener_type &node) {
  17. if (node.is_linked()) {
  18. node.unlink();
  19. }
  20. node_list.push_back(node);
  21. }
  22. void emit_signal(Args... args) {
  23. for (auto &node: node_list) {
  24. node.on_signal_received(args...);
  25. }
  26. }
  27. private:
  28. list_type node_list;
  29. };
  30. #endif //SOPHIAR2_TINY_SIGNAL_HPP