NoticeCenter.h 5.1 KB

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