| 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 |
* The Classes of handling event |
| 19 |
***********************************************************************/ |
| 20 |
#ifndef eventH |
| 21 |
#define eventH |
| 22 |
#include <iostream> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
using namespace std; |
| 26 |
|
| 27 |
class TEventObject; |
| 28 |
class TEventDispatcher; |
| 29 |
|
| 30 |
namespace TEventType |
| 31 |
{ |
| 32 |
const int All = 1; |
| 33 |
const int FrameChanged = 2; |
| 34 |
}; |
| 35 |
|
| 36 |
struct TEventItem |
| 37 |
{ |
| 38 |
TEventObject *_object; |
| 39 |
void (TEventObject::*Handle)(int eventType, TEventDispatcher * dispatcher, void *extra); |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
class TEventItems |
| 44 |
{ |
| 45 |
int _eventType; |
| 46 |
vector<TEventItem> _items; |
| 47 |
}; |
| 48 |
|
| 49 |
class TEventTable |
| 50 |
{ |
| 51 |
protected: |
| 52 |
vector<TEventItems*> _table; |
| 53 |
|
| 54 |
public: |
| 55 |
TEventTable(); |
| 56 |
TEventTable(const TEventTable &table); |
| 57 |
~TEventTable(); |
| 58 |
|
| 59 |
void AddItem(int eventType, TEventObject *object, |
| 60 |
void (TEventObject::*Handle)(int, TEventDispatcher*, void *)); |
| 61 |
void RemoveItem(int eventType, TEventObject *object, |
| 62 |
void (TEventObject::*Handle)(int, TEventDispatcher*, void *)); |
| 63 |
void RemoveItem(int eventType, TEventObject *object); |
| 64 |
|
| 65 |
void Dispatch(int eventType, TEventDispatcher * dispatcher, void *extra); |
| 66 |
}; |
| 67 |
|
| 68 |
class TEventObject |
| 69 |
{ |
| 70 |
protected: |
| 71 |
TEventTable *_table; |
| 72 |
|
| 73 |
public: |
| 74 |
TEventObject(TEventTable *table) { _table = table;} |
| 75 |
~TEventObject() {} |
| 76 |
|
| 77 |
TEventTable *GetEventTable() { return _table;} |
| 78 |
void SetEventTable(TEventTable *table) { _table = table;} |
| 79 |
|
| 80 |
virtual void DefaultHandle(int eventType, |
| 81 |
TEventDispatcher * dispatcher, void *extra) {} |
| 82 |
|
| 83 |
void Register(int eventType, TEventObject *object, |
| 84 |
void (TEventObject::*Handle)(int, TEventDispatcher*, void *) = DefaultHandle); |
| 85 |
void UnRegister(int eventType, TEventObject *object, |
| 86 |
void (TEventObject::*Handle)(int, TEventDispatcher*, void *)); |
| 87 |
void UnRegister(int eventType, TEventObject *object); |
| 88 |
}; |
| 89 |
|
| 90 |
class TEventDispatcher |
| 91 |
{ |
| 92 |
protected: |
| 93 |
TEventTable *_table; |
| 94 |
|
| 95 |
public: |
| 96 |
TEventDispatcher(TEventTable *table) { _table = table;} |
| 97 |
~TEventDispatcher() {} |
| 98 |
|
| 99 |
Dispatch(int eventType, TEventDispatcher * dispatcher, void *extra); |
| 100 |
}; |
| 101 |
|
| 102 |
#endif |