UEventsManager.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef UEVENTSMANAGER_H
00021 #define UEVENTSMANAGER_H
00022 
00023 #include "rtabmap/utilite/UtiLiteExp.h" // DLL export/import defines
00024 
00025 #include "rtabmap/utilite/UEventsHandler.h"
00026 #include "rtabmap/utilite/UThreadNode.h"
00027 #include "rtabmap/utilite/ULogger.h"
00028 #include "rtabmap/utilite/UDestroyer.h"
00029 
00030 #include <list>
00031 #include <map>
00032 
00033 // TODO Not implemented... for multithreading event handling
00034 class UEventDispatcher : public UThread
00035 {
00036 public:
00037         virtual ~UEventDispatcher();
00038 protected:
00039         friend class UEventsManager;
00040         UEventDispatcher();
00041 
00042         virtual void mainLoop();
00043 
00044 private:
00045         virtual void killCleanup();
00046 
00047 private:
00048         UEvent * _event;
00049         std::vector<UEventsHandler*> _handlers;
00050 };
00051 
00078 class UTILITE_EXP UEventsManager : public UThread{
00079 
00080 public:
00081 
00088     static void addHandler(UEventsHandler* handler);
00089 
00096     static void removeHandler(UEventsHandler* handler);
00097 
00110     static void post(UEvent * event, bool async = true, const UEventsSender * sender = 0);
00111 
00112     static void createPipe(
00113                 const UEventsSender * sender,
00114                 const UEventsHandler * receiver,
00115                 const std::string & eventName);
00116 
00117     static void removePipe(
00118                 const UEventsSender * sender,
00119                 const UEventsHandler * receiver,
00120                 const std::string & eventName);
00121 
00122     static void removeAllPipes(const UEventsSender * sender);
00123     static void removeNullPipes(const UEventsSender * sender);
00124 
00125 protected:
00126 
00127     /*
00128      * This method is used to have a reference on the 
00129      * EventsManager. When no EventsManager exists, one is 
00130      * created. There is only one instance in the application.
00131      * See the Singleton pattern further explanation.
00132      *
00133      * @return the reference on the EventsManager
00134      */
00135     static UEventsManager* getInstance();
00136 
00137     /*
00138      * Called only once in getInstance(). It can't be instantiated
00139      * by the user.
00140      *
00141      */
00142     UEventsManager();
00143 
00144     /*
00145      * Only called by a Destroyer.
00146      */
00147     virtual ~UEventsManager();
00148 
00149     /*
00150      * A Destroyer is used to remove a dynamically created
00151      * Singleton. It is friend here to have access to the 
00152      * destructor.
00153      *
00154      */
00155     friend class UDestroyer<UEventsManager>;
00156 
00160     virtual void mainLoop();
00161 
00162 private:
00163 
00167     virtual void mainLoopKill();
00168 
00169     /*
00170      * This method dispatches asynchronized events to all handlers.
00171      * FIFO (first in first out) dispatching is used.
00172      */
00173     virtual void dispatchEvents();
00174 
00175     /*
00176          * This method dispatches an event to all handlers.
00177          */
00178     virtual bool dispatchEvent(UEvent * event, const UEventsSender * sender);
00179 
00180     /*
00181      * This method is used to add an events 
00182      * handler to the list of handlers.
00183      *
00184      * @param handler the handler to be added.
00185      */
00186     void _addHandler(UEventsHandler* handler);
00187 
00188     /*
00189      * This method is used to remove an events 
00190      * handler from the list of handlers.
00191      *
00192      * @param handler the handler to be removed.
00193      */
00194     void _removeHandler(UEventsHandler* handler);
00195 
00196     /*
00197      * This method is used to post an event to
00198      * handlers.
00199      *
00200      * Event can be posted asynchronously or not. In the first case,
00201      * the event is dispatched by the UEventsManager's thread. In the
00202      * second case, the event is handled immediately by event's
00203      * receivers, thus in the sender thread.
00204      *
00205      * @param event the event to be posted.
00206      * @param async if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous).
00207      */
00208     void _postEvent(UEvent * event, bool async = true, const UEventsSender * sender = 0);
00209 
00210     std::list<UEventsHandler*> getPipes(
00211                 const UEventsSender * sender,
00212                 const std::string & eventName);
00213 
00214     void _createPipe(
00215                 const UEventsSender * sender,
00216                 const UEventsHandler * receiver,
00217                 const std::string & eventName);
00218 
00219     void _removePipe(
00220                 const UEventsSender * sender,
00221                 const UEventsHandler * receiver,
00222                 const std::string & eventName);
00223 
00224     void _removeAllPipes(const UEventsSender * sender);
00225     void _removeNullPipes(const UEventsSender * sender);
00226 
00227 private:
00228     
00229     class Pipe
00230     {
00231     public:
00232         Pipe(const UEventsSender * sender, const UEventsHandler * receiver, const std::string & eventName) :
00233                 sender_(sender),
00234                 receiver_(receiver),
00235                 eventName_(eventName)
00236         {}
00237         const UEventsSender * sender_;
00238         const UEventsHandler * receiver_;
00239         const std::string eventName_;
00240     };
00241 
00242     static UEventsManager* instance_;            /* The EventsManager instance pointer. */
00243     static UDestroyer<UEventsManager> destroyer_; /* The EventsManager's destroyer. */
00244     std::list<std::pair<UEvent*, const UEventsSender * > > events_; /* The events list. */
00245     std::list<UEventsHandler*> handlers_;      /* The handlers list. */
00246     UMutex eventsMutex_;                         /* The mutex of the events list, */
00247     UMutex handlersMutex_;                       /* The mutex of the handlers list. */
00248     USemaphore postEventSem_;                    /* Semaphore used to signal when an events is posted. */
00249     std::list<Pipe> pipes_;
00250     UMutex pipesMutex_;
00251 };
00252 
00253 #endif // UEVENTSMANAGER_H


rtabmap
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 21:59:32