Looking back, perhaps that example was a little to simple =) Allow me to complicate it further :lol:
This is an excerpt from my slot system. It supports up to 5 customizable signal/slot systems without having to write 5 separate classes. I'm adding in user variables by trying to reuse one of the extra template types. EventParam::null is just an empty class so that you don't have to declare all 5 template types if you don't want to.
template <class Class, class param1 = EventParam::null,
class param2 = EventParam::null,
class param3 = EventParam::null,
class param4 = EventParam::null,
class param5 = EventParam::null>
class CSlot : public CEvent<param1, param2, param3, param4, param5>
/*
Typedefs
*/
typedef void (Class::*Method) ();
typedef void (Class::*Method1) (param1);
...
/*
Aliases
*/
Class *mClass;
Method mMethod;
Method1 mMethod1;
...
/*
User Variables
*/
bool mUser;
param1 mParam1;
param2 mParam2;
...
/*
Attach
@desc: To assign the slot to a method pointer
*/
void attach (Class *classPtr, Method method) { mClass = classPtr; mMethod = method; }
void attach (Class *classPtr, Method1 method) { mClass = classPtr; mMethod1 = method; }
...
/*
Attach + User Variable
@desc: To assign the slot to a method pointer with a defined user variable
*/
void attach (Class *classPtr, Method1 method, param1 user) { mClass = classPtr; mMethod1 = method; mParam1 = user; mUser = true; }
void attach (Class *classPtr, Method2 method, param2 user) { mClass = classPtr; mMethod2 = method; mParam2 = user; mUser = true; }
...
In my engine, I have signals such as CSignal<CMouseEvent &> that would get called whenever a GUI object receives a mouse event. In my code, I need to have just one method, but maybe I would like to add a pointer to the object evoking the signal so I can perform some functionality.
void mySlot (CMouseEvent &, CGUIObject *objEvent)
{
// Do something with objEvent
}
void main ()
{
...
CSlot<CMouseEvent &, CGUIObject *> slot;
guiObject->OnMouseEvent.connect(slot, guiObject); // <- Special case, adding user variable
}
Because I'm using a reference to CMouseEvent as one of the types, the compiler will fail because mParam1 must be defined, but in this case mParam1 isn't going to be used as the user variable, mParam2 will. So unless there's a hack/trick to assign garbage to those mParam members to satisfy the compiler, I may need to write special classes to support user variable defined slots (which I'm reluctant to do) or I can remove any use of referencing in my engine to satisfy the compiler.