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 |
90 |
|
28 |
|
|
using namespace std; |
29 |
|
|
|
30 |
|
|
class TEventObject; |
31 |
|
|
class TEventDispatcher; |
32 |
|
|
|
33 |
|
|
namespace TEventType |
34 |
|
|
{ |
35 |
tim |
91 |
const int Unknown = 1; |
36 |
tim |
90 |
const int FrameChanged = 2; |
37 |
|
|
}; |
38 |
|
|
|
39 |
tim |
91 |
//in next version, we may use map data structure to store the event items which |
40 |
|
|
//can speed up the searching. |
41 |
|
|
typedef void (TEventObject::*PEventHandler)(int, TEventDispatcher *, void *) ; |
42 |
|
|
|
43 |
|
|
class TEventItem |
44 |
tim |
90 |
{ |
45 |
tim |
91 |
protected: |
46 |
|
|
TEventObject *_object; |
47 |
|
|
vector<PEventHandler> _handlers; |
48 |
|
|
|
49 |
|
|
void Clear(); |
50 |
|
|
|
51 |
|
|
public: |
52 |
|
|
TEventItem(TEventObject *object); |
53 |
|
|
~TEventItem(); |
54 |
|
|
|
55 |
|
|
TEventObject *GetEventObject() { return _object;} |
56 |
|
|
vector<PEventHandler> &GetHandlers() { return _handlers;} |
57 |
|
|
|
58 |
|
|
void AddHandler(PEventHandler handler); |
59 |
|
|
void RemoveHandler(PEventHandler handler); |
60 |
|
|
|
61 |
|
|
int Count() { return _handlers.size();} |
62 |
tim |
90 |
}; |
63 |
|
|
|
64 |
tim |
91 |
//functor for match TEventItem |
65 |
|
|
class TMatchEventItem : public unary_function<TEventItem, bool> |
66 |
|
|
{ |
67 |
|
|
private: |
68 |
|
|
TEventObject *_object; |
69 |
tim |
90 |
|
70 |
tim |
91 |
public: |
71 |
|
|
TMatchEventItem(TEventObject *object) |
72 |
|
|
{ |
73 |
|
|
_object = object; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
bool operator()(TEventItem *eventItem) |
77 |
|
|
{ |
78 |
|
|
return _object == eventItem->GetEventObject(); |
79 |
|
|
} |
80 |
|
|
}; |
81 |
|
|
|
82 |
tim |
90 |
class TEventItems |
83 |
|
|
{ |
84 |
tim |
91 |
protected: |
85 |
|
|
vector<TEventItem*> _items; |
86 |
|
|
|
87 |
|
|
void Clear(); |
88 |
|
|
|
89 |
|
|
public: |
90 |
|
|
int _eventType; |
91 |
|
|
|
92 |
|
|
TEventItems(); |
93 |
|
|
TEventItems(int eventType); |
94 |
|
|
~TEventItems(); |
95 |
|
|
|
96 |
|
|
int GetType() { return _eventType;} |
97 |
|
|
void SetType(int eventType) {_eventType =eventType;} |
98 |
|
|
|
99 |
|
|
int Count() { return _items.size();} |
100 |
|
|
|
101 |
|
|
void AddItem(TEventObject *object, PEventHandler handler); |
102 |
|
|
void RemoveItem(TEventObject *object, PEventHandler handler); |
103 |
|
|
void RemoveItem(TEventObject *object); |
104 |
|
|
|
105 |
|
|
TEventItem *GetEventItem(TEventObject *object); |
106 |
|
|
|
107 |
|
|
void Dispatch(TEventDispatcher *dispatcher, void *extra); |
108 |
tim |
90 |
}; |
109 |
|
|
|
110 |
tim |
91 |
//functor for match TEventItem |
111 |
|
|
class TMatchEventItems : public unary_function<TEventItems*, bool> |
112 |
|
|
{ |
113 |
|
|
private: |
114 |
|
|
int _eventType; |
115 |
|
|
|
116 |
|
|
public: |
117 |
|
|
TMatchEventItems(int eventType) |
118 |
|
|
{ |
119 |
|
|
|
120 |
|
|
_eventType = eventType; |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
bool operator()(TEventItems *eventItems) const |
124 |
|
|
{ |
125 |
|
|
return _eventType == eventItems->GetType(); |
126 |
|
|
} |
127 |
|
|
}; |
128 |
|
|
|
129 |
tim |
90 |
class TEventTable |
130 |
|
|
{ |
131 |
|
|
protected: |
132 |
|
|
vector<TEventItems*> _table; |
133 |
|
|
|
134 |
tim |
91 |
void Clear(); |
135 |
|
|
|
136 |
tim |
90 |
public: |
137 |
|
|
TEventTable(); |
138 |
|
|
~TEventTable(); |
139 |
|
|
|
140 |
tim |
91 |
int Count() { return _table.size();} |
141 |
|
|
|
142 |
|
|
void AddItem(int eventType, TEventObject *object, PEventHandler handler); |
143 |
|
|
void RemoveItem(int eventType, TEventObject *object, PEventHandler handler); |
144 |
tim |
90 |
void RemoveItem(int eventType, TEventObject *object); |
145 |
tim |
91 |
void RemoveItem(TEventObject *object); |
146 |
tim |
90 |
|
147 |
tim |
91 |
TEventItems *GetEventItemsByType(int eventType); |
148 |
|
|
|
149 |
|
|
void Dispatch(int eventType, TEventDispatcher *dispatcher, void *extra); |
150 |
tim |
90 |
}; |
151 |
|
|
|
152 |
tim |
91 |
|
153 |
tim |
90 |
class TEventObject |
154 |
|
|
{ |
155 |
|
|
protected: |
156 |
|
|
TEventTable *_table; |
157 |
|
|
|
158 |
|
|
public: |
159 |
|
|
TEventObject(TEventTable *table) { _table = table;} |
160 |
tim |
91 |
~TEventObject(); |
161 |
tim |
90 |
|
162 |
|
|
TEventTable *GetEventTable() { return _table;} |
163 |
|
|
void SetEventTable(TEventTable *table) { _table = table;} |
164 |
|
|
|
165 |
tim |
91 |
void DefaultHandle(int eventType,TEventDispatcher *dispatcher, void *extra) {} |
166 |
|
|
//GCC did not support default argument here |
167 |
|
|
#ifdef __GNUC__ |
168 |
tim |
90 |
void Register(int eventType, TEventObject *object, |
169 |
tim |
91 |
PEventHandler handler); |
170 |
|
|
#else |
171 |
|
|
void Register(int eventType, TEventObject *object, |
172 |
|
|
PEventHandler handler = DefaultHandle); |
173 |
|
|
#endif |
174 |
|
|
void UnRegister(int eventType, TEventObject *object, PEventHandler handler); |
175 |
tim |
90 |
void UnRegister(int eventType, TEventObject *object); |
176 |
tim |
91 |
void UnRegister(TEventObject *object); |
177 |
tim |
90 |
}; |
178 |
|
|
|
179 |
|
|
class TEventDispatcher |
180 |
|
|
{ |
181 |
|
|
protected: |
182 |
|
|
TEventTable *_table; |
183 |
|
|
|
184 |
|
|
public: |
185 |
|
|
TEventDispatcher(TEventTable *table) { _table = table;} |
186 |
|
|
~TEventDispatcher() {} |
187 |
|
|
|
188 |
tim |
91 |
void Dispatch(int eventType, TEventDispatcher *dispatcher, void *extra); |
189 |
tim |
90 |
}; |
190 |
|
|
|
191 |
|
|
#endif |