1) How can you make the "Attach" method to be able to receive different function pointer ?? And how does the EnventManager know what parameter to send to that specific function ?
My code doesn't actually have an EventMgr().GetChannel(THROW_GRENADE_KEY) ... although I could add that, I guess, I just did it to make the example cleaner. My code looks more like:
EventHandle mGrenadeHandle = ThrowGrenadeEvent.Attach(this, &cPlayer::OnGrenadeKey);
ThrowGrenadeEvent is statically declared somewhere, and is an EventChannel, which is a templated class, so declared something like:
static EventChannel<bool> ThrowGrenadeEvent;
So, the EventChannel has a templated type which is the kind of information it carries, and has an Attach method which feeds it a pointer to an object, and a pointer to a method in the class from which that object was instantiated - it stores those pointers in a list of listeners. When you call Send on that EventChannel, it iterates through the listeners, and does this for each one:
(mObject->*mMethod)(param);
where param is the templated type (bool in this case).
In my system, you wouldn't send two bools as part of an event, you'd send a single struct that contains two bools:
struct sTwoBools
{
bool foo;
bool bar;
};
EventChannel<sTwoBools> SomeTwoBoolsEvent;
The case when you want to send nothing (void) is a bit fiddly and I had to do a template specialisation thing with that. Basically you define a specialised version of EventChannel where the templated type is void and don't pass anything into the method pointer.
2) Could you illustrate on how would you handle this with the model you presented in the other post ? Smiley
I'm not 100% sure I understand the question, I've not used those features with Boost. From what I can see from the code you posted though, you're talking about adding a callback to aCharacter to some list in aButton, which presumably requires aButton to be derived from some base class which handles that list. My system doesn't quite work that way, because it has a third class called something like aButtonEventChannel, which is an EventChannel. aCharacter registers itself with that, and aButton would call aButtonEventChannel.Send() with some int as a parameter, and it'd be aButtonEventChannel's job to notify all of the listeners. The beauty of this system is that you can totally mix and match if you want to - aButton has no particular attachment to aButtonEventChannel, doesn't know how it works or who its registered listeners are, and can just send events on that channel (or any other one) without ever caring about what's at the other end. aButtonEventChannel doesn't care who called its Send method, and although it's got a list of listeners, it doesn't even know who registered those listeners. It's possible for a class to Attach() a listener by providing an object and a method pointer to some completely different class, if it so desires.
Does that make sense?