[gnu.g++.help] Solutions to Event Schedulers in C++

hawley@stengel.ucr.edu (brian hawley) (12/12/90)

Since I was asked to post a summary to this, here goes.


I got input from several sources.  These included the news groups, and
the "Programming in C++" book by Dewhurst and Stark.  Between the ideas
given in the news groups, and the information I was able to glean from
the book, both of the problems were solved.

Problem number 1:
	Dealing with objects from an event queue.  There could possibly be
	several types of objects on this queue, and each has its own 
	class specific handler.  The problem was how to invoke the correct
	handler without actually knowing which object is being taken off
	of the queue (since the queue had to store various classes of
	objects, it had to be general).

Solution to problem number 1:
	Virtual types provided an easy and elegant solution to this problem.
	Here is an example:
		class ESNode;
	
		class EventScheduler {
			private:
				int currenttime; // in usecs
				Queue queue;  // it has a queue to store events (ESNode's)
			public:
				EventScheduler();  // a constructor
				// schedule instance for time usecs from currenttime
				schedule(ESNode *instance, int time);
				static void handler(int, int, struct sigcontext *, char *);

		};

		class ESNode {
    		protected:
        		friend EventScheduler;
        		int wakeup_time;  // the wakeup time
        		// we protect from people declaring instances of this base type
        		ESNode() {};
        		virtual void handler(void)
                                { fprintf(stderr,"error, esnode::handler\n"); }
		};

		class Timer : public ESNode {
    		private:
        		int interval;   // interval is in usecs
        		void handler(void);
    		public:
        		// interval is in usecs
        		Timer(int interval);  // The constructor, initialize timer
        		~Timer();
		};


Problem #2:
	There are several such classes of event schedulers, each driven by
	different signals and each requiring a different handler. 
	For clarity, we wanted to declare the signal handler as a member
	function.  However, member functions expect an implicit "this" 
	pointer.  The kernal does not provide such a pointer when invoking
	a signal handler.

Solution to problem #2:
	declare the member function as static.  This way the handler is
	class specific, not instance specific, and no "this" pointer
	is expected by the function.  Since there can only be one class
	of event scheduler for each signal, this solution was perfect.


I hope this helps save someone else a little time if they are doing
anything similar.

I've found that C++ has so much to it, that you don't remember everything
the first time you read a book on it...or the second, or the third...one
actually has to almost go looking for a solution to a particular problem
to find an appropriate C++ construct to handle it.  I've found the
news groups and the input from everyone who responded particularly useful
in giving me ideas on what constructs to investigate further.


Thanks,
Brian

------------------------------------------------------------------------------
Brian N. Hawley                            Internet: hawley@ucrmath.ucr.edu
Dept. of Math & Computer Science           uucp: {ucsd, uci}!ucrmath!hawley
Univ. of Calif., Riverside, CA 92521       phone: (714) 787-4645