llevent.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /**
  2. * @file llevent.h
  3. * @author Tom Yedwab
  4. * @brief LLEvent and LLEventListener base classes.
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewergpl$
  7. *
  8. * Copyright (c) 2001-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_EVENT_H
  34. #define LL_EVENT_H
  35. #include <vector>
  36. #include "llpointer.h"
  37. #include "llrefcount.h"
  38. #include "llsd.h"
  39. namespace LLOldEvents
  40. {
  41. class LLEventListener;
  42. class LLEvent;
  43. class LLEventDispatcher;
  44. class LLObservable;
  45. // Abstract event. All events derive from LLEvent
  46. class LLEvent : public LLThreadSafeRefCount
  47. {
  48. protected:
  49. virtual ~LLEvent() override = default;;
  50. public:
  51. LLEvent(LLObservable* source, const std::string& desc = "")
  52. : mSource(source),
  53. mDesc(desc)
  54. {
  55. }
  56. LL_INLINE LLObservable* getSource() { return mSource; }
  57. LL_INLINE virtual LLSD getValue() { return LLSD(); }
  58. // Determines whether this particular listener
  59. // should be notified of this event.
  60. // If this function returns true, handleEvent is
  61. // called on the listener with this event as the
  62. // argument.
  63. // Defaults to handling all events. Override this
  64. // if associated with an Observable with many different listeners
  65. virtual bool accept(LLEventListener* listenerp);
  66. // return a string describing the event
  67. virtual const std::string& desc();
  68. private:
  69. LLObservable* mSource;
  70. std::string mDesc;
  71. };
  72. // Abstract listener. All listeners derive from LLEventListener
  73. class LLEventListener : public LLThreadSafeRefCount
  74. {
  75. protected:
  76. ~LLEventListener() override = default;
  77. public:
  78. // Processes the event. *TODO: Make the return value less ambiguous ?
  79. virtual bool handleEvent(LLPointer<LLEvent> event,
  80. const LLSD& userdata) = 0;
  81. // Called when an dispatcher starts/stops listening
  82. virtual bool handleAttach(LLEventDispatcher* dispatcherp) = 0;
  83. virtual bool handleDetach(LLEventDispatcher* dispatcherp) = 0;
  84. };
  85. // A listener which tracks references to it and cleans up when it is
  86. // deallocated
  87. class LLSimpleListener : public LLEventListener
  88. {
  89. public:
  90. void clearDispatchers();
  91. bool handleAttach(LLEventDispatcher* dispatcherp) override;
  92. bool handleDetach(LLEventDispatcher* dispatcherp) override;
  93. protected:
  94. ~LLSimpleListener() override;
  95. protected:
  96. std::vector<LLEventDispatcher*> mDispatchers;
  97. };
  98. class LLObservable; // Defined below
  99. // A structure which stores a Listener and its metadata
  100. struct LLListenerEntry
  101. {
  102. LLEventListener* listener;
  103. LLSD filter;
  104. LLSD userdata;
  105. };
  106. // Base class for a dispatcher: an object which listens to events being fired
  107. // and relays them to their appropriate destinations.
  108. class LLEventDispatcher : public LLThreadSafeRefCount
  109. {
  110. protected:
  111. ~LLEventDispatcher() override;
  112. public:
  113. // The default constructor creates a default simple dispatcher
  114. // implementation. The simple implementation has an array of listeners and
  115. // fires every event to all of them.
  116. LLEventDispatcher();
  117. // This dispatcher is being attached to an observable object. If we return
  118. // false, the attach fails.
  119. bool engage(LLObservable* observablep);
  120. // This dispatcher is being detached from an observable object.
  121. void disengage(LLObservable* observablep);
  122. // Adds a listener to this dispatcher, with a given user data that will be
  123. // passed to the listener when an event is fired. Duplicate pointers are
  124. // removed on addtion.
  125. void addListener(LLEventListener* listenerp, LLSD filter,
  126. const LLSD& userdata);
  127. // Removes a listener from this dispatcher
  128. void removeListener(LLEventListener* listenerp);
  129. // Gets a list of interested listeners
  130. std::vector<LLListenerEntry> getListeners() const;
  131. // Handle an event that has just been fired by communicating it to
  132. // listeners, passing it across a network, etc.
  133. bool fireEvent(LLPointer<LLEvent> event, LLSD filter);
  134. public:
  135. class Impl;
  136. private:
  137. Impl* impl;
  138. };
  139. // Interface for observable data (data that fires events). In order for this
  140. // class to work properly, it needs an instance of an LLEventDispatcher to
  141. // route events to their listeners.
  142. class LLObservable
  143. {
  144. public:
  145. // Initialize with the default Dispatcher
  146. LLObservable();
  147. virtual ~LLObservable();
  148. // Replaces the existing dispatcher pointer to the new one,
  149. // informing the dispatcher of the change.
  150. virtual bool setDispatcher(LLPointer<LLEventDispatcher> dispatcher);
  151. // Returns the current dispatcher pointer.
  152. virtual LLEventDispatcher* getDispatcher();
  153. LL_INLINE void addListener(LLEventListener* listenerp, LLSD filter = "",
  154. const LLSD& userdata = "")
  155. {
  156. if (mDispatcher.notNull())
  157. {
  158. mDispatcher->addListener(listenerp, filter, userdata);
  159. }
  160. }
  161. LL_INLINE void removeListener(LLEventListener* listenerp)
  162. {
  163. if (mDispatcher.notNull())
  164. {
  165. mDispatcher->removeListener(listenerp);
  166. }
  167. }
  168. // Notifies the dispatcher of an event being fired.
  169. void fireEvent(LLPointer<LLEvent> event, LLSD filter = LLSD());
  170. protected:
  171. LLPointer<LLEventDispatcher> mDispatcher;
  172. };
  173. class LLValueChangedEvent : public LLEvent
  174. {
  175. public:
  176. LLValueChangedEvent(LLObservable* sourcep, LLSD value)
  177. : LLEvent(sourcep, "value_changed"),
  178. mValue(value)
  179. {
  180. }
  181. LL_INLINE LLSD getValue() { return mValue; }
  182. public:
  183. LLSD mValue;
  184. };
  185. } // LLOldEvents namespace
  186. #endif // LL_EVENT_H