PageRenderTime 39ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llcommon/llevent.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 196 lines | 96 code | 34 blank | 66 comment | 2 complexity | f220bf772713cbdc9edb829836a96f7f MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llevent.h
  3. * @author Tom Yedwab
  4. * @brief LLEvent and LLEventListener base classes.
  5. *
  6. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  7. * Second Life Viewer Source Code
  8. * Copyright (C) 2010, Linden Research, Inc.
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation;
  13. * version 2.1 of the License only.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  25. * $/LicenseInfo$
  26. */
  27. #ifndef LL_EVENT_H
  28. #define LL_EVENT_H
  29. #include "llsd.h"
  30. #include "llpointer.h"
  31. #include "llthread.h"
  32. namespace LLOldEvents
  33. {
  34. class LLEventListener;
  35. class LLEvent;
  36. class LLEventDispatcher;
  37. class LLObservable;
  38. // Abstract event. All events derive from LLEvent
  39. class LL_COMMON_API LLEvent : public LLThreadSafeRefCount
  40. {
  41. protected:
  42. virtual ~LLEvent();
  43. public:
  44. LLEvent(LLObservable* source, const std::string& desc = "") : mSource(source), mDesc(desc) { }
  45. LLObservable* getSource() { return mSource; }
  46. virtual LLSD getValue() { return LLSD(); }
  47. // Determines whether this particular listener
  48. // should be notified of this event.
  49. // If this function returns true, handleEvent is
  50. // called on the listener with this event as the
  51. // argument.
  52. // Defaults to handling all events. Override this
  53. // if associated with an Observable with many different listeners
  54. virtual bool accept(LLEventListener* listener);
  55. // return a string describing the event
  56. virtual const std::string& desc();
  57. private:
  58. LLObservable* mSource;
  59. std::string mDesc;
  60. };
  61. // Abstract listener. All listeners derive from LLEventListener
  62. class LL_COMMON_API LLEventListener : public LLThreadSafeRefCount
  63. {
  64. protected:
  65. virtual ~LLEventListener();
  66. public:
  67. // Processes the event.
  68. // TODO: Make the return value less ambiguous?
  69. virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata) = 0;
  70. // Called when an dispatcher starts/stops listening
  71. virtual bool handleAttach(LLEventDispatcher *dispatcher) = 0;
  72. virtual bool handleDetach(LLEventDispatcher *dispatcher) = 0;
  73. };
  74. // A listener which tracks references to it and cleans up when it's deallocated
  75. class LL_COMMON_API LLSimpleListener : public LLEventListener
  76. {
  77. public:
  78. void clearDispatchers();
  79. virtual bool handleAttach(LLEventDispatcher *dispatcher);
  80. virtual bool handleDetach(LLEventDispatcher *dispatcher);
  81. protected:
  82. ~LLSimpleListener();
  83. std::vector<LLEventDispatcher *> mDispatchers;
  84. };
  85. class LLObservable; // defined below
  86. // A structure which stores a Listener and its metadata
  87. struct LLListenerEntry
  88. {
  89. LLEventListener* listener;
  90. LLSD filter;
  91. LLSD userdata;
  92. };
  93. // Base class for a dispatcher - an object which listens
  94. // to events being fired and relays them to their
  95. // appropriate destinations.
  96. class LL_COMMON_API LLEventDispatcher : public LLThreadSafeRefCount
  97. {
  98. protected:
  99. virtual ~LLEventDispatcher();
  100. public:
  101. // The default constructor creates a default simple dispatcher implementation.
  102. // The simple implementation has an array of listeners and fires every event to
  103. // all of them.
  104. LLEventDispatcher();
  105. // This dispatcher is being attached to an observable object.
  106. // If we return false, the attach fails.
  107. bool engage(LLObservable* observable);
  108. // This dispatcher is being detached from an observable object.
  109. void disengage(LLObservable* observable);
  110. // Adds a listener to this dispatcher, with a given user data
  111. // that will be passed to the listener when an event is fired.
  112. // Duplicate pointers are removed on addtion.
  113. void addListener(LLEventListener *listener, LLSD filter, const LLSD& userdata);
  114. // Removes a listener from this dispatcher
  115. void removeListener(LLEventListener *listener);
  116. // Gets a list of interested listeners
  117. std::vector<LLListenerEntry> getListeners() const;
  118. // Handle an event that has just been fired by communicating it
  119. // to listeners, passing it across a network, etc.
  120. bool fireEvent(LLPointer<LLEvent> event, LLSD filter);
  121. public:
  122. class Impl;
  123. private:
  124. Impl* impl;
  125. };
  126. // Interface for observable data (data that fires events)
  127. // In order for this class to work properly, it needs
  128. // an instance of an LLEventDispatcher to route events to their
  129. // listeners.
  130. class LL_COMMON_API LLObservable
  131. {
  132. public:
  133. // Initialize with the default Dispatcher
  134. LLObservable();
  135. virtual ~LLObservable();
  136. // Replaces the existing dispatcher pointer to the new one,
  137. // informing the dispatcher of the change.
  138. virtual bool setDispatcher(LLPointer<LLEventDispatcher> dispatcher);
  139. // Returns the current dispatcher pointer.
  140. virtual LLEventDispatcher* getDispatcher();
  141. void addListener(LLEventListener *listener, LLSD filter = "", const LLSD& userdata = "")
  142. {
  143. if (mDispatcher.notNull()) mDispatcher->addListener(listener, filter, userdata);
  144. }
  145. void removeListener(LLEventListener *listener)
  146. {
  147. if (mDispatcher.notNull()) mDispatcher->removeListener(listener);
  148. }
  149. // Notifies the dispatcher of an event being fired.
  150. void fireEvent(LLPointer<LLEvent> event, LLSD filter = LLSD());
  151. protected:
  152. LLPointer<LLEventDispatcher> mDispatcher;
  153. };
  154. class LLValueChangedEvent : public LLEvent
  155. {
  156. public:
  157. LLValueChangedEvent(LLObservable* source, LLSD value) : LLEvent(source, "value_changed"), mValue(value) { }
  158. LLSD getValue() { return mValue; }
  159. LLSD mValue;
  160. };
  161. } // LLOldEvents
  162. #endif // LL_EVENT_H