With event-driven programs, event dispatchers act as central control agents that poll for events from event sources, invoking event handlers as necessary. Generally, there are different types of event sources, each with its own mechanism for generating events:
- I/O event sources (sockets), which are common in networked applications.
- Timer event sources, in which operating-system timers are used to activate certain tasks of the applications.
- Signal event sources, including interruptions such as the UNIX signal generated by Ctrl-C. Some applications need to handle such signals.
- UI event sources, such as those in GUI environments (Windows and X-Windows, for instance) in which key strokes and mouse clicks generate events.
- Third-party messaging APIs that applications have to deal with.
In general, each application or application platform has its own way of dispatching events. While similar, these dispatchers aren't quite the same. Moreover, many existing event dispatchers are difficult (if not impossible) to extend at the event-source level. By design, Windows and X-Windows are UI oriented, making it awkward to plug in support of third-party messaging events. In many instances, a software-development organization keeps event-dispatching routines in a library that is shared by different projects across the organization. Supported event sources are then hard-coded in the library, making it impossible to add new event sources without modifying the library code. Finally, there appears to be a lack of generic event-dispatching mechanisms that might address some of these problems.
Here presents just such a generic event dispatcher framework that:
- Captures the commonalities found in event dispatching of normal event-driven applications.
- Is extendible at event-source level, letting you add new event sources without changing the library's code.
- Describes events, event sources, event dispatchers, and their relationships in C++ so that extending a new event source is straightforward.
The complete article of this framework (a.k.a. the GED library) was published in 'C/C++ Users Journal October, 2005' and is available here.
cd src
make
./time_event