[Tdg] More enums

Graham Wakefield wakefield at mat.ucsb.edu
Tue Feb 20 19:17:22 PST 2007


On Feb 20, 2007, at 5:17 PM, Rama Hoetzlein wrote:

> I see. That does solve one problem. Sounds like a good solution for  
> building enum lists.
>
> Wes and I found another problem related to enums.
> We have the base class called Event, which supposedly has a virtual  
> function called GetEventType.
> (remember the GetGroupType just returns a string for the group, eg.  
> Mouse, Midi)
>
> But what is the return type of this base-class function, GetEventType?
> Each group should have its own enum space, but the base class is  
> required to return only one type.
> Another way to look at this issue is, what goes into the switch on  
> a callback:

[Caveat: let me know if I'm getting the wrong end of the stick here.]

The event group and event type must be instance values, since we need  
to know what they are BEFORE we cast the Event * to a specific Event  
subclass. So, getEventType or getGroupType can be a method of the  
base class rather than virtual method.

Event :: getEventGroup() { return this->group; }
Event :: getEventType() { return this->type; }

void callback(Event * e)
{
	switch(e->getEventGroup())
	{	
		case kEventGroupMouse: {
			switch(e->getEventType())
			{
				case kEventTypeMouseDown: {
					// do shit
					break;
				}
			}
			break;
		}
	}
}

A fancy way of doing this could be to use bitmasks, with the event  
group in the upper bits and event type in the lower bits.  It would  
need some care in creating the event enums in the first place.  Then  
if you just want to look for a specific event type without the nested  
switch, you could use:

	switch(e->getEventMask())
	
		case (kEventGroupMouse & kEventTypeMouseDown):

etc.





More information about the Tdg mailing list