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/UtiLiteExp.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 
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
virtual void mainLoop()
const UEventsHandler * receiver_
std::vector< UEventsHandler * > _handlers
USemaphore postEventSem_
const std::string eventName_
Definition: UEvent.h:57
virtual ~UEventDispatcher()
std::list< Pipe > pipes_
#define UTILITE_EXP
Definition: UtiLiteExp.h:33
static UEventsManager * instance_
const UEventsSender * sender_
virtual void killCleanup()
Definition: UMutex.h:54
virtual void mainLoopKill()
Definition: UThread.h:203
Pipe(const UEventsSender *sender, const UEventsHandler *receiver, const std::string &eventName)
static UDestroyer< UEventsManager > destroyer_
friend class UEventsManager
ULogger class and convenient macros.
std::list< UEventsHandler * > handlers_
std::list< std::pair< UEvent *, const UEventsSender * > > events_


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06