1 |
tim |
90 |
/********************************************************************** |
2 |
|
|
* Copyright (C) 2002-2003 by Gezelter's Group
|
3 |
|
|
*This program is free software; you can redistribute it and/or modify
|
4 |
|
|
*it under the terms of the GNU General Public License as published by
|
5 |
|
|
*the Free Software Foundation version 2 of the License.
|
6 |
|
|
*
|
7 |
|
|
*This program is distributed in the hope that it will be useful,
|
8 |
|
|
*but WITHOUT ANY WARRANTY; without even the implied warranty of
|
9 |
|
|
*MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
10 |
|
|
*GNU General Public License for more details.
|
11 |
|
|
*
|
12 |
|
|
************************************************************************ |
13 |
|
|
*Author: Teng Lin Email: tlin@nd.edu |
14 |
|
|
*Date: 08/13/2002 Version: 1.0 |
15 |
|
|
* |
16 |
|
|
************************************************************************ |
17 |
|
|
*Description: |
18 |
|
|
* The Classes of handling event |
19 |
|
|
***********************************************************************/ |
20 |
|
|
#ifndef eventH |
21 |
|
|
#define eventH |
22 |
|
|
#include <iostream> |
23 |
|
|
#include <vector> |
24 |
tim |
91 |
#include <string> |
25 |
|
|
#include <algorithm> |
26 |
|
|
#include <functional> |
27 |
tim |
92 |
//in this version, we register the event type and event object to event table. |
28 |
|
|
//Alternately, we can record the event types which event object want to response |
29 |
|
|
//by itself. I will consider to implement in latter way in next version |
30 |
tim |
90 |
|
31 |
|
|
using namespace std; |
32 |
|
|
|
33 |
|
|
class TEventObject; |
34 |
tim |
92 |
class TEvenPoster; |
35 |
tim |
90 |
|
36 |
|
|
namespace TEventType |
37 |
|
|
{ |
38 |
tim |
91 |
const int Unknown = 1; |
39 |
tim |
90 |
const int FrameChanged = 2; |
40 |
|
|
}; |
41 |
|
|
|
42 |
tim |
92 |
typedef void (TEventObject::*PEventHandler)(int, TEvenPoster *, void *) ; |
43 |
tim |
91 |
|
44 |
|
|
class TEventItem |
45 |
tim |
90 |
{ |
46 |
tim |
91 |
protected: |
47 |
|
|
TEventObject *_object; |
48 |
|
|
vector<PEventHandler> _handlers; |
49 |
|
|
|
50 |
|
|
void Clear(); |
51 |
|
|
|
52 |
|
|
public: |
53 |
|
|
TEventItem(TEventObject *object); |
54 |
|
|
~TEventItem(); |
55 |
|
|
|
56 |
|
|
TEventObject *GetEventObject() { return _object;} |
57 |
|
|
vector<PEventHandler> &GetHandlers() { return _handlers;} |
58 |
|
|
|
59 |
tim |
92 |
virtual void AddHandler(PEventHandler handler); |
60 |
|
|
virtual void RemoveHandler(PEventHandler handler); |
61 |
tim |
91 |
|
62 |
tim |
92 |
virtual void ExecuteHandler(int eventType, TEvenPoster *poster, void *extra); |
63 |
tim |
91 |
int Count() { return _handlers.size();} |
64 |
tim |
90 |
}; |
65 |
|
|
|
66 |
tim |
91 |
//functor for match TEventItem |
67 |
|
|
class TMatchEventItem : public unary_function<TEventItem, bool> |
68 |
|
|
{ |
69 |
|
|
private: |
70 |
|
|
TEventObject *_object; |
71 |
tim |
90 |
|
72 |
tim |
91 |
public: |
73 |
|
|
TMatchEventItem(TEventObject *object) |
74 |
|
|
{ |
75 |
|
|
_object = object; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
bool operator()(TEventItem *eventItem) |
79 |
|
|
{ |
80 |
|
|
return _object == eventItem->GetEventObject(); |
81 |
|
|
} |
82 |
|
|
}; |
83 |
|
|
|
84 |
tim |
90 |
class TEventItems |
85 |
|
|
{ |
86 |
tim |
91 |
protected: |
87 |
|
|
vector<TEventItem*> _items; |
88 |
|
|
|
89 |
|
|
void Clear(); |
90 |
|
|
|
91 |
|
|
public: |
92 |
|
|
int _eventType; |
93 |
|
|
|
94 |
tim |
92 |
TEventItems(int eventType = TEventType::Unknown); |
95 |
tim |
91 |
~TEventItems(); |
96 |
|
|
|
97 |
|
|
int GetType() { return _eventType;} |
98 |
|
|
void SetType(int eventType) {_eventType =eventType;} |
99 |
|
|
|
100 |
|
|
int Count() { return _items.size();} |
101 |
|
|
|
102 |
|
|
void AddItem(TEventObject *object, PEventHandler handler); |
103 |
|
|
void RemoveItem(TEventObject *object, PEventHandler handler); |
104 |
|
|
void RemoveItem(TEventObject *object); |
105 |
|
|
|
106 |
|
|
TEventItem *GetEventItem(TEventObject *object); |
107 |
|
|
|
108 |
tim |
92 |
void Dispatch(int eventType, TEvenPoster *poster, void *extra); |
109 |
tim |
90 |
}; |
110 |
|
|
|
111 |
tim |
91 |
//functor for match TEventItem |
112 |
|
|
class TMatchEventItems : public unary_function<TEventItems*, bool> |
113 |
|
|
{ |
114 |
|
|
private: |
115 |
|
|
int _eventType; |
116 |
|
|
|
117 |
|
|
public: |
118 |
|
|
TMatchEventItems(int eventType) |
119 |
|
|
{ |
120 |
|
|
|
121 |
|
|
_eventType = eventType; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
bool operator()(TEventItems *eventItems) const |
125 |
|
|
{ |
126 |
|
|
return _eventType == eventItems->GetType(); |
127 |
|
|
} |
128 |
|
|
}; |
129 |
|
|
|
130 |
tim |
90 |
class TEventTable |
131 |
|
|
{ |
132 |
|
|
protected: |
133 |
|
|
vector<TEventItems*> _table; |
134 |
|
|
|
135 |
tim |
91 |
void Clear(); |
136 |
|
|
|
137 |
tim |
90 |
public: |
138 |
|
|
TEventTable(); |
139 |
|
|
~TEventTable(); |
140 |
|
|
|
141 |
tim |
91 |
int Count() { return _table.size();} |
142 |
|
|
|
143 |
|
|
void AddItem(int eventType, TEventObject *object, PEventHandler handler); |
144 |
|
|
void RemoveItem(int eventType, TEventObject *object, PEventHandler handler); |
145 |
tim |
90 |
void RemoveItem(int eventType, TEventObject *object); |
146 |
tim |
91 |
void RemoveItem(TEventObject *object); |
147 |
tim |
90 |
|
148 |
tim |
91 |
TEventItems *GetEventItemsByType(int eventType); |
149 |
|
|
|
150 |
tim |
92 |
void Dispatch(int eventType, TEvenPoster *poster, void *extra); |
151 |
tim |
90 |
}; |
152 |
|
|
|
153 |
tim |
92 |
namespace TEventObjectType |
154 |
|
|
{ |
155 |
|
|
const int BaseEventObject = 1; |
156 |
|
|
}; |
157 |
tim |
91 |
|
158 |
tim |
90 |
class TEventObject |
159 |
|
|
{ |
160 |
|
|
protected: |
161 |
tim |
92 |
int _type; |
162 |
|
|
string _ident; |
163 |
tim |
90 |
TEventTable *_table; |
164 |
|
|
|
165 |
|
|
public: |
166 |
tim |
92 |
TEventObject(TEventTable *table = NULL) |
167 |
|
|
{ |
168 |
|
|
|
169 |
|
|
_table = table; |
170 |
|
|
} |
171 |
tim |
91 |
~TEventObject(); |
172 |
tim |
90 |
|
173 |
tim |
92 |
int GetEventObjectType() { return _type;} |
174 |
|
|
string GetIdent() { return _ident;} |
175 |
tim |
90 |
TEventTable *GetEventTable() { return _table;} |
176 |
tim |
92 |
|
177 |
|
|
void SetEventObjectType(int type) { _type = type;} |
178 |
|
|
void SetIdent(string ident) { _ident = ident;} |
179 |
tim |
90 |
void SetEventTable(TEventTable *table) { _table = table;} |
180 |
|
|
|
181 |
tim |
92 |
void DefaultHandle(int eventType,TEvenPoster *poster, void *extra) {} |
182 |
tim |
91 |
//GCC did not support default argument here |
183 |
|
|
#ifdef __GNUC__ |
184 |
tim |
90 |
void Register(int eventType, TEventObject *object, |
185 |
tim |
91 |
PEventHandler handler); |
186 |
|
|
#else |
187 |
|
|
void Register(int eventType, TEventObject *object, |
188 |
|
|
PEventHandler handler = DefaultHandle); |
189 |
|
|
#endif |
190 |
|
|
void UnRegister(int eventType, TEventObject *object, PEventHandler handler); |
191 |
tim |
90 |
void UnRegister(int eventType, TEventObject *object); |
192 |
tim |
91 |
void UnRegister(TEventObject *object); |
193 |
tim |
90 |
}; |
194 |
|
|
|
195 |
tim |
92 |
class TEvenPoster |
196 |
tim |
90 |
{ |
197 |
|
|
protected: |
198 |
|
|
TEventTable *_table; |
199 |
|
|
|
200 |
|
|
public: |
201 |
tim |
92 |
TEvenPoster(TEventTable *table = NULL) { _table = table;} |
202 |
|
|
~TEvenPoster() {} |
203 |
tim |
90 |
|
204 |
tim |
92 |
TEventTable *GetEventTable() { return _table;} |
205 |
|
|
void SetEventTable(TEventTable *table) { _table = table;} |
206 |
|
|
|
207 |
|
|
virtual void PostEvent(int eventType, TEvenPoster *poster, void *extra); |
208 |
|
|
|
209 |
tim |
90 |
}; |
210 |
|
|
|
211 |
tim |
92 |
class EventFactory |
212 |
|
|
{ |
213 |
|
|
public: |
214 |
|
|
virtual TEventItem *CreatEventItem(TEventObject *object) |
215 |
|
|
{ |
216 |
|
|
return new TEventItem(object); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
virtual TEventItems *CreatEventItems(int eventType) |
220 |
|
|
{ |
221 |
|
|
return new TEventItems(eventType); |
222 |
|
|
} |
223 |
|
|
|
224 |
|
|
virtual TEventTable *CreatEventTable() |
225 |
|
|
{ |
226 |
|
|
return new TEventTable(); |
227 |
|
|
} |
228 |
|
|
|
229 |
|
|
virtual TEventObject *CreatEventObject(TEventTable *table) |
230 |
|
|
{ |
231 |
|
|
return new TEventObject(table); |
232 |
|
|
} |
233 |
|
|
|
234 |
|
|
virtual TEvenPoster *CreatEventDispatcher(TEventTable *table) |
235 |
|
|
{ |
236 |
|
|
return new TEvenPoster(table); |
237 |
|
|
} |
238 |
|
|
}; |
239 |
|
|
|
240 |
|
|
template<class T> CallHandler(T *object, const vector<PEventHandler>& handlers, |
241 |
|
|
int eventType, TEvenPoster *poster, void *extra); |
242 |
|
|
|
243 |
tim |
90 |
#endif |