CCNotificationCenter.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /****************************************************************************
  2. Copyright (c) 2011 Erawppa
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2013-2017 Chukong Technologies Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "deprecated/CCNotificationCenter.h"
  23. #include <string>
  24. #include "base/CCScriptSupport.h"
  25. #include "deprecated/CCArray.h"
  26. using namespace std;
  27. NS_CC_BEGIN
  28. static __NotificationCenter *s_sharedNotifCenter = nullptr;
  29. __NotificationCenter::__NotificationCenter()
  30. : _scriptHandler(0)
  31. {
  32. _observers = __Array::createWithCapacity(3);
  33. _observers->retain();
  34. }
  35. __NotificationCenter::~__NotificationCenter()
  36. {
  37. _observers->release();
  38. }
  39. __NotificationCenter *__NotificationCenter::getInstance()
  40. {
  41. if (!s_sharedNotifCenter)
  42. {
  43. s_sharedNotifCenter = new (std::nothrow) __NotificationCenter;
  44. }
  45. return s_sharedNotifCenter;
  46. }
  47. void __NotificationCenter::destroyInstance()
  48. {
  49. CC_SAFE_RELEASE_NULL(s_sharedNotifCenter);
  50. }
  51. // FIXME:: deprecated
  52. __NotificationCenter *__NotificationCenter::sharedNotificationCenter(void)
  53. {
  54. return __NotificationCenter::getInstance();
  55. }
  56. // FIXME:: deprecated
  57. void __NotificationCenter::purgeNotificationCenter(void)
  58. {
  59. __NotificationCenter::destroyInstance();
  60. }
  61. //
  62. // internal functions
  63. //
  64. bool __NotificationCenter::observerExisted(Ref *target, const std::string& name, Ref *sender)
  65. {
  66. Ref* obj = nullptr;
  67. CCARRAY_FOREACH(_observers, obj)
  68. {
  69. NotificationObserver* observer = (NotificationObserver*) obj;
  70. if (!observer)
  71. continue;
  72. if (observer->getName() == name && observer->getTarget() == target && observer->getSender() == sender)
  73. return true;
  74. }
  75. return false;
  76. }
  77. //
  78. // observer functions
  79. //
  80. void __NotificationCenter::addObserver(Ref *target,
  81. SEL_CallFuncO selector,
  82. const std::string& name,
  83. Ref *sender)
  84. {
  85. if (this->observerExisted(target, name, sender))
  86. return;
  87. NotificationObserver *observer = new (std::nothrow) NotificationObserver(target, selector, name, sender);
  88. if (!observer)
  89. return;
  90. observer->autorelease();
  91. _observers->addObject(observer);
  92. }
  93. void __NotificationCenter::removeObserver(Ref *target, const std::string& name)
  94. {
  95. Ref* obj = nullptr;
  96. CCARRAY_FOREACH(_observers, obj)
  97. {
  98. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  99. if (!observer)
  100. continue;
  101. if (observer->getName() == name && observer->getTarget() == target)
  102. {
  103. _observers->removeObject(observer);
  104. return;
  105. }
  106. }
  107. }
  108. int __NotificationCenter::removeAllObservers(Ref *target)
  109. {
  110. Ref *obj = nullptr;
  111. __Array *toRemove = __Array::create();
  112. CCARRAY_FOREACH(_observers, obj)
  113. {
  114. NotificationObserver *observer = static_cast<NotificationObserver *>(obj);
  115. if (!observer)
  116. continue;
  117. if (observer->getTarget() == target)
  118. {
  119. toRemove->addObject(observer);
  120. }
  121. }
  122. _observers->removeObjectsInArray(toRemove);
  123. return static_cast<int>(toRemove->count());
  124. }
  125. void __NotificationCenter::registerScriptObserver(Ref *target, int handler,const std::string& name)
  126. {
  127. if (this->observerExisted(target, name, nullptr))
  128. return;
  129. NotificationObserver *observer = new (std::nothrow) NotificationObserver(target, nullptr, name, nullptr);
  130. if (!observer)
  131. return;
  132. observer->setHandler(handler);
  133. observer->autorelease();
  134. _observers->addObject(observer);
  135. }
  136. void __NotificationCenter::unregisterScriptObserver(Ref *target,const std::string& name)
  137. {
  138. Ref* obj = nullptr;
  139. CCARRAY_FOREACH(_observers, obj)
  140. {
  141. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  142. if (!observer)
  143. continue;
  144. if ( observer->getName() == name && observer->getTarget() == target)
  145. {
  146. _observers->removeObject(observer);
  147. }
  148. }
  149. }
  150. void __NotificationCenter::postNotification(const std::string& name, Ref *sender)
  151. {
  152. __Array* ObserversCopy = __Array::createWithCapacity(_observers->count());
  153. ObserversCopy->addObjectsFromArray(_observers);
  154. Ref* obj = nullptr;
  155. CCARRAY_FOREACH(ObserversCopy, obj)
  156. {
  157. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  158. if (!observer)
  159. continue;
  160. if (observer->getName() == name && (observer->getSender() == sender || observer->getSender() == nullptr || sender == nullptr))
  161. {
  162. if (0 == observer->getHandler())
  163. {
  164. observer->performSelector(sender);
  165. }
  166. }
  167. }
  168. }
  169. void __NotificationCenter::postNotification(const std::string& name)
  170. {
  171. this->postNotification(name,nullptr);
  172. }
  173. int __NotificationCenter::getObserverHandlerByName(const std::string& name)
  174. {
  175. if (name.empty())
  176. {
  177. return 0;
  178. }
  179. Ref* obj = nullptr;
  180. CCARRAY_FOREACH(_observers, obj)
  181. {
  182. NotificationObserver* observer = static_cast<NotificationObserver*>(obj);
  183. if (nullptr == observer)
  184. continue;
  185. if ( observer->getName() == name )
  186. {
  187. return observer->getHandler();
  188. break;
  189. }
  190. }
  191. return 0;
  192. }
  193. ////////////////////////////////////////////////////////////////////////////////
  194. ///
  195. /// NotificationObserver
  196. ///
  197. ////////////////////////////////////////////////////////////////////////////////
  198. NotificationObserver::NotificationObserver(Ref *target,
  199. SEL_CallFuncO selector,
  200. const std::string& name,
  201. Ref *sender)
  202. {
  203. _target = target;
  204. _selector = selector;
  205. _sender = sender;
  206. _name = name;
  207. _handler = 0;
  208. }
  209. NotificationObserver::~NotificationObserver()
  210. {
  211. }
  212. void NotificationObserver::performSelector(Ref *sender)
  213. {
  214. if (_target)
  215. {
  216. if (sender) {
  217. (_target->*_selector)(sender);
  218. } else {
  219. (_target->*_selector)(_sender);
  220. }
  221. }
  222. }
  223. Ref *NotificationObserver::getTarget() const
  224. {
  225. return _target;
  226. }
  227. SEL_CallFuncO NotificationObserver::getSelector() const
  228. {
  229. return _selector;
  230. }
  231. const std::string& NotificationObserver::getName() const
  232. {
  233. return _name;
  234. }
  235. Ref* NotificationObserver::getSender() const
  236. {
  237. return _sender;
  238. }
  239. int NotificationObserver::getHandler() const
  240. {
  241. return _handler;
  242. }
  243. void NotificationObserver::setHandler(int var)
  244. {
  245. _handler = var;
  246. }
  247. NS_CC_END