llevent.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /**
  2. * @file llevent.cpp
  3. * @brief LLEvent and LLEventListener base classes.
  4. *
  5. * $LicenseInfo:firstyear=2006&license=viewergpl$
  6. *
  7. * Copyright (c) 2006-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "linden_common.h"
  33. #include "llevent.h"
  34. using namespace LLOldEvents;
  35. /************************************************
  36. Events
  37. ************************************************/
  38. // virtual
  39. bool LLEvent::accept(LLEventListener* listener)
  40. {
  41. return true;
  42. }
  43. // virtual
  44. const std::string& LLEvent::desc()
  45. {
  46. return mDesc;
  47. }
  48. /************************************************
  49. Observables
  50. ************************************************/
  51. LLObservable::LLObservable()
  52. : mDispatcher(new LLEventDispatcher())
  53. {
  54. }
  55. // virtual
  56. LLObservable::~LLObservable()
  57. {
  58. if (mDispatcher.notNull())
  59. {
  60. mDispatcher->disengage(this);
  61. mDispatcher = NULL;
  62. }
  63. }
  64. // virtual
  65. bool LLObservable::setDispatcher(LLPointer<LLEventDispatcher> dispatcher)
  66. {
  67. if (mDispatcher.notNull())
  68. {
  69. mDispatcher->disengage(this);
  70. mDispatcher = NULL;
  71. }
  72. if (dispatcher.notNull() && dispatcher->engage(this))
  73. {
  74. mDispatcher = dispatcher;
  75. return true;
  76. }
  77. return false;
  78. }
  79. // Returns the current dispatcher pointer.
  80. // virtual
  81. LLEventDispatcher* LLObservable::getDispatcher()
  82. {
  83. return mDispatcher;
  84. }
  85. // Notifies the dispatcher of an event being fired.
  86. void LLObservable::fireEvent(LLPointer<LLEvent> event, LLSD filter)
  87. {
  88. if (mDispatcher.notNull())
  89. {
  90. mDispatcher->fireEvent(event, filter);
  91. }
  92. }
  93. /************************************************
  94. Dispatchers
  95. ************************************************/
  96. class LLEventDispatcher::Impl
  97. {
  98. public:
  99. virtual ~Impl() { }
  100. virtual bool engage(LLObservable* observable) { return true; }
  101. virtual void disengage(LLObservable* observable) {}
  102. virtual void addListener(LLEventListener* listener, LLSD filter,
  103. const LLSD& userdata) = 0;
  104. virtual void removeListener(LLEventListener* listener) = 0;
  105. virtual std::vector<LLListenerEntry> getListeners() const = 0;
  106. virtual bool fireEvent(LLPointer<LLEvent> event, LLSD filter) = 0;
  107. };
  108. bool LLEventDispatcher::engage(LLObservable* observable)
  109. {
  110. return impl->engage(observable);
  111. }
  112. void LLEventDispatcher::disengage(LLObservable* observable)
  113. {
  114. impl->disengage(observable);
  115. }
  116. void LLEventDispatcher::addListener(LLEventListener* listener, LLSD filter,
  117. const LLSD& userdata)
  118. {
  119. impl->addListener(listener, filter, userdata);
  120. }
  121. void LLEventDispatcher::removeListener(LLEventListener* listener)
  122. {
  123. impl->removeListener(listener);
  124. }
  125. std::vector<LLListenerEntry> LLEventDispatcher::getListeners() const
  126. {
  127. return impl->getListeners();
  128. }
  129. bool LLEventDispatcher::fireEvent(LLPointer<LLEvent> event, LLSD filter)
  130. {
  131. return impl->fireEvent(event, filter);
  132. }
  133. class LLSimpleDispatcher : public LLEventDispatcher::Impl
  134. {
  135. public:
  136. LLSimpleDispatcher(LLEventDispatcher* parent)
  137. : mParent(parent)
  138. {
  139. }
  140. virtual ~LLSimpleDispatcher();
  141. virtual void addListener(LLEventListener* listener, LLSD filter,
  142. const LLSD& userdata);
  143. virtual void removeListener(LLEventListener* listener);
  144. virtual std::vector<LLListenerEntry> getListeners() const;
  145. virtual bool fireEvent(LLPointer<LLEvent> event, LLSD filter);
  146. protected:
  147. std::vector<LLListenerEntry> mListeners;
  148. LLEventDispatcher* mParent;
  149. };
  150. LLSimpleDispatcher::~LLSimpleDispatcher()
  151. {
  152. while (mListeners.size() > 0)
  153. {
  154. removeListener(mListeners.begin()->listener);
  155. }
  156. }
  157. void LLSimpleDispatcher::addListener(LLEventListener* listener, LLSD filter,
  158. const LLSD& userdata)
  159. {
  160. if (listener)
  161. {
  162. removeListener(listener);
  163. LLListenerEntry new_entry;
  164. new_entry.listener = listener;
  165. new_entry.filter = filter;
  166. new_entry.userdata = userdata;
  167. mListeners.push_back(new_entry);
  168. listener->handleAttach(mParent);
  169. }
  170. }
  171. void LLSimpleDispatcher::removeListener(LLEventListener* listener)
  172. {
  173. for (std::vector<LLListenerEntry>::iterator it = mListeners.begin(),
  174. end = mListeners.end();
  175. it != end; ++it)
  176. {
  177. if (it->listener == listener)
  178. {
  179. mListeners.erase(it);
  180. break;
  181. }
  182. }
  183. listener->handleDetach(mParent);
  184. }
  185. std::vector<LLListenerEntry> LLSimpleDispatcher::getListeners() const
  186. {
  187. std::vector<LLListenerEntry> ret;
  188. for (std::vector<LLListenerEntry>::const_iterator it = mListeners.begin(),
  189. end = mListeners.end();
  190. it != end; ++it)
  191. {
  192. ret.push_back(*it);
  193. }
  194. return ret;
  195. }
  196. // virtual
  197. bool LLSimpleDispatcher::fireEvent(LLPointer<LLEvent> event, LLSD filter)
  198. {
  199. std::string filter_string = filter.asString();
  200. for (std::vector<LLListenerEntry>::iterator it = mListeners.begin();
  201. it != mListeners.end(); ++it)
  202. {
  203. LLListenerEntry& entry = *it;
  204. if (filter_string.empty() || entry.filter.asString() == filter_string)
  205. {
  206. (entry.listener)->handleEvent(event, it->userdata);
  207. }
  208. }
  209. return true;
  210. }
  211. LLEventDispatcher::LLEventDispatcher()
  212. {
  213. impl = new LLSimpleDispatcher(this);
  214. }
  215. LLEventDispatcher::~LLEventDispatcher()
  216. {
  217. if (impl)
  218. {
  219. delete impl;
  220. impl = NULL;
  221. }
  222. }
  223. /************************************************
  224. Listeners
  225. ************************************************/
  226. LLSimpleListener::~LLSimpleListener()
  227. {
  228. clearDispatchers();
  229. }
  230. void LLSimpleListener::clearDispatchers()
  231. {
  232. // Remove myself from all listening dispatchers
  233. std::vector<LLEventDispatcher*>::iterator it;
  234. while (mDispatchers.size() > 0)
  235. {
  236. it = mDispatchers.begin();
  237. LLEventDispatcher* dispatcher = *it;
  238. dispatcher->removeListener(this);
  239. it = mDispatchers.begin();
  240. if (it != mDispatchers.end() && *it == dispatcher)
  241. {
  242. // Somehow, the dispatcher was not removed. Remove it forcibly
  243. mDispatchers.erase(it);
  244. }
  245. }
  246. }
  247. bool LLSimpleListener::handleAttach(LLEventDispatcher* dispatcher)
  248. {
  249. // Add dispatcher if it does not already exist
  250. for (std::vector<LLEventDispatcher*>::iterator it = mDispatchers.begin(),
  251. end = mDispatchers.end();
  252. it != end; ++it)
  253. {
  254. if (*it == dispatcher)
  255. {
  256. return true;
  257. }
  258. }
  259. mDispatchers.push_back(dispatcher);
  260. return true;
  261. }
  262. bool LLSimpleListener::handleDetach(LLEventDispatcher* dispatcher)
  263. {
  264. // Remove dispatcher from list
  265. for (std::vector<LLEventDispatcher*>::iterator it = mDispatchers.begin();
  266. it != mDispatchers.end(); )
  267. {
  268. if (*it == dispatcher)
  269. {
  270. it = mDispatchers.erase(it);
  271. }
  272. else
  273. {
  274. ++it;
  275. }
  276. }
  277. return true;
  278. }