RTAB-Map 0.25.2
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
UEventsManager.h
1/*
2Copyright (c) 2010, Mathieu Labbe
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holders nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifndef UEVENTSMANAGER_H
29#define UEVENTSMANAGER_H
30
31#include "rtabmap/utilite/utilite_export.h" // DLL export/import defines
32
33#include "rtabmap/utilite/UEventsHandler.h"
36#include "rtabmap/utilite/UDestroyer.h"
37
38#include <list>
39#include <map>
40
41// TODO Not implemented... for multithreading event handling
43{
44public:
45 virtual ~UEventDispatcher();
46protected:
47 friend class UEventsManager;
49
50 virtual void mainLoop();
51
52private:
53 virtual void killCleanup();
54
55private:
56 UEvent * _event;
57 std::vector<UEventsHandler*> _handlers;
58};
59
86class UTILITE_EXPORT UEventsManager : public UThread{
87
88public:
89
96 static void addHandler(UEventsHandler* handler);
97
104 static void removeHandler(UEventsHandler* handler);
105
118 static void post(UEvent * event, bool async = true, const UEventsSender * sender = 0);
119
120 static void createPipe(
121 const UEventsSender * sender,
122 const UEventsHandler * receiver,
123 const std::string & eventName);
124
125 static void removePipe(
126 const UEventsSender * sender,
127 const UEventsHandler * receiver,
128 const std::string & eventName);
129
130 static void removeAllPipes(const UEventsSender * sender);
131 static void removeNullPipes(const UEventsSender * sender);
132
133protected:
134
135 /*
136 * This method is used to have a reference on the
137 * EventsManager. When no EventsManager exists, one is
138 * created. There is only one instance in the application.
139 * See the Singleton pattern further explanation.
140 *
141 * @return the reference on the EventsManager
142 */
143 static UEventsManager* getInstance();
144
145 /*
146 * Called only once in getInstance(). It can't be instantiated
147 * by the user.
148 *
149 */
151
152 /*
153 * Only called by a Destroyer.
154 */
155 virtual ~UEventsManager();
156
157 /*
158 * A Destroyer is used to remove a dynamically created
159 * Singleton. It is friend here to have access to the
160 * destructor.
161 *
162 */
163 friend class UDestroyer<UEventsManager>;
164
168 virtual void mainLoop();
169
170private:
171
175 virtual void mainLoopKill();
176
177 /*
178 * This method dispatches asynchronized events to all handlers.
179 * FIFO (first in first out) dispatching is used.
180 */
181 virtual void dispatchEvents();
182
183 /*
184 * This method dispatches an event to all handlers.
185 */
186 virtual bool dispatchEvent(UEvent * event, const UEventsSender * sender);
187
188 /*
189 * This method is used to add an events
190 * handler to the list of handlers.
191 *
192 * @param handler the handler to be added.
193 */
194 void _addHandler(UEventsHandler* handler);
195
196 /*
197 * This method is used to remove an events
198 * handler from the list of handlers.
199 *
200 * @param handler the handler to be removed.
201 */
202 void _removeHandler(UEventsHandler* handler);
203
204 /*
205 * This method is used to post an event to
206 * handlers.
207 *
208 * Event can be posted asynchronously or not. In the first case,
209 * the event is dispatched by the UEventsManager's thread. In the
210 * second case, the event is handled immediately by event's
211 * receivers, thus in the sender thread.
212 *
213 * @param event the event to be posted.
214 * @param async if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous).
215 */
216 void _postEvent(UEvent * event, bool async = true, const UEventsSender * sender = 0);
217
218 std::list<UEventsHandler*> getPipes(
219 const UEventsSender * sender,
220 const std::string & eventName);
221
222 void _createPipe(
223 const UEventsSender * sender,
224 const UEventsHandler * receiver,
225 const std::string & eventName);
226
227 void _removePipe(
228 const UEventsSender * sender,
229 const UEventsHandler * receiver,
230 const std::string & eventName);
231
232 void _removeAllPipes(const UEventsSender * sender);
233 void _removeNullPipes(const UEventsSender * sender);
234
235private:
236
237 class Pipe
238 {
239 public:
240 Pipe(const UEventsSender * sender, const UEventsHandler * receiver, const std::string & eventName) :
241 sender_(sender),
242 receiver_(receiver),
243 eventName_(eventName)
244 {}
245 const UEventsSender * sender_;
246 const UEventsHandler * receiver_;
247 const std::string eventName_;
248 };
249
250 static UEventsManager* instance_; /* The EventsManager instance pointer. */
251 static UDestroyer<UEventsManager> destroyer_; /* The EventsManager's destroyer. */
252 std::list<std::pair<UEvent*, const UEventsSender * > > events_; /* The events list. */
253 std::list<UEventsHandler*> handlers_; /* The handlers list. */
254 UMutex eventsMutex_; /* The mutex of the events list, */
255 UMutex handlersMutex_; /* The mutex of the handlers list. */
256 USemaphore postEventSem_; /* Semaphore used to signal when an events is posted. */
257 std::list<Pipe> pipes_;
258 UMutex pipesMutex_;
259};
260
261#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)