Class representing an event. More...
#include <Inventor/SbEventHandler.h>
Public Member Functions | |
void | operator() (ArgType arg) |
void | add (void(*f)(ArgType)) |
template<typename ClassType > | |
void | add (ClassType &a, void(ClassType::*f)(ArgType)) |
bool | remove (void(*f)(ArgType)) |
template<typename ClassType > | |
bool | remove (ClassType &a, void(ClassType::*f)(ArgType)) |
Class representing an event.
An SbEventHandler encapsulates a list of "callbacks" or "delegates" that will be called when an event occurs. We generally think of events as input events, like mouseMove or keyboard, but they can be anything, like starting a computation or notifying that an object's state has changed.
The callback signature must be:
All SbEventHandler callbacks must return void and have a single argument of type EventArg . This argument will generally be a structure containing information about the event and possibly methods to query additional information. In OpenInventor, all EventArg will inherit from the SbEventArg class, but it is not mandatory if you want to define your own events. In any case, EventArg must not be void
Callbacks are registered using one of the add() methods. You can register a free function, a static class method or a member class method.
// a free function void freeFunction(int arg) { } // Some class class MyClass { public: // a member function void memberFunction(int arg) { } // a static function static void staticFunction(int arg) { } }; // event raised when something occurs. Prototype of callback should be void callback(int arg); SbEventHandler<int> onSomth; // To register a free or a static function, simply add it to EventHandler: onSomth.add(freeFunction); onSomth.add(MyClass::staticFunction); // Registering a member function is a little bit more complicated. // You need to specified the instance of class and a pointer to member function. // WARNING: The EventHandler will keep a reference on given instance. Be careful concerning lifetime // or this instance. If you want to delete it, don't forget to remove this callback front EventHandler // before deleting your instance. MyClass myInstance; onSomth.add(myInstance, &MyClass::memberFunction); // Raise the event. This will call all the registered callback with given argument. Here, "12". onSomth(12); // Unregister callback onSomth.remove(freeFunction); onSomth.remove(MyClass::staticFunction); onSomth.remove(myInstance, &MyClass::memberFunction);
Warning: All registered callbacks will be called exactly once, but the order is undefined.
void SbEventHandler< ArgType >::add | ( | ClassType & | a, | |
void(ClassType::*)(ArgType) | f | |||
) | [inline] |
Add a member function callback for this event.
void SbEventHandler< ArgType >::add | ( | void(*)(ArgType) | f | ) | [inline] |
Add a static function callback for this event.
void SbEventHandler< ArgType >::operator() | ( | ArgType | arg | ) | [inline] |
Call all registered callbacks.
arg | Argument that will be given to callback. |
bool SbEventHandler< ArgType >::remove | ( | ClassType & | a, | |
void(ClassType::*)(ArgType) | f | |||
) | [inline] |
Remove a member function.
If you add the same callback several times, you must remove it several times.
bool SbEventHandler< ArgType >::remove | ( | void(*)(ArgType) | f | ) | [inline] |
Remove a static function.
If you add the same callback several times, you must remove it several times.