Open Inventor Release 2023.2.3
 
Loading...
Searching...
No Matches
SbEventHandler< ArgType > Class Template Reference

Class representing an event. More...

#include <Inventor/SbEventHandler.h>

Public Member Functions

void operator() (ArgType arg)
 Call all registered callbacks.
 
void add (void(*f)(ArgType))
 Add a static function callback for this event.
 
template<typename ClassType >
void add (ClassType &a, void(ClassType::*f)(ArgType))
 Add a member function callback for this event.
 
bool remove (void(*f)(ArgType))
 Remove a static function.
 
template<typename ClassType >
bool remove (ClassType &a, void(ClassType::*f)(ArgType))
 Remove a member function.
 

Detailed Description

template<typename ArgType>
class SbEventHandler< 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:

void callback(EventArg arg)
where EventArg is the template type of SbEventHandler.

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.

EXAMPLE

    // 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);
    // 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);
    Class representing an event.
    bool remove(void(*f)(ArgType))
    Remove a static function.
    void add(void(*f)(ArgType))
    Add a static function callback for this event.

    Warning: All registered callbacks will be called exactly once, but the order is undefined.

SEE ALSO

SbEventArg

Definition at line 100 of file SbEventHandler.h.

Member Function Documentation

◆ add() [1/2]

template<typename ArgType >
template<typename ClassType >
void SbEventHandler< ArgType >::add ( ClassType &  a,
void(ClassType::*)(ArgType)  f 
)
inline

Add a member function callback for this event.

Definition at line 131 of file SbEventHandler.h.

◆ add() [2/2]

template<typename ArgType >
void SbEventHandler< ArgType >::add ( void(*)(ArgType)  f)
inline

Add a static function callback for this event.

Definition at line 123 of file SbEventHandler.h.

◆ operator()()

template<typename ArgType >
void SbEventHandler< ArgType >::operator() ( ArgType  arg)
inline

Call all registered callbacks.

Parameters
argArgument that will be given to callback.
Warning
Calling order of callbacks is undefined.

Definition at line 109 of file SbEventHandler.h.

◆ remove() [1/2]

template<typename ArgType >
template<typename ClassType >
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.

Returns
True if callback has been removed.

Definition at line 161 of file SbEventHandler.h.

◆ remove() [2/2]

template<typename ArgType >
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.

Returns
True if callback has been removed.

Definition at line 142 of file SbEventHandler.h.


The documentation for this class was generated from the following file: