| 1 |
#ifndef _PROTO_EVENT |
| 2 |
#define _PROTO_EVENT |
| 3 |
|
| 4 |
#include "protoDefs.h" |
| 5 |
#include "protoNotify.h" |
| 6 |
|
| 7 |
// Manual reset event |
| 8 |
class ProtoEvent : ProtoNotify |
| 9 |
{ |
| 10 |
public: |
| 11 |
ProtoEvent(bool autoReset=true); |
| 12 |
~ProtoEvent(); |
| 13 |
|
| 14 |
bool Open(); |
| 15 |
bool Set(); |
| 16 |
bool Reset(); |
| 17 |
bool Wait(); |
| 18 |
void Close(); |
| 19 |
|
| 20 |
void SetAutoReset(bool state) |
| 21 |
{auto_reset = state;} |
| 22 |
bool GetAutoReset() const |
| 23 |
{return auto_reset;} |
| 24 |
|
| 25 |
bool IsOpen() const; |
| 26 |
|
| 27 |
class Notifier |
| 28 |
{ |
| 29 |
public: |
| 30 |
virtual ~Notifier() {} |
| 31 |
virtual bool UpdateEventNotification(ProtoEvent& theEvent, |
| 32 |
int notifyFlags) {return true;} |
| 33 |
}; |
| 34 |
bool SetNotifier(ProtoEvent::Notifier* theNotifier); |
| 35 |
Notifier* GetNotifier() const {return notifier;} |
| 36 |
|
| 37 |
// Make the event handle/descriptor available for notifications |
| 38 |
#ifdef WIN32 |
| 39 |
typedef HANDLE Descriptor; // WIN32 uses "HANDLE" type for descriptors, INVALID_HANDLE_VALUE is invalid; (although CreateEvent() returns NULL) |
| 40 |
#else |
| 41 |
typedef int Descriptor; // UNIX uses "int" type for descriptors, -1 is invalid descriptor |
| 42 |
#endif // if/else WIN32 |
| 43 |
Descriptor GetDescriptor() const |
| 44 |
{ |
| 45 |
#ifdef WIN32 |
| 46 |
return event_handle; |
| 47 |
#else |
| 48 |
#if defined(USE_EVENTFD) || defined(USE_KEVENT) |
| 49 |
return event_fd; |
| 50 |
#else |
| 51 |
return event_pipe_fd[0]; |
| 52 |
#endif // if/else USE_EVENTFD | USE_EVENTFD) / pipe |
| 53 |
#endif // if/else WIN32 / UNIX |
| 54 |
} |
| 55 |
|
| 56 |
// NOTE: For VC++ 6.0 Debug builds, you _cannot_ use pre-compiled |
| 57 |
// headers with this template code. Also, "/ZI" or "/Z7" compile options |
| 58 |
// must NOT be specified. (or else VC++ 6.0 experiences an "internal compiler error") |
| 59 |
// (Later Visual Studio versions have fixed this error) |
| 60 |
template <class listenerType> |
| 61 |
bool SetListener(listenerType* theListener, void(listenerType::*eventHandler)(ProtoEvent&)) |
| 62 |
{ |
| 63 |
bool doUpdate = ((NULL != theListener) || (NULL != listener)); |
| 64 |
if (NULL != listener) delete listener; |
| 65 |
listener = (NULL != theListener) ? new LISTENER_TYPE<listenerType>(theListener, eventHandler) : NULL; |
| 66 |
bool result = (NULL != theListener) ? (NULL != listener) : true; |
| 67 |
return result ? (doUpdate ? UpdateNotification() : true) : false; |
| 68 |
} |
| 69 |
void OnNotify(); |
| 70 |
bool HasNotifier() const |
| 71 |
{return (NULL != notifier);} |
| 72 |
bool HasListener() const |
| 73 |
{return (NULL != listener);} |
| 74 |
bool UpdateNotification(); |
| 75 |
|
| 76 |
protected: |
| 77 |
/** |
| 78 |
* @class Listener |
| 79 |
* |
| 80 |
* @brief Listens for event trigger and invokes ProtoEvent handler. |
| 81 |
*/ |
| 82 |
class Listener |
| 83 |
{ |
| 84 |
public: |
| 85 |
virtual ~Listener() {} |
| 86 |
virtual void on_event(ProtoEvent& theEvent) = 0; |
| 87 |
virtual Listener* duplicate() = 0; |
| 88 |
}; |
| 89 |
|
| 90 |
/** |
| 91 |
* @class LISTENER_TYPE |
| 92 |
* |
| 93 |
* @brief Listener template |
| 94 |
*/ |
| 95 |
template <class listenerType> |
| 96 |
class LISTENER_TYPE : public Listener |
| 97 |
{ |
| 98 |
public: |
| 99 |
LISTENER_TYPE(listenerType* theListener, |
| 100 |
void(listenerType::*eventHandler)(ProtoEvent&)) |
| 101 |
: listener(theListener), event_handler(eventHandler) {} |
| 102 |
void on_event(ProtoEvent& theEvent) |
| 103 |
{(listener->*event_handler)(theEvent);} |
| 104 |
Listener* duplicate() |
| 105 |
{return (static_cast<Listener*>(new LISTENER_TYPE<listenerType>(listener, event_handler)));} |
| 106 |
private: |
| 107 |
listenerType* listener; |
| 108 |
void(listenerType::*event_handler)(ProtoEvent&); |
| 109 |
}; |
| 110 |
|
| 111 |
private: |
| 112 |
bool auto_reset; |
| 113 |
#ifdef WIN32 |
| 114 |
HANDLE event_handle; |
| 115 |
#else // UNIX |
| 116 |
#if defined(USE_EVENTFD) || defined(USE_KEVENT) |
| 117 |
int event_fd; |
| 118 |
#else |
| 119 |
int event_pipe_fd[2]; |
| 120 |
#endif // if/else (USE_EVENTFD | USE_KEVENT) / pipe |
| 121 |
#endif // if/else WIN32/UNIX |
| 122 |
|
| 123 |
Notifier* notifier; |
| 124 |
Listener* listener; |
| 125 |
|
| 126 |
}; // end class ProtoEvent |
| 127 |
|
| 128 |
#endif // _PROTO_EVENT |