iMSTK
Interactive Medical Simulation Toolkit
Public Types | Public Member Functions | Protected Attributes | Friends | List of all members
imstk::EventObject Class Reference

EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed. More...

#include <imstkEventObject.h>

Inheritance diagram for imstk::EventObject:
Inheritance graph
[legend]
Collaboration diagram for imstk::EventObject:
Collaboration graph
[legend]

Public Types

using Observer = std::tuple< bool, std::weak_ptr< EventObject >, std::function< void(Event *)> >
 

Public Member Functions

template<typename T >
void postEvent (const T &e)
 Emits the event Direct observers will be immediately called, in sync Queued observers will receive the Command in their queue for later execution, reciever must implement doEvent.
 
template<typename T >
void queueEvent (const T &e)
 Queues event directly to this.
 
void doEvent ()
 Do an event, if none exists return.
 
void doAllEvents ()
 Do all the events in the event queue.
 
void foreachEvent (std::function< void(Command cmd)> func)
 Thread safe loop over all event commands, one can implement a custom handler.
 
void rforeachEvent (std::function< void(Command cmd)> func)
 thread safe reverse loop over all event commands, one can implement a custom handler
 
void clearEvents ()
 Removes all events from queue cleans up copies of the event.
 

Protected Attributes

ParallelUtils::SpinLock eventQueueLock
 
std::deque< CommandeventQueue
 
std::vector< std::pair< std::string, std::vector< Observer > > > queuedObservers
 
std::vector< std::pair< std::string, std::vector< Observer > > > directObservers
 

Friends

template<class T , class RecieverType >
void connect (std::shared_ptr< EventObject >, std::string(*)(), std::shared_ptr< RecieverType >, void(RecieverType::*)(T *))
 
template<typename T >
void connect (std::shared_ptr< EventObject >, std::string(*)(), std::function< void(T *)>)
 Direct connection for lambda functions. When sender invokes senderFunc through postEvent, the receiverFunc is called. Note: Lambda's do not contain any ids or equivalence, the only way to disconnect this event is to disconnect all receivers of a given signal. More...
 
template<typename T , class RecieverType >
void queueConnect (std::shared_ptr< EventObject >, std::string(*)(), std::shared_ptr< RecieverType >, void(RecieverType::*)(T *))
 
template<class T >
void queueConnect (std::shared_ptr< EventObject >, std::string(*)(), std::shared_ptr< EventObject >, std::function< void(T *)>)
 Queued connection for member functions. When sender invokes senderFunc through postEvent, the receiverFunc is placed in receivers queue of events. Call doAllEvents to invoke in your own time. Good for multithreaded scenarios. The difference here is you can use a lambda, that does not need to exist on the receiver object. To disconnect, disconnect all receivers from signal or clear/delete the receiver. More...
 
void disconnect (std::shared_ptr< EventObject >, std::shared_ptr< EventObject >, std::string(*)())
 Remove an observer from the sender. More...
 

Detailed Description

EventObject is the base class for all objects in iMSTK that can receive and emit events. It supports direct and queued observer functions. Direct observers receive events immediately on the same thread This can either be posted on an object or be a function pointer Queued observers receive events within their queue which they can process whenever they like. These can be connected with the connect/queuedConnect/disconnect functions Lambda recievers cannot be disconnected unless all receivers to a signal are removed.

Todo:
ThreadObject affinity

Definition at line 104 of file imstkEventObject.h.

Friends And Related Function Documentation

◆ connect

template<typename T >
void connect ( std::shared_ptr< EventObject sender,
std::string(*)()  senderFunc,
std::function< void(T *)>  receiverFunc 
)
friend

Direct connection for lambda functions. When sender invokes senderFunc through postEvent, the receiverFunc is called. Note: Lambda's do not contain any ids or equivalence, the only way to disconnect this event is to disconnect all receivers of a given signal.

Parameters
senderThe object sending the signal
senderFuncThe function called to send
receiverThe function called when it is sent

Definition at line 407 of file imstkEventObject.h.

◆ disconnect

void disconnect ( std::shared_ptr< EventObject sender,
std::shared_ptr< EventObject reciever,
std::string(*)()  senderFunc 
)
friend

Remove an observer from the sender.

Note: Direct lambda connections cannot be removed Note: Specific sender, senderFunc, receiver, receiverFunc cannot be removed because the recieverFunc cannot be id'd

Parameters
senderThe sender to remove on
receiverThe receiver to remove on
senderFuncthe signal for which all receivers will not receive

Definition at line 469 of file imstkEventObject.h.

◆ queueConnect

template<class T >
void queueConnect ( std::shared_ptr< EventObject sender,
std::string(*)()  senderFunc,
std::shared_ptr< EventObject receiver,
std::function< void(T *)>  recieverFunc 
)
friend

Queued connection for member functions. When sender invokes senderFunc through postEvent, the receiverFunc is placed in receivers queue of events. Call doAllEvents to invoke in your own time. Good for multithreaded scenarios. The difference here is you can use a lambda, that does not need to exist on the receiver object. To disconnect, disconnect all receivers from signal or clear/delete the receiver.

Parameters
senderThe object sending the signal
senderFuncThe function called to send to receivers queue
receiverThe object receiving the signal in its queue
receiverThe function called when it is invoked from queue

Definition at line 451 of file imstkEventObject.h.


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