llevent.h

Go to the documentation of this file.
00001 
00033 #ifndef LL_EVENT_H
00034 #define LL_EVENT_H
00035 
00036 #include "llsd.h"
00037 #include "llmemory.h"
00038 #include "llthread.h"
00039 
00040 class LLEventListener;
00041 class LLEvent;
00042 class LLEventDispatcher;
00043 class LLObservable;
00044 
00045 // Abstract event. All events derive from LLEvent
00046 class LLEvent : public LLThreadSafeRefCount
00047 {
00048 protected:
00049         virtual ~LLEvent();
00050         
00051 public:
00052         LLEvent(LLObservable* source, const std::string& desc = "") : mSource(source), mDesc(desc) { }
00053 
00054         LLObservable* getSource() { return mSource; }
00055         virtual LLSD            getValue() { return LLSD(); }
00056         // Determines whether this particular listener
00057         //   should be notified of this event.
00058         // If this function returns true, handleEvent is
00059         //   called on the listener with this event as the
00060         //   argument.
00061         // Defaults to handling all events. Override this
00062         //   if associated with an Observable with many different listeners
00063         virtual bool accept(LLEventListener* listener);
00064 
00065         // return a string describing the event
00066         virtual const std::string& desc();
00067 
00068 private:
00069         LLObservable* mSource;
00070         std::string mDesc;
00071 };
00072 
00073 // Abstract listener. All listeners derive from LLEventListener
00074 class LLEventListener : public LLThreadSafeRefCount
00075 {
00076 protected:
00077         virtual ~LLEventListener();
00078         
00079 public:
00080 
00081         // Processes the event.
00082         // TODO: Make the return value less ambiguous?
00083         virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata) = 0;
00084 
00085         // Called when an dispatcher starts/stops listening
00086         virtual bool handleAttach(LLEventDispatcher *dispatcher) = 0;
00087         virtual bool handleDetach(LLEventDispatcher *dispatcher) = 0;
00088 };
00089 
00090 // A listener which tracks references to it and cleans up when it's deallocated
00091 class LLSimpleListener : public LLEventListener
00092 {
00093 public:
00094         void clearDispatchers();
00095         virtual bool handleAttach(LLEventDispatcher *dispatcher);
00096         virtual bool handleDetach(LLEventDispatcher *dispatcher);
00097 
00098 protected:
00099         ~LLSimpleListener();
00100         std::vector<LLEventDispatcher *> mDispatchers;
00101 };
00102 
00103 class LLObservable; // defined below
00104 
00105 // A structure which stores a Listener and its metadata
00106 struct LLListenerEntry
00107 {
00108         LLEventListener* listener;
00109         LLSD filter;
00110         LLSD userdata;
00111 };
00112 
00113 // Base class for a dispatcher - an object which listens
00114 // to events being fired and relays them to their
00115 // appropriate destinations.
00116 class LLEventDispatcher : public LLThreadSafeRefCount
00117 {
00118 protected:
00119         virtual ~LLEventDispatcher();
00120         
00121 public:
00122         // The default constructor creates a default simple dispatcher implementation.
00123         // The simple implementation has an array of listeners and fires every event to
00124         // all of them.
00125         LLEventDispatcher();
00126         
00127         // This dispatcher is being attached to an observable object.
00128         // If we return false, the attach fails.
00129         bool engage(LLObservable* observable);
00130 
00131         // This dispatcher is being detached from an observable object.
00132         void disengage(LLObservable* observable);
00133 
00134         // Adds a listener to this dispatcher, with a given user data
00135         // that will be passed to the listener when an event is fired.
00136         // Duplicate pointers are removed on addtion.
00137         void addListener(LLEventListener *listener, LLSD filter, const LLSD& userdata);
00138 
00139         // Removes a listener from this dispatcher
00140         void removeListener(LLEventListener *listener);
00141 
00142         // Gets a list of interested listeners
00143         std::vector<LLListenerEntry> getListeners() const;
00144 
00145         // Handle an event that has just been fired by communicating it
00146         // to listeners, passing it across a network, etc.
00147         bool fireEvent(LLPointer<LLEvent> event, LLSD filter);
00148 
00149 public:
00150         class Impl;
00151 private:
00152         Impl* impl;
00153 };
00154 
00155 // Interface for observable data (data that fires events)
00156 // In order for this class to work properly, it needs
00157 // an instance of an LLEventDispatcher to route events to their
00158 // listeners.
00159 class LLObservable
00160 {
00161 public:
00162         // Initialize with the default Dispatcher
00163         LLObservable();
00164         virtual ~LLObservable();
00165 
00166         // Replaces the existing dispatcher pointer to the new one,
00167         // informing the dispatcher of the change.
00168         virtual bool setDispatcher(LLPointer<LLEventDispatcher> dispatcher);
00169 
00170         // Returns the current dispatcher pointer.
00171         virtual LLEventDispatcher* getDispatcher();
00172 
00173         void addListener(LLEventListener *listener, LLSD filter = "", const LLSD& userdata = "")
00174         {
00175                 if (mDispatcher.notNull()) mDispatcher->addListener(listener, filter, userdata);
00176         }
00177         void removeListener(LLEventListener *listener)
00178         {
00179                 if (mDispatcher.notNull()) mDispatcher->removeListener(listener);
00180         }
00181         // Notifies the dispatcher of an event being fired.
00182         void fireEvent(LLPointer<LLEvent> event, LLSD filter = LLSD());
00183 
00184 protected:
00185         LLPointer<LLEventDispatcher> mDispatcher;
00186 };
00187 
00188 class LLValueChangedEvent : public LLEvent
00189 {
00190 public:
00191         LLValueChangedEvent(LLObservable* source, LLSD value) : LLEvent(source, "value_changed"), mValue(value) { }
00192         LLSD getValue() { return mValue; }
00193         LLSD mValue;
00194 };
00195 
00196 #endif // LL_EVENT_H

Generated on Fri May 16 08:32:03 2008 for SecondLife by  doxygen 1.5.5