UtiLite  0.3.1
A lite utilities library
include/utilite/UEventsManager.h
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 "utilite/UtiLiteExp.h" // DLL export/import defines
00024 
00025 #include "utilite/UEventsHandler.h"
00026 #include "utilite/UThreadNode.h"
00027 #include "utilite/ULogger.h"
00028 #include "utilite/UDestroyer.h"
00029 
00030 #include <list>
00031 
00032 // TODO Not implemented... for multithreading event handling
00033 class UEventDispatcher : public UThread
00034 {
00035 public:
00036         virtual ~UEventDispatcher();
00037 protected:
00038         friend class UEventsManager;
00039         UEventDispatcher();
00040 
00041         virtual void mainLoop();
00042 
00043 private:
00044         virtual void killCleanup();
00045 
00046 private:
00047         UEvent * _event;
00048         std::vector<UEventsHandler*> _handlers;
00049 };
00050 
00077 class UTILITE_EXP UEventsManager : public UThread{
00078 
00079 public:
00080 
00087     static void addHandler(UEventsHandler* handler);
00088 
00095     static void removeHandler(UEventsHandler* handler);
00096 
00109     static void post(UEvent * event, bool async = true);
00110     
00111 protected:
00112 
00113     /*
00114      * This method is used to have a reference on the 
00115      * EventsManager. When no EventsManager exists, one is 
00116      * created. There is only one instance in the application.
00117      * See the Singleton pattern further explanation.
00118      *
00119      * @return the reference on the EventsManager
00120      */
00121     static UEventsManager* getInstance();
00122 
00123     /*
00124      * Called only once in getInstance(). It can't be instantiated
00125      * by the user.
00126      *
00127      */
00128     UEventsManager();
00129 
00130     /*
00131      * Only called by a Destroyer.
00132      */
00133     virtual ~UEventsManager();
00134 
00135     /*
00136      * A Destroyer is used to remove a dynamically created
00137      * Singleton. It is friend here to have access to the 
00138      * destructor.
00139      *
00140      */
00141     friend class UDestroyer<UEventsManager>;
00142 
00146     virtual void mainLoop();
00147 
00148 private:
00149 
00153     virtual void mainLoopKill();
00154 
00155     /*
00156      * This method dispatches asynchronized events to all handlers.
00157      * FIFO (first in first out) dispatching is used.
00158      */
00159     virtual void dispatchEvents();
00160 
00161     /*
00162          * This method dispatches an event to all handlers.
00163          */
00164     virtual void dispatchEvent(UEvent * event);
00165 
00166     /*
00167      * This method is used to add an events 
00168      * handler to the list of handlers.
00169      *
00170      * @param handler the handler to be added.
00171      */
00172     void _addHandler(UEventsHandler* handler);
00173 
00174     /*
00175      * This method is used to remove an events 
00176      * handler from the list of handlers.
00177      *
00178      * @param handler the handler to be removed.
00179      */
00180     void _removeHandler(UEventsHandler* handler);
00181 
00182     /*
00183      * This method is used to post an event to
00184      * handlers.
00185      *
00186      * Event can be posted asynchronously or not. In the first case,
00187      * the event is dispatched by the UEventsManager's thread. In the
00188      * second case, the event is handled immediately by event's
00189      * receivers, thus in the sender thread.
00190      *
00191      * @param event the event to be posted.
00192      * @param async if true, the event is dispatched by the UEventsManager thread, otherwise it's in the caller thread (synchronous).
00193      */
00194     void _postEvent(UEvent * event, bool async = true);
00195     
00196 private:
00197     
00198     static UEventsManager* instance_;            /* The EventsManager instance pointer. */
00199     static UDestroyer<UEventsManager> destroyer_; /* The EventsManager's destroyer. */
00200     std::list<UEvent*> events_;                /* The events list. */
00201     std::list<UEventsHandler*> handlers_;      /* The handlers list. */
00202     UMutex eventsMutex_;                         /* The mutex of the events list, */
00203     UMutex handlersMutex_;                       /* The mutex of the handlers list. */
00204     USemaphore postEventSem_;                    /* Semaphore used to signal when an events is posted. */
00205 };
00206 
00207 #endif // UEVENTSMANAGER_H
 All Classes Files Functions Variables Enumerations Friends Defines