M
Mark
Hi,
I'm looking for some ideas on how to build a very simple Event processing
framework in my C++ app. Here is a quick background ...
I'm building a multithreaded app in C++ (on Linux) that
uses message queues to pass pointers to Events between threads. In my app
there are simple events that can be defined using an enum (for example an
event called NETWORK_TIMEOUT) and more complex events that contain data
(for example an event called ALARM that contains a timestamp).
I've hacked together a quick solution that uses an Event class that
contains an enum identifying the type of event (ie. NETWORK_TIMEOUT,
ALARM, etc). For the more complex events that contain extra data a
subclass is created (ie. AlarmEvent) that contains the data. This
requires the thread that is receiving the event to check the enum and downcast
if necessary.
void processEvent(Event *event)
{
if(event->getEventType() == ALARM)
{
AlarmEvent *alarmEvent = static_cast<AlarmEvent *>event;
// process AlarmEvent here...
}
}
Does anyone have any suggestions on a better way to do this? The
downcasting seems a bit messy so maybe someone can suggest a more elegant
and typesafe way of doing this?
Thanks,
Mark
I'm looking for some ideas on how to build a very simple Event processing
framework in my C++ app. Here is a quick background ...
I'm building a multithreaded app in C++ (on Linux) that
uses message queues to pass pointers to Events between threads. In my app
there are simple events that can be defined using an enum (for example an
event called NETWORK_TIMEOUT) and more complex events that contain data
(for example an event called ALARM that contains a timestamp).
I've hacked together a quick solution that uses an Event class that
contains an enum identifying the type of event (ie. NETWORK_TIMEOUT,
ALARM, etc). For the more complex events that contain extra data a
subclass is created (ie. AlarmEvent) that contains the data. This
requires the thread that is receiving the event to check the enum and downcast
if necessary.
void processEvent(Event *event)
{
if(event->getEventType() == ALARM)
{
AlarmEvent *alarmEvent = static_cast<AlarmEvent *>event;
// process AlarmEvent here...
}
}
Does anyone have any suggestions on a better way to do this? The
downcasting seems a bit messy so maybe someone can suggest a more elegant
and typesafe way of doing this?
Thanks,
Mark