| 1 |
/*********************************************************************** |
| 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 |
* |
| 19 |
***********************************************************************/ |
| 20 |
#include "event.h" |
| 21 |
/*********************************************************************** |
| 22 |
* Class TEventItem |
| 23 |
***********************************************************************/ |
| 24 |
void TEventItem::Clear() |
| 25 |
{ |
| 26 |
_handlers.clear(); |
| 27 |
} |
| 28 |
TEventItem::TEventItem(TEventObject *object) |
| 29 |
{ |
| 30 |
Clear(); |
| 31 |
|
| 32 |
_object = object; |
| 33 |
} |
| 34 |
|
| 35 |
TEventItem::~TEventItem() |
| 36 |
{ |
| 37 |
Clear(); |
| 38 |
} |
| 39 |
|
| 40 |
void TEventItem::AddHandler(PEventHandler handler) |
| 41 |
{ |
| 42 |
vector<PEventHandler>::iterator i; |
| 43 |
|
| 44 |
i = find(_handlers.begin(), _handlers.end(), handler); |
| 45 |
|
| 46 |
if (i == _handlers.end()) |
| 47 |
{ |
| 48 |
_handlers.push_back(handler); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
void TEventItem::RemoveHandler(PEventHandler handler) |
| 53 |
{ |
| 54 |
vector<PEventHandler>::iterator i; |
| 55 |
|
| 56 |
i = find(_handlers.begin(), _handlers.end(), handler); |
| 57 |
|
| 58 |
if (i != _handlers.end()) |
| 59 |
{ |
| 60 |
_handlers.erase(i); |
| 61 |
} |
| 62 |
|
| 63 |
} |
| 64 |
/*********************************************************************** |
| 65 |
* Class TEventItems |
| 66 |
***********************************************************************/ |
| 67 |
void TEventItems::Clear() |
| 68 |
{ |
| 69 |
for(int i=0; i<_items.size(); i++) |
| 70 |
{ |
| 71 |
if (_items[i] != NULL) |
| 72 |
{ |
| 73 |
delete _items[i]; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
_items.clear(); |
| 78 |
} |
| 79 |
|
| 80 |
TEventItems::TEventItems() |
| 81 |
{ |
| 82 |
Clear(); |
| 83 |
|
| 84 |
SetType(TEventType::Unknown); |
| 85 |
} |
| 86 |
|
| 87 |
TEventItems::TEventItems(int eventType) |
| 88 |
{ |
| 89 |
Clear(); |
| 90 |
SetType(eventType); |
| 91 |
} |
| 92 |
|
| 93 |
TEventItems::~TEventItems() |
| 94 |
{ |
| 95 |
Clear(); |
| 96 |
} |
| 97 |
|
| 98 |
void TEventItems::AddItem(TEventObject *object, PEventHandler handler) |
| 99 |
{ |
| 100 |
TEventItem * item; |
| 101 |
|
| 102 |
item = GetEventItem(object); |
| 103 |
|
| 104 |
if (item != NULL) |
| 105 |
{ |
| 106 |
item->AddHandler(handler); |
| 107 |
} |
| 108 |
else |
| 109 |
{ |
| 110 |
item = new TEventItem(object); |
| 111 |
if(item != NULL) |
| 112 |
{ |
| 113 |
item->AddHandler(handler); |
| 114 |
_items.push_back(item); |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
//Memory allocation fail |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void TEventItems::RemoveItem(TEventObject *object, PEventHandler handler) |
| 124 |
{ |
| 125 |
TEventItem * item; |
| 126 |
|
| 127 |
item = GetEventItem(object); |
| 128 |
|
| 129 |
if (item !=NULL) |
| 130 |
{ |
| 131 |
item->RemoveHandler(handler); |
| 132 |
if (item->Count() == 0) |
| 133 |
_items.erase(find(_items.begin(), _items.end(), item)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
void TEventItems::RemoveItem(TEventObject *object) |
| 138 |
{ |
| 139 |
vector<TEventItem *>::iterator i; |
| 140 |
|
| 141 |
i = find_if(_items.begin(), _items.end(), TMatchEventItem(object)); |
| 142 |
|
| 143 |
if (i != _items.end()) |
| 144 |
{ |
| 145 |
delete *i; |
| 146 |
_items.erase(i); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
TEventItem *TEventItems::GetEventItem(TEventObject *object) |
| 151 |
{ |
| 152 |
vector<TEventItem *>::iterator i; |
| 153 |
|
| 154 |
i = find_if(_items.begin(), _items.end(), TMatchEventItem(object)); |
| 155 |
|
| 156 |
if (i != _items.end()) |
| 157 |
{ |
| 158 |
return *i; |
| 159 |
} |
| 160 |
else |
| 161 |
{ |
| 162 |
return NULL; |
| 163 |
} |
| 164 |
|
| 165 |
} |
| 166 |
void TEventItems::Dispatch(TEventDispatcher *dispatcher, void *extra) |
| 167 |
{ |
| 168 |
|
| 169 |
} |
| 170 |
/*********************************************************************** |
| 171 |
* Class TEventTable |
| 172 |
***********************************************************************/ |
| 173 |
void TEventTable::Clear() |
| 174 |
{ |
| 175 |
for(int i=0; i<_table.size(); i++) |
| 176 |
{ |
| 177 |
if (_table[i] != NULL) |
| 178 |
delete _table[i]; |
| 179 |
} |
| 180 |
|
| 181 |
_table.clear(); |
| 182 |
} |
| 183 |
|
| 184 |
TEventTable::TEventTable() |
| 185 |
{ |
| 186 |
Clear(); |
| 187 |
} |
| 188 |
|
| 189 |
TEventTable::~TEventTable() |
| 190 |
{ |
| 191 |
Clear(); |
| 192 |
} |
| 193 |
|
| 194 |
void TEventTable::AddItem(int eventType, |
| 195 |
TEventObject *object, PEventHandler handler) |
| 196 |
{ |
| 197 |
TEventItems *items; |
| 198 |
|
| 199 |
items = GetEventItemsByType(eventType); |
| 200 |
|
| 201 |
|
| 202 |
if (items == NULL) |
| 203 |
{ |
| 204 |
items = new TEventItems(eventType); |
| 205 |
if (items != NULL) |
| 206 |
{ |
| 207 |
items->AddItem(object, handler); |
| 208 |
_table.push_back(items); |
| 209 |
} |
| 210 |
else |
| 211 |
{ |
| 212 |
//Error |
| 213 |
} |
| 214 |
} |
| 215 |
else |
| 216 |
{ |
| 217 |
items->AddItem(object, handler); |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
void TEventTable::RemoveItem(int eventType, |
| 223 |
TEventObject *object, PEventHandler handler) |
| 224 |
{ |
| 225 |
TEventItems *items; |
| 226 |
|
| 227 |
items = GetEventItemsByType(eventType); |
| 228 |
|
| 229 |
if (items != NULL) |
| 230 |
{ |
| 231 |
items->RemoveItem(object, handler); |
| 232 |
|
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void TEventTable::RemoveItem(int eventType, TEventObject *object) |
| 237 |
{ |
| 238 |
TEventItems *items; |
| 239 |
|
| 240 |
items = GetEventItemsByType(eventType); |
| 241 |
|
| 242 |
if (items != NULL) |
| 243 |
{ |
| 244 |
items->RemoveItem(object); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void TEventTable::RemoveItem(TEventObject *object) |
| 249 |
{ |
| 250 |
for(int i=0; i<_table.size(); i++) |
| 251 |
RemoveItem(_table[i]->_eventType, object); |
| 252 |
} |
| 253 |
|
| 254 |
TEventItems *TEventTable::GetEventItemsByType(int eventType) |
| 255 |
{ |
| 256 |
vector<TEventItems*>::iterator i; |
| 257 |
|
| 258 |
|
| 259 |
i = find_if(_table.begin(), _table.end(), TMatchEventItems(eventType)); |
| 260 |
|
| 261 |
if(i == _table.end()) |
| 262 |
{ |
| 263 |
return NULL; |
| 264 |
} |
| 265 |
else |
| 266 |
{ |
| 267 |
return *i; |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void TEventTable::Dispatch(int eventType, |
| 272 |
TEventDispatcher *dispatcher, void *extra) |
| 273 |
{ |
| 274 |
TEventItems *items; |
| 275 |
|
| 276 |
items = GetEventItemsByType(eventType); |
| 277 |
|
| 278 |
if (items != NULL) |
| 279 |
{ |
| 280 |
items->Dispatch(dispatcher, extra); |
| 281 |
} |
| 282 |
else |
| 283 |
{ |
| 284 |
return; |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
/*********************************************************************** |
| 289 |
* Class TEventObject |
| 290 |
***********************************************************************/ |
| 291 |
TEventObject::~TEventObject() |
| 292 |
{ |
| 293 |
UnRegister(this); |
| 294 |
} |
| 295 |
|
| 296 |
void TEventObject::Register(int eventType, TEventObject *object, |
| 297 |
PEventHandler handler) |
| 298 |
{ |
| 299 |
_table->AddItem(eventType, object, handler); |
| 300 |
} |
| 301 |
|
| 302 |
void TEventObject::UnRegister(int eventType, TEventObject *object, |
| 303 |
PEventHandler handler) |
| 304 |
{ |
| 305 |
_table->RemoveItem(eventType, object, handler); |
| 306 |
} |
| 307 |
|
| 308 |
void TEventObject::UnRegister(int eventType, TEventObject *object) |
| 309 |
{ |
| 310 |
_table->RemoveItem(eventType, object); |
| 311 |
} |
| 312 |
|
| 313 |
void TEventObject::UnRegister(TEventObject *object) |
| 314 |
{ |
| 315 |
_table->RemoveItem(object); |
| 316 |
} |
| 317 |
/*********************************************************************** |
| 318 |
* Class TEventDispatcher |
| 319 |
***********************************************************************/ |
| 320 |
void TEventDispatcher::Dispatch(int eventType, |
| 321 |
TEventDispatcher *dispatcher,void *extra) |
| 322 |
{ |
| 323 |
_table->Dispatch(eventType, dispatcher, extra); |
| 324 |
} |
| 325 |
|