diff options
Diffstat (limited to 'linden/indra/newview/rlvevent.h')
-rw-r--r-- | linden/indra/newview/rlvevent.h | 227 |
1 files changed, 0 insertions, 227 deletions
diff --git a/linden/indra/newview/rlvevent.h b/linden/indra/newview/rlvevent.h deleted file mode 100644 index a98996f..0000000 --- a/linden/indra/newview/rlvevent.h +++ /dev/null | |||
@@ -1,227 +0,0 @@ | |||
1 | #ifndef RLV_EVENTEMITTER_H | ||
2 | #define RLV_EVENTEMITTER_H | ||
3 | |||
4 | #include <algorithm> | ||
5 | #include <typeinfo> | ||
6 | #include <list> | ||
7 | |||
8 | #include "lluuid.h" | ||
9 | |||
10 | #include "rlvhelper.h" | ||
11 | |||
12 | // ============================================================================ | ||
13 | // RlvEvent - Passed to observer event handlers (contains the same paramaters as RlvHandler::processXXXCommand) | ||
14 | // | ||
15 | |||
16 | class RlvEvent | ||
17 | { | ||
18 | public: | ||
19 | RlvEvent(const LLUUID& uuid, const RlvCommand& rlvCmd) : m_UUID(uuid), m_rlvCmd(rlvCmd) {} | ||
20 | virtual ~RlvEvent() {} | ||
21 | |||
22 | const LLUUID& getSenderID() const { return m_UUID; }; | ||
23 | const RlvCommand& getCommand() const { return m_rlvCmd; }; | ||
24 | |||
25 | protected: | ||
26 | LLUUID m_UUID; | ||
27 | RlvCommand m_rlvCmd; | ||
28 | }; | ||
29 | |||
30 | // ============================================================================ | ||
31 | /* | ||
32 | * RlvObserver | ||
33 | * =========== | ||
34 | * Provides a way to extend the existing command set without changing the main RlvHandler class | ||
35 | * | ||
36 | * Steps: | ||
37 | * - derive your class from RlvObserver | ||
38 | * - override any of the event functions you need | ||
39 | * - add it as an observer: gRlvHandler.addObserver(new RlbObserverDerivedClass()); | ||
40 | * - done | ||
41 | * | ||
42 | * Notes: | ||
43 | * - as long as you don't call gRlvHandler.remObserver() your class will be cleaned up from | ||
44 | * RlvEventEmitter destructor (see below) | ||
45 | * - event handlers are called only if RlvHandler didn't handle it so while you can | ||
46 | * add a new command @foobar=n, you won't get called for @detach=n | ||
47 | * - event handlers are called *after* the command is added so a call to | ||
48 | * RlvHandler::hasBehaviour("foobar") would return TRUE | ||
49 | * - return TRUE if you handled the command (won't get passed on to the next observer) | ||
50 | * return FALSE if you didn't handle the command (gets passed on to the next observer) | ||
51 | * | ||
52 | */ | ||
53 | |||
54 | class RlvObserver | ||
55 | { | ||
56 | public: | ||
57 | virtual ~RlvObserver() {} | ||
58 | |||
59 | typedef RlvEvent EventType; | ||
60 | |||
61 | virtual BOOL onAddCommand(const EventType& rlvEvent) { return FALSE; } | ||
62 | virtual BOOL onRemoveCommand(const EventType& rlvEvent) { return FALSE; } | ||
63 | virtual BOOL onClearCommand(const EventType& rlvEvent) { return FALSE; } | ||
64 | virtual BOOL onReplyCommand(const EventType& rlvEvent) { return FALSE; } | ||
65 | virtual BOOL onForceCommand(const EventType& rlvEvent) { return FALSE; } | ||
66 | }; | ||
67 | |||
68 | // ============================================================================ | ||
69 | /* | ||
70 | * RlvEventEmitter | ||
71 | * =============== | ||
72 | * Essentially a slightly modified eventEmitter (see lleventemitter.h) | ||
73 | * | ||
74 | * Changes: | ||
75 | * - if an event handler returns TRUE then no further observers are notified | ||
76 | * - cleans up the (remaining) observers in the destructor | ||
77 | */ | ||
78 | |||
79 | template < class T > | ||
80 | class RlvEventEmitter | ||
81 | { | ||
82 | public: | ||
83 | typedef typename T::EventType EventType; | ||
84 | typedef std::list< T* > ObserverContainer; | ||
85 | typedef BOOL ( T::*observerMethod )( const EventType& ); | ||
86 | |||
87 | protected: | ||
88 | ObserverContainer observers; | ||
89 | |||
90 | public: | ||
91 | RlvEventEmitter() { }; | ||
92 | |||
93 | ~RlvEventEmitter() | ||
94 | { | ||
95 | clearObservers(); | ||
96 | } | ||
97 | |||
98 | BOOL addObserver ( T* observerIn ) | ||
99 | { | ||
100 | if ( ! observerIn ) | ||
101 | return FALSE; | ||
102 | |||
103 | // check if observer already exists | ||
104 | if ( std::find ( observers.begin (), observers.end (), observerIn ) != observers.end () ) | ||
105 | return FALSE; | ||
106 | |||
107 | // save it | ||
108 | observers.push_back ( observerIn ); | ||
109 | |||
110 | return true; | ||
111 | } | ||
112 | |||
113 | BOOL remObserver ( T* observerIn ) | ||
114 | { | ||
115 | if ( ! observerIn ) | ||
116 | return FALSE; | ||
117 | |||
118 | observers.remove ( observerIn ); | ||
119 | |||
120 | return TRUE; | ||
121 | } | ||
122 | |||
123 | void clearObservers() | ||
124 | { | ||
125 | typename std::list< T* >::iterator iter = observers.begin (); | ||
126 | |||
127 | while (iter != observers.end ()) | ||
128 | { | ||
129 | delete *iter; | ||
130 | ++iter; | ||
131 | }; | ||
132 | |||
133 | observers.clear(); | ||
134 | } | ||
135 | |||
136 | BOOL update ( observerMethod method, const EventType& msgIn ) | ||
137 | { | ||
138 | typename std::list< T* >::iterator iter = observers.begin (); | ||
139 | |||
140 | BOOL fContinue = TRUE; | ||
141 | while ( (iter != observers.end ()) && (fContinue) ) | ||
142 | { | ||
143 | fContinue = !( ( ( *iter )->*method ) ( msgIn ) ); | ||
144 | ++iter; | ||
145 | }; | ||
146 | |||
147 | return !fContinue; | ||
148 | } | ||
149 | }; | ||
150 | |||
151 | // ============================================================================ | ||
152 | |||
153 | class RlvBehaviourObserver | ||
154 | { | ||
155 | public: | ||
156 | virtual ~RlvBehaviourObserver() {} | ||
157 | virtual void changed(const RlvCommand& rlvCmd, bool fInternal) = 0; | ||
158 | }; | ||
159 | |||
160 | // ============================================================================ | ||
161 | |||
162 | class RlvBehaviourNotifyObserver : public RlvBehaviourObserver | ||
163 | { | ||
164 | public: | ||
165 | virtual ~RlvBehaviourNotifyObserver() { } | ||
166 | |||
167 | void changed(const RlvCommand& rlvCmd, bool fInternal) | ||
168 | { | ||
169 | if (fInternal) | ||
170 | return; | ||
171 | |||
172 | std::string strCmd = rlvCmd.asString(), strNotify; ERlvParamType eCmdType = rlvCmd.getParamType(); | ||
173 | if ( (RLV_TYPE_ADD == eCmdType) || (RLV_TYPE_REMOVE == eCmdType) ) | ||
174 | strNotify = llformat("/%s=%s", strCmd.c_str(), rlvCmd.getParam().c_str()); | ||
175 | else if (RLV_TYPE_CLEAR == eCmdType) | ||
176 | strNotify = llformat("/%s", strCmd.c_str()); | ||
177 | else | ||
178 | return; | ||
179 | |||
180 | for (std::multimap<LLUUID, notifyData>::const_iterator itNotify = m_Notifications.begin(); | ||
181 | itNotify != m_Notifications.end(); ++itNotify) | ||
182 | { | ||
183 | if ( (itNotify->second.strFilter.empty()) || (std::string::npos != strCmd.find(itNotify->second.strFilter)) ) | ||
184 | rlvSendChatReply(itNotify->second.nChannel, strNotify); | ||
185 | } | ||
186 | } | ||
187 | |||
188 | void addNotify(const LLUUID& idObj, S32 nChannel, const std::string& strFilter) | ||
189 | { | ||
190 | m_Notifications.insert(std::pair<LLUUID, notifyData>(idObj, notifyData(nChannel, strFilter))); | ||
191 | } | ||
192 | |||
193 | void clearNotify(const LLUUID& idObj) | ||
194 | { | ||
195 | m_Notifications.erase(idObj); | ||
196 | } | ||
197 | |||
198 | bool hasNotify() | ||
199 | { | ||
200 | return (m_Notifications.size() != 0); | ||
201 | } | ||
202 | |||
203 | void removeNotify(const LLUUID& idObj, S32 nChannel, const std::string& strFilter) | ||
204 | { | ||
205 | for (std::multimap<LLUUID, notifyData>::iterator itNotify = m_Notifications.lower_bound(idObj), | ||
206 | endNotify = m_Notifications.upper_bound(idObj); itNotify != endNotify; ++itNotify) | ||
207 | { | ||
208 | if ( (itNotify->second.nChannel == nChannel) && (itNotify->second.strFilter == strFilter) ) | ||
209 | { | ||
210 | m_Notifications.erase(itNotify); | ||
211 | break; | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | protected: | ||
216 | struct notifyData | ||
217 | { | ||
218 | S32 nChannel; | ||
219 | std::string strFilter; | ||
220 | notifyData(S32 channel, const std::string& filter) : nChannel(channel), strFilter(filter) {} | ||
221 | }; | ||
222 | std::multimap<LLUUID, notifyData> m_Notifications; | ||
223 | }; | ||
224 | |||
225 | // ============================================================================ | ||
226 | |||
227 | #endif // RLV_EVENTEMITTER_H | ||