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
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
00057
00058
00059
00060
00061
00062
00063 virtual bool accept(LLEventListener* listener);
00064
00065
00066 virtual const std::string& desc();
00067
00068 private:
00069 LLObservable* mSource;
00070 std::string mDesc;
00071 };
00072
00073
00074 class LLEventListener : public LLThreadSafeRefCount
00075 {
00076 protected:
00077 virtual ~LLEventListener();
00078
00079 public:
00080
00081
00082
00083 virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata) = 0;
00084
00085
00086 virtual bool handleAttach(LLEventDispatcher *dispatcher) = 0;
00087 virtual bool handleDetach(LLEventDispatcher *dispatcher) = 0;
00088 };
00089
00090
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;
00104
00105
00106 struct LLListenerEntry
00107 {
00108 LLEventListener* listener;
00109 LLSD filter;
00110 LLSD userdata;
00111 };
00112
00113
00114
00115
00116 class LLEventDispatcher : public LLThreadSafeRefCount
00117 {
00118 protected:
00119 virtual ~LLEventDispatcher();
00120
00121 public:
00122
00123
00124
00125 LLEventDispatcher();
00126
00127
00128
00129 bool engage(LLObservable* observable);
00130
00131
00132 void disengage(LLObservable* observable);
00133
00134
00135
00136
00137 void addListener(LLEventListener *listener, LLSD filter, const LLSD& userdata);
00138
00139
00140 void removeListener(LLEventListener *listener);
00141
00142
00143 std::vector<LLListenerEntry> getListeners() const;
00144
00145
00146
00147 bool fireEvent(LLPointer<LLEvent> event, LLSD filter);
00148
00149 public:
00150 class Impl;
00151 private:
00152 Impl* impl;
00153 };
00154
00155
00156
00157
00158
00159 class LLObservable
00160 {
00161 public:
00162
00163 LLObservable();
00164 virtual ~LLObservable();
00165
00166
00167
00168 virtual bool setDispatcher(LLPointer<LLEventDispatcher> dispatcher);
00169
00170
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
00182 void fireEvent(LLPointer<LLEvent> event, LLSD filter);
00183
00184 protected:
00185 LLPointer<LLEventDispatcher> mDispatcher;
00186 };
00187
00188
00189 class LLSimpleListenerObservable : public LLObservable, public LLSimpleListener
00190 {
00191 public:
00192 virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata) = 0;
00193 };
00194
00195 class LLValueChangedEvent : public LLEvent
00196 {
00197 public:
00198 LLValueChangedEvent(LLObservable* source, LLSD value) : LLEvent(source, "value_changed"), mValue(value) { }
00199 LLSD getValue() { return mValue; }
00200 LLSD mValue;
00201 };
00202
00203 #endif // LL_EVENT_H