Orion S. Lawlor (ffosl@uaf.edu)
CS 321, 2005/2/2
PROJECT1 Topic Paper

I've chosen to implement an event handling library called oslevent.  I will implement the library in C++, because I'm familiar with the language. 


My library will consist of one class called oslevent_pool.  My "events" will be user-defined C++ objects that inherit from a library-defined class called oslevent_class.  To execute an event, the library will call the event's "execute" method, which is declared virtual so users can override it.

To use the library, the user first creates an object of type oslevent_pool, normally called "oslevent".  This class represents a pool (or group) of events that are ready to execute.  The library then has only two interface calls: add and loop.

To add an event to the library, the user will call the library routine oslevent->add(event).  Events are assumed to not depend on other events--as soon as an event is added, it can be executed.

To begin executing events from the library, the user will call the library routine oslevent->loop(), which will run until no further events are available.  Any events added before the call to loop, or any events added during the processing of other events will be processed before oslevent->loop returns.

Events will execute in first-in, first-out (FIFO) order--that is, given a bunch of pending events, the oldest event will be the next one to execute. Event execution is not prioritized in this version of the library.


An example program that might use oslevent to run three tasks ("foo", "bar", and "late") might look like this.
#include "oslevent.h"
oslevent_pool *oslevent; /* library interface object (doesn't have to be global) */

class sequencer : public oslevent_class {
public:
const char *what; /* what we're doing */
int i, n; /* current step, and number of steps */
sequencer(const char *what_,int n_) {what=what_; i=0; n=n_;}

virtual void execute(void) {
i++;
printf("%d: step %d of %d\n",what,i,n); /* or real code here... */
if (i==4) oslevent->add(new sequencer("late",5); /* create new task */
if (i<n) oslevent->add(this); /* ask for another execution */
}
};

int main() {
oslevent=new oslevent_pool; /* initialize library */
sequencer s1("foo",6), s2("bar",3);
oslevent->add(&s1); /* will run s1.execute() once during loop */
oslevent->add(&s2); /* will run s2.execute() once during loop */
oslevent->loop(); /* keep processing events until there are no more */
}