RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UEventsManager.h
1/*
2* utilite is a cross-platform library with
3* useful utilities for fast and small developing.
4* Copyright (C) 2010 Mathieu Labbe
5*
6* utilite is free library: you can redistribute it and/or modify
7* it under the terms of the GNU Lesser General Public License as published by
8* the Free Software Foundation, either version 3 of the License, or
9* (at your option) any later version.
10*
11* utilite is distributed in the hope that it will be useful,
12* but WITHOUT ANY WARRANTY; without even the implied warranty of
13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14* GNU Lesser General Public License for more details.
15*
16* You should have received a copy of the GNU Lesser General Public License
17* along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef UEVENTSMANAGER_H
21#define UEVENTSMANAGER_H
22
23#include "rtabmap/utilite/utilite_export.h" // DLL export/import defines
24
25#include "rtabmap/utilite/UEventsHandler.h"
28#include "rtabmap/utilite/UDestroyer.h"
29
30#include <list>
31#include <map>
32
33// TODO Not implemented... for multithreading event handling
35{
36public:
37 virtual ~UEventDispatcher();
38protected:
39 friend class UEventsManager;
41
42 virtual void mainLoop();
43
44private:
45 virtual void killCleanup();
46
47private:
48 UEvent * _event;
49 std::vector<UEventsHandler*> _handlers;
50};
51
78class UTILITE_EXPORT UEventsManager : public UThread{
79
80public:
81
88 static void addHandler(UEventsHandler* handler);
89
96 static void removeHandler(UEventsHandler* handler);
97
110 static void post(UEvent * event, bool async = true, const UEventsSender * sender = 0);
111
112 static void createPipe(
113 const UEventsSender * sender,
114 const UEventsHandler * receiver,
115 const std::string & eventName);
116
117 static void removePipe(
118 const UEventsSender * sender,
119 const UEventsHandler * receiver,
120 const std::string & eventName);
121
122 static void removeAllPipes(const UEventsSender * sender);
123 static void removeNullPipes(const UEventsSender * sender);
124
125protected:
126
127 /*
128 * This method is used to have a reference on the
129 * EventsManager. When no EventsManager exists, one is
130 * created. There is only one instance in the application.
131 * See the Singleton pattern further explanation.
132 *
133 * @return the reference on the EventsManager
134 */
135 static UEventsManager* getInstance();
136
137 /*
138 * Called only once in getInstance(). It can't be instantiated
139 * by the user.
140 *
141 */
143
144 /*
145 * Only called by a Destroyer.
146 */
147 virtual ~UEventsManager();
148
149 /*
150 * A Destroyer is used to remove a dynamically created
151 * Singleton. It is friend here to have access to the
152 * destructor.
153 *
154 */
155 friend class UDestroyer<UEventsManager>;
156
160 virtual void mainLoop();
161
162private:
163
167 virtual void mainLoopKill();
168
169 /*
170 * This method dispatches asynchronized events to all handlers.
171 * FIFO (first in first out) dispatching is used.
172 */
173 virtual void dispatchEvents();
174
175 /*
176 * This method dispatches an event to all handlers.
177 */
178 virtual bool dispatchEvent(UEvent * event, const UEventsSender * sender);
179
180 /*
181 * This method is used to add an events
182 * handler to the list of handlers.
183 *
184 * @param handler the handler to be added.
185 */
186 void _addHandler(UEventsHandler* handler);
187
188 /*
189 * This method is used to remove an events
190 * handler from the list of handlers.
191 *
192 * @param handler the handler to be removed.
193 */
194 void _removeHandler(UEventsHandler* handler);
195
196 /*
197 * This method is used to post an event to
198 * handlers.
199 *
200 * Event can be posted asynchronously or not. In the first case,
201 * the event is dispatched by the UEventsManager's thread. In the
202 * second case, the event is handled immediately by event's
203 * receivers, thus in the sender thread.
204 *
205 * @param event the event to be posted.
206 * @param async if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous).
207 */
208 void _postEvent(UEvent * event, bool async = true, const UEventsSender * sender = 0);
209
210 std::list<UEventsHandler*> getPipes(
211 const UEventsSender * sender,
212 const std::string & eventName);
213
214 void _createPipe(
215 const UEventsSender * sender,
216 const UEventsHandler * receiver,
217 const std::string & eventName);
218
219 void _removePipe(
220 const UEventsSender * sender,
221 const UEventsHandler * receiver,
222 const std::string & eventName);
223
224 void _removeAllPipes(const UEventsSender * sender);
225 void _removeNullPipes(const UEventsSender * sender);
226
227private:
228
229 class Pipe
230 {
231 public:
232 Pipe(const UEventsSender * sender, const UEventsHandler * receiver, const std::string & eventName) :
233 sender_(sender),
234 receiver_(receiver),
235 eventName_(eventName)
236 {}
237 const UEventsSender * sender_;
238 const UEventsHandler * receiver_;
239 const std::string eventName_;
240 };
241
242 static UEventsManager* instance_; /* The EventsManager instance pointer. */
243 static UDestroyer<UEventsManager> destroyer_; /* The EventsManager's destroyer. */
244 std::list<std::pair<UEvent*, const UEventsSender * > > events_; /* The events list. */
245 std::list<UEventsHandler*> handlers_; /* The handlers list. */
246 UMutex eventsMutex_; /* The mutex of the events list, */
247 UMutex handlersMutex_; /* The mutex of the handlers list. */
248 USemaphore postEventSem_; /* Semaphore used to signal when an events is posted. */
249 std::list<Pipe> pipes_;
250 UMutex pipesMutex_;
251};
252
253#endif // UEVENTSMANAGER_H
ULogger class and convenient macros.
Backward-compatible alias header for UThread.
virtual void mainLoop()
static void removeHandler(UEventsHandler *handler)
static void addHandler(UEventsHandler *handler)
virtual void mainLoop()
static void post(UEvent *event, bool async=true, const UEventsSender *sender=0)