Public Member Functions | Protected Member Functions | Friends | List of all members
UEventsHandler Class Referenceabstract

#include <UEventsHandler.h>

Inheritance diagram for UEventsHandler:
Inheritance graph
[legend]

Public Member Functions

void registerToEventsManager ()
 
void unregisterFromEventsManager ()
 
- Public Member Functions inherited from UEventsSender
 UEventsSender ()
 
virtual ~UEventsSender ()
 

Protected Member Functions

virtual bool handleEvent (UEvent *event)=0
 
 UEventsHandler ()
 
virtual ~UEventsHandler ()
 
- Protected Member Functions inherited from UEventsSender
void post (UEvent *event, bool async=true) const
 

Friends

class UEventsManager
 

Detailed Description

The class UEventsHandler is an abstract class for handling events.

Inherited classes must implement handleEvent() function, which is called by the UEventsManager when an event is dispatched. Once the handler is created, it must be added to events manager with UEventsManager::addHandler() function. Note that it is not safe to automatically add the handler to UEventsManager in the handler's constructor.

Note for multi-threading: the handleEvent() method is called inside the UEventsManager thread. If the inherited class also inherits from UThreadNode, handleEvent() is done as well outside the thread's main loop, so be careful to protect private data of the thread used in its main loop.

Example for a useful combination of an UEventsHandler and a UThreadNode, with safe data modification while not blocking the handleEvent() call on a mutex:

#include "utilite/UEvent.h"
// Implement a simple event
class ResetEvent : public UEvent {
public:
ResetEvent() {}
virtual ~ResetEvent() {}
virtual std::string getClassName() const {return "ResetEvent";} // Must be implemented
};
// There is the thread counting indefinitely, the count can be reseted by sending a ResetEvent.
class CounterThread : public UThreadNode, public UEventsHandler {
public:
CounterThread() : state_(0), count_(0) {}
virtual ~CounterThread() {this->join(true);}
protected:
virtual void mainLoop() {
if(state_ == 1) {
state_ = 0;
// Do a long initialization, reset memory or other special long works... here
// we reset the count. This could be done in the handleEvent() but
// with many objects, it is more safe to do it here (in the thread's loop). A safe
// way could be also to use a UMutex to protect this initialization in
// the handleEvent(), but it is not recommended to do long works in handleEvent()
// because this will add latency in the UEventsManager dispatching events loop.
count_ = 0; // Reset the count
printf("Reset!\n");
}
// Do some works...
printf("count=%d\n", count_++);
uSleep(100); // wait 100 ms
}
virtual void handleEvent(UEvent * event) {
if(event->getClassName().compare("ResetEvent") == 0) {
state_ = 1;
}
}
private:
int state_;
int count_;
};
int main(int argc, char * argv[])
{
CounterThread counter;
counter.start();
uSleep(500); // wait 500 ms before sending a reset event
UEventsManager::post(new ResetEvent());
uSleep(500); // wait 500 ms before termination
counter.join(true); // Kill and wait to finish
return 0;
}

The output is:

count=0
count=1
count=2
count=3
count=4
Reset!
count=0
count=1
count=2
count=3
count=4
See also
UEventsManager
UEvent
UThreadNode

Definition at line 128 of file UEventsHandler.h.

Constructor & Destructor Documentation

◆ UEventsHandler()

UEventsHandler::UEventsHandler ( )
inlineprotected

UEventsHandler constructor.

Note : You can call EventsManager::addHandler(this) at the end of the constructor of the inherited class where the virtual method handleEvent(...) is defined. If so, the UEventsHandler doesn't need to be manually added to the EventsManager where the handler is instantiated. We decided to not include UEventsManager::addHandler(this) in this abstract class constructor because an event can be handled (calling the pure virtual method) while the concrete class is constructed.

Definition at line 172 of file UEventsHandler.h.

◆ ~UEventsHandler()

UEventsHandler::~UEventsHandler ( )
protectedvirtual

UEventsHandler destructor.

By default, it removes the handler reference from the UEventsManager. To be thread-safe, the inherited class must remove itself from the UEventsManager before it is deleted because an event can be handled (calling the pure virtual method handleEvent()) after the concrete class is deleted.

Definition at line 23 of file UEventsHandler.cpp.

Member Function Documentation

◆ handleEvent()

virtual bool UEventsHandler::handleEvent ( UEvent event)
protectedpure virtual

Method called by the UEventsManager to handle an event. Important : this method must do a minimum of work because the faster the dispatching loop is done; the faster the events are received. If a handling function takes too much time, the events list can grow faster than it is emptied. The event can be modified.

Returns
"true" to notify UEventsManager that this handler took ownership of the event (meaning it must delete it). The event will not be dispatched to next handlers.
"false" to let event be dispatched to next handlers (default behavior). UEventsManager will take care of deleting the event.

Implemented in RTABMapApp, rtabmap::MainWindow, rtabmap::CalibrationDialog, rtabmap::ProgressionStatus, rtabmap::RtabmapThread, rtabmap::OdometryViewer, rtabmap::ConsoleWidget, rtabmap::DataRecorder, rtabmap::CameraViewer, rtabmap::LogHandler, and rtabmap::OdometryThread.

◆ registerToEventsManager()

void UEventsHandler::registerToEventsManager ( )

Definition at line 29 of file UEventsHandler.cpp.

◆ unregisterFromEventsManager()

void UEventsHandler::unregisterFromEventsManager ( )

Definition at line 33 of file UEventsHandler.cpp.

Friends And Related Function Documentation

◆ UEventsManager

friend class UEventsManager
friend

Only the UEventsManager has access to the handleEvent() method.

Definition at line 140 of file UEventsHandler.h.


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


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:59