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