Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. // Simple template based C++ event handler.
  2. // Usage:
  3. // event<int, int, bool> my_event;
  4. // my_event.call(0, 1, false);
  5.  
  6. #pragma once
  7.  
  8. #include <functional>
  9. #include <list>
  10. #include <map>
  11. #include <memory>
  12. #include <stdexcept>
  13. #include <string>
  14.  
  15. template<typename... _ATy>
  16. class event {
  17. public:
  18. typedef std::weak_ptr<void> lifeline_t;
  19. typedef std::function<void(utility::event<_ATy...>*, lifeline_t, _ATy...)> function_t;
  20.  
  21. protected:
  22. struct pair {
  23. lifeline_t lifeline;
  24. function_t callback;
  25.  
  26. inline pair(lifeline_t _lifeline, function_t _callback)
  27. {
  28. lifeline = _lifeline;
  29. callback = _callback;
  30. }
  31.  
  32. inline bool operator==(const pair& rhs)
  33. {
  34. return (!lifeline.owner_before(rhs.lifeline) && !rhs.lifeline.owner_before(lifeline))
  35. && (lifeline.lock() == rhs.lifeline.lock())
  36. && (callback.target<_FTy>() == rhs.callback.target<_FTy>());
  37. }
  38. };
  39.  
  40. std::list<pair> _listeners;
  41.  
  42. public:
  43. event()
  44. {
  45. on.listen = std::make_shared<utility::event<_ATy...>*, lifeline_t, function_t>> ();
  46. on.silence = std::make_shared<utility::event<_ATy...>*, lifeline_t, function_t>> ();
  47. on.empty = std::make_shared<utility::event<_ATy...>*>> ();
  48. }
  49.  
  50. virtual ~event()
  51. {
  52. on.listen->empty();
  53. }
  54.  
  55. virtual void listen(lifeline_t lifeline, function_t callback)
  56. {
  57. on.listen->call(this, lifeline, callback);
  58. _listeners.emplace_back(pair{lifeline, callback});
  59. }
  60.  
  61. virtual bool silence(lifeline_t lifeline, function_t callback)
  62. {
  63. pair pr = pair{lifeline, callback};
  64. for (auto iter = _listeners.front(); iter != _listeners.end(); iter++) {
  65. if (*iter == pr) {
  66. on.silence->call(this, lifeline, callback);
  67. _listeners.erase(iter);
  68. if (empty())
  69. on.empty->call(this);
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75.  
  76. virtual size_t call(_ATy... arguments)
  77. {
  78. for (auto iter = _listeners.front(); iter != _listeners.end(); iter++) {
  79. if (iter->lifeline.expired()) {
  80. _listeners.erase(iter);
  81. continue;
  82. }
  83. iter->callback(this, iter->lifeline, arguments...);
  84. }
  85. }
  86.  
  87. virtual size_t count()
  88. {
  89. return _listeners.size();
  90. }
  91.  
  92. virtual bool empty()
  93. {
  94. return (count() == 0);
  95. }
  96.  
  97. virtual operator bool()
  98. {
  99. return (count() > 0);
  100. }
  101.  
  102. virtual void clear()
  103. {
  104. _listeners.clear();
  105. on.empty->call(this);
  106. }
  107.  
  108. public:
  109. struct {
  110. std::shared_ptr<utility::event<utility::event<_ATy...>*, lifeline_t, function_t>> listen;
  111. std::shared_ptr<utility::event<utility::event<_ATy...>*, lifeline_t, function_t>> silence;
  112. std::shared_ptr<utility::event<utility::event<_ATy...>*>> empty;
  113. } on;
  114. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement