PageRenderTime 39ms CodeModel.GetById 27ms app.highlight 9ms RepoModel.GetById 1ms 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
  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
 28#ifndef LL_EVENT_H
 29#define LL_EVENT_H
 30
 31#include "llsd.h"
 32#include "llpointer.h"
 33#include "llthread.h"
 34
 35namespace LLOldEvents
 36{
 37
 38class LLEventListener;
 39class LLEvent;
 40class LLEventDispatcher;
 41class LLObservable;
 42
 43// Abstract event. All events derive from LLEvent
 44class LL_COMMON_API LLEvent : public LLThreadSafeRefCount
 45{
 46protected:
 47	virtual ~LLEvent();
 48	
 49public:
 50	LLEvent(LLObservable* source, const std::string& desc = "") : mSource(source), mDesc(desc) { }
 51
 52	LLObservable* getSource() { return mSource; }
 53	virtual LLSD		getValue() { return LLSD(); }
 54	// Determines whether this particular listener
 55	//   should be notified of this event.
 56	// If this function returns true, handleEvent is
 57	//   called on the listener with this event as the
 58	//   argument.
 59	// Defaults to handling all events. Override this
 60	//   if associated with an Observable with many different listeners
 61	virtual bool accept(LLEventListener* listener);
 62
 63	// return a string describing the event
 64	virtual const std::string& desc();
 65
 66private:
 67	LLObservable* mSource;
 68	std::string mDesc;
 69};
 70
 71// Abstract listener. All listeners derive from LLEventListener
 72class LL_COMMON_API LLEventListener : public LLThreadSafeRefCount
 73{
 74protected:
 75	virtual ~LLEventListener();
 76	
 77public:
 78
 79	// Processes the event.
 80	// TODO: Make the return value less ambiguous?
 81	virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata) = 0;
 82
 83	// Called when an dispatcher starts/stops listening
 84	virtual bool handleAttach(LLEventDispatcher *dispatcher) = 0;
 85	virtual bool handleDetach(LLEventDispatcher *dispatcher) = 0;
 86};
 87
 88// A listener which tracks references to it and cleans up when it's deallocated
 89class LL_COMMON_API LLSimpleListener : public LLEventListener
 90{
 91public:
 92	void clearDispatchers();
 93	virtual bool handleAttach(LLEventDispatcher *dispatcher);
 94	virtual bool handleDetach(LLEventDispatcher *dispatcher);
 95
 96protected:
 97	~LLSimpleListener();
 98	std::vector<LLEventDispatcher *> mDispatchers;
 99};
100
101class LLObservable; // defined below
102
103// A structure which stores a Listener and its metadata
104struct LLListenerEntry
105{
106	LLEventListener* listener;
107	LLSD filter;
108	LLSD userdata;
109};
110
111// Base class for a dispatcher - an object which listens
112// to events being fired and relays them to their
113// appropriate destinations.
114class LL_COMMON_API LLEventDispatcher : public LLThreadSafeRefCount
115{
116protected:
117	virtual ~LLEventDispatcher();
118	
119public:
120	// The default constructor creates a default simple dispatcher implementation.
121	// The simple implementation has an array of listeners and fires every event to
122	// all of them.
123	LLEventDispatcher();
124	
125	// This dispatcher is being attached to an observable object.
126	// If we return false, the attach fails.
127	bool engage(LLObservable* observable);
128
129	// This dispatcher is being detached from an observable object.
130	void disengage(LLObservable* observable);
131
132	// Adds a listener to this dispatcher, with a given user data
133	// that will be passed to the listener when an event is fired.
134	// Duplicate pointers are removed on addtion.
135	void addListener(LLEventListener *listener, LLSD filter, const LLSD& userdata);
136
137	// Removes a listener from this dispatcher
138	void removeListener(LLEventListener *listener);
139
140	// Gets a list of interested listeners
141	std::vector<LLListenerEntry> getListeners() const;
142
143	// Handle an event that has just been fired by communicating it
144	// to listeners, passing it across a network, etc.
145	bool fireEvent(LLPointer<LLEvent> event, LLSD filter);
146
147public:
148	class Impl;
149private:
150	Impl* impl;
151};
152
153// Interface for observable data (data that fires events)
154// In order for this class to work properly, it needs
155// an instance of an LLEventDispatcher to route events to their
156// listeners.
157class LL_COMMON_API LLObservable
158{
159public:
160	// Initialize with the default Dispatcher
161	LLObservable();
162	virtual ~LLObservable();
163
164	// Replaces the existing dispatcher pointer to the new one,
165	// informing the dispatcher of the change.
166	virtual bool setDispatcher(LLPointer<LLEventDispatcher> dispatcher);
167
168	// Returns the current dispatcher pointer.
169	virtual LLEventDispatcher* getDispatcher();
170
171	void addListener(LLEventListener *listener, LLSD filter = "", const LLSD& userdata = "")
172	{
173		if (mDispatcher.notNull()) mDispatcher->addListener(listener, filter, userdata);
174	}
175	void removeListener(LLEventListener *listener)
176	{
177		if (mDispatcher.notNull()) mDispatcher->removeListener(listener);
178	}
179	// Notifies the dispatcher of an event being fired.
180	void fireEvent(LLPointer<LLEvent> event, LLSD filter = LLSD());
181
182protected:
183	LLPointer<LLEventDispatcher> mDispatcher;
184};
185
186class LLValueChangedEvent : public LLEvent
187{
188public:
189	LLValueChangedEvent(LLObservable* source, LLSD value) : LLEvent(source, "value_changed"), mValue(value) { }
190	LLSD getValue() { return mValue; }
191	LLSD mValue;
192};
193
194} // LLOldEvents
195
196#endif // LL_EVENT_H