UEventsManager.h
Go to the documentation of this file.
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 
29 
30 #include <list>
31 #include <map>
32 
33 // TODO Not implemented... for multithreading event handling
34 class UEventDispatcher : public UThread
35 {
36 public:
37  virtual ~UEventDispatcher();
38 protected:
39  friend class UEventsManager;
41 
42  virtual void mainLoop();
43 
44 private:
45  virtual void killCleanup();
46 
47 private:
49  std::vector<UEventsHandler*> _handlers;
50 };
51 
78 class UTILITE_EXPORT UEventsManager : public UThread{
79 
80 public:
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 
125 protected:
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  */
142  UEventsManager();
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 
162 private:
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 
227 private:
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  {}
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_;
251 };
252 
253 #endif // UEVENTSMANAGER_H
UEventsHandler
Definition: UEventsHandler.h:128
UEventDispatcher::killCleanup
virtual void killCleanup()
UThreadNode.h
UEventsManager
Definition: UEventsManager.h:78
UEventsManager::handlersMutex_
UMutex handlersMutex_
Definition: UEventsManager.h:247
UEventsManager::Pipe::Pipe
Pipe(const UEventsSender *sender, const UEventsHandler *receiver, const std::string &eventName)
Definition: UEventsManager.h:232
UDestroyer.h
UEventsManager::postEventSem_
USemaphore postEventSem_
Definition: UEventsManager.h:248
UThread::mainLoop
virtual void mainLoop()=0
UEventDispatcher::UEventDispatcher
UEventDispatcher()
UEvent
Definition: UEvent.h:57
UDestroyer
Definition: UDestroyer.h:32
UMutex
Definition: UMutex.h:54
UEventDispatcher::mainLoop
virtual void mainLoop()
UEventDispatcher::_handlers
std::vector< UEventsHandler * > _handlers
Definition: UEventsManager.h:49
UEventsManager::handlers_
std::list< UEventsHandler * > handlers_
Definition: UEventsManager.h:245
UEventsManager::destroyer_
static UDestroyer< UEventsManager > destroyer_
Definition: UEventsManager.h:243
UEventsManager::events_
std::list< std::pair< UEvent *, const UEventsSender * > > events_
Definition: UEventsManager.h:244
UEventsManager::Pipe
Definition: UEventsManager.h:229
UEventDispatcher
Definition: UEventsManager.h:34
UEventsManager::Pipe::eventName_
const std::string eventName_
Definition: UEventsManager.h:239
UEventDispatcher::~UEventDispatcher
virtual ~UEventDispatcher()
ULogger.h
ULogger class and convenient macros.
UThread::mainLoopKill
virtual void mainLoopKill()
Definition: UThread.h:203
UEventsManager::pipes_
std::list< Pipe > pipes_
Definition: UEventsManager.h:249
UThread
Definition: UThread.h:86
UEventsManager::Pipe::receiver_
const UEventsHandler * receiver_
Definition: UEventsManager.h:238
UEventsManager::pipesMutex_
UMutex pipesMutex_
Definition: UEventsManager.h:250
UEventsHandler.h
USemaphore
Definition: USemaphore.h:54
UEventDispatcher::_event
UEvent * _event
Definition: UEventsManager.h:48
UEventsManager::instance_
static UEventsManager * instance_
Definition: UEventsManager.h:242
UEventsManager::eventsMutex_
UMutex eventsMutex_
Definition: UEventsManager.h:246
UEventsManager::Pipe::sender_
const UEventsSender * sender_
Definition: UEventsManager.h:237
UEventsSender
Definition: UEventsSender.h:15


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jul 25 2024 02:50:23