NoticeCenter.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef SRC_UTIL_NOTICECENTER_H_
  11. #define SRC_UTIL_NOTICECENTER_H_
  12. #include <mutex>
  13. #include <memory>
  14. #include <string>
  15. #include <exception>
  16. #include <functional>
  17. #include <unordered_map>
  18. #include <stdexcept>
  19. #include "function_traits.h"
  20. namespace toolkit {
  21. class EventDispatcher {
  22. public:
  23. friend class NoticeCenter;
  24. using Ptr = std::shared_ptr<EventDispatcher>;
  25. ~EventDispatcher() = default;
  26. private:
  27. using MapType = std::unordered_multimap<void *, std::shared_ptr<void> >;
  28. EventDispatcher() = default;
  29. class InterruptException : public std::runtime_error {
  30. public:
  31. InterruptException() : std::runtime_error("InterruptException") {}
  32. ~InterruptException() {}
  33. };
  34. template<typename ...ArgsType>
  35. int emitEvent(ArgsType &&...args) {
  36. using funType = std::function<void(decltype(std::forward<ArgsType>(args))...)>;
  37. decltype(_mapListener) copy;
  38. {
  39. //先拷贝(开销比较小),目的是防止在触发回调时还是上锁状态从而导致交叉互锁
  40. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  41. copy = _mapListener;
  42. }
  43. int ret = 0;
  44. for (auto &pr : copy) {
  45. funType *obj = (funType *) (pr.second.get());
  46. try {
  47. (*obj)(std::forward<ArgsType>(args)...);
  48. ++ret;
  49. } catch (InterruptException &) {
  50. ++ret;
  51. break;
  52. }
  53. }
  54. return ret;
  55. }
  56. template<typename FUNC>
  57. void addListener(void *tag, FUNC &&func) {
  58. using funType = typename function_traits<typename std::remove_reference<FUNC>::type>::stl_function_type;
  59. std::shared_ptr<void> pListener(new funType(std::forward<FUNC>(func)), [](void *ptr) {
  60. funType *obj = (funType *) ptr;
  61. delete obj;
  62. });
  63. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  64. _mapListener.emplace(tag, pListener);
  65. }
  66. void delListener(void *tag, bool &empty) {
  67. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  68. _mapListener.erase(tag);
  69. empty = _mapListener.empty();
  70. }
  71. private:
  72. std::recursive_mutex _mtxListener;
  73. MapType _mapListener;
  74. };
  75. class NoticeCenter : public std::enable_shared_from_this<NoticeCenter> {
  76. public:
  77. using Ptr = std::shared_ptr<NoticeCenter>;
  78. static NoticeCenter &Instance();
  79. template<typename ...ArgsType>
  80. int emitEvent(const std::string &strEvent, ArgsType &&...args) {
  81. auto dispatcher = getDispatcher(strEvent);
  82. if (!dispatcher) {
  83. //该事件无人监听
  84. return 0;
  85. }
  86. return dispatcher->emitEvent(std::forward<ArgsType>(args)...);
  87. }
  88. template<typename FUNC>
  89. void addListener(void *tag, const std::string &event, FUNC &&func) {
  90. getDispatcher(event, true)->addListener(tag, std::forward<FUNC>(func));
  91. }
  92. void delListener(void *tag, const std::string &event) {
  93. auto dispatcher = getDispatcher(event);
  94. if (!dispatcher) {
  95. //不存在该事件
  96. return;
  97. }
  98. bool empty;
  99. dispatcher->delListener(tag, empty);
  100. if (empty) {
  101. delDispatcher(event, dispatcher);
  102. }
  103. }
  104. //这个方法性能比较差
  105. void delListener(void *tag) {
  106. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  107. bool empty;
  108. for (auto it = _mapListener.begin(); it != _mapListener.end();) {
  109. it->second->delListener(tag, empty);
  110. if (empty) {
  111. it = _mapListener.erase(it);
  112. continue;
  113. }
  114. ++it;
  115. }
  116. }
  117. void clearAll() {
  118. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  119. _mapListener.clear();
  120. }
  121. private:
  122. EventDispatcher::Ptr getDispatcher(const std::string &event, bool create = false) {
  123. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  124. auto it = _mapListener.find(event);
  125. if (it != _mapListener.end()) {
  126. return it->second;
  127. }
  128. if (create) {
  129. //如果为空则创建一个
  130. EventDispatcher::Ptr dispatcher(new EventDispatcher());
  131. _mapListener.emplace(event, dispatcher);
  132. return dispatcher;
  133. }
  134. return nullptr;
  135. }
  136. void delDispatcher(const std::string &event, const EventDispatcher::Ptr &dispatcher) {
  137. std::lock_guard<std::recursive_mutex> lck(_mtxListener);
  138. auto it = _mapListener.find(event);
  139. if (it != _mapListener.end() && dispatcher == it->second) {
  140. //两者相同则删除
  141. _mapListener.erase(it);
  142. }
  143. }
  144. private:
  145. std::recursive_mutex _mtxListener;
  146. std::unordered_map<std::string, EventDispatcher::Ptr> _mapListener;
  147. };
  148. } /* namespace toolkit */
  149. #endif /* SRC_UTIL_NOTICECENTER_H_ */