Sign in to follow this  
  • entries
    570
  • comments
    2427
  • views
    215907

Untitled

Sign in to follow this  
Mushu

72 views

LOL, sour syntax -

Assuming you have a listener class which is currently listening for multiple events (which wouldn't be uncommon in my code), it might look something like this -
stuct MyHappyListener :
pain::core::QuitListener,
pain::core::KeyDownListener
{
void onEvent( pain::core::QuitEvent ) {
...
}

void onEvent( pain::core::KeyDownEvent ) {
...
}
};

Now, you should already be shaking your head, to a degree, if you've been following along with my rants. Why? Because as soon as we attempt to attach this to one of our handler lists (which supports multiple lists), we immediately run into a problem:

someHandlerList.addListener( &myHappyListener );

Now, we've got an ambiguous call there, because &myHappyListener can be implicitly downcast to either a QuitListener* or a KeyDownListener*.

The quick cut-and-dry solution for this (at the moment) is to explicitly state which implicit conversion we want to use -

someHandlerList.addListener( (QuitListener*)&myHappyListener );

How's that for ugly syntax?

Now I remember thinking about these problems a long time ago, and I think the solution I had in mind was to add a layer of, yes, INDIRECTION between the handler template instances which are inherited by a class implementing more than one of them which would, in essence, look something like this:

template < UNGODLY STUFF >
class Middleman :
handler lists to inherit from
{
tempate < class EventType >
void addListener( Listener< EventType >* rah ) {
_addListener( rah ); // the old function, adds a listener.
}
};


This (I think) would allow us to change the syntax to something, IMO, a little more pleasing -

myHandlerList.addListener(&myListener);

At least it got rid of the cast.

Now, I remember Loki implemented something along the lines of this, so I'm going to dig that book back up and read through that part again. Possibly. I'm honestly not very decided on the issue at all, since there are very few handlers, and it would almost be easier to just hardcode it instead of relying on dirty template metaprogramming.

In any case, I seem to have a bloody stump where my leg should be. Doctor in the house?
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now