Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

EventSender.hh

Go to the documentation of this file.
00001 #ifndef EVENT_SENDER_H
00002 #define EVENT_SENDER_H
00003 
00023 #include <list>
00024 #include <typeinfo>
00025 #include <string>
00026 #include <sstream>
00027 
00028 #include "Listener.hh" // uses inline
00029 #include "EventFWD.hh"
00030 class PolymorphEvent;
00031 
00032 template <typename EvType>
00033 class EventSender
00034 {
00035     public: // types
00037         typedef Listener<EvType> TListener;
00038         
00039     private: // types
00041         friend class Listener<EvType>;
00043         typedef std::list<TListener*> Registry;
00044     
00045     public: // static methods
00046         class IllegalSendError;
00047         inline static void remove(TListener&);
00048         inline static void add(TListener&);
00049         inline static void send(const EvType&);
00050         inline static bool isSending();
00051         inline static bool hasListeners();
00052         inline static unsigned int getNumListeners();
00053         inline static unsigned int getMinNumIgnored();
00054         
00055     private: // construct/destroy singleton
00056         EventSender(): _isBusySending(false), _eventIgnored(0) {}
00057         ~EventSender();
00058         
00059     private: // methods called by Listener
00060         void registerListener(TListener*);
00061         void removeListener(TListener*);
00062         void incEventIgnored() {_eventIgnored ++;}
00063         static EventSender<EvType>& instance(); // MUST NOT BE INLINE
00064         
00065     private: // utility methods 
00066         void sendEvent(const EvType&);
00067         void cleanupQueues();
00068         bool removeFrom(Registry&, TListener*);
00069         inline std::string lisnrID(TListener* = NULL) const;
00070         //bool hasListener(TListener*) const;
00071     
00072     private: // data
00074         Registry _registry;
00075     
00077         bool _isBusySending;
00079         unsigned int _eventIgnored;
00081         Registry _registrationQueue;
00083         Registry _removalQueue;
00084 };
00085         
00090 template <>
00091 class EventSender<PolymorphEvent>
00092 {
00093     public: 
00099         static void send(const PolymorphEvent& event)
00100         {
00101             event.send();
00102         }
00103 };
00104 
00105 /*  Include IllegalSendError in this header so users don't have 
00106     to include manually. It has to be included after class definition
00107     since it is an inner class to EventSender. 
00108     */
00109 #include "IllegalSendError.hh" 
00110 
00116 template<typename EvType>
00117 inline bool 
00118 EventSender<EvType>::hasListeners()
00119 {
00120     return ! instance()._registry.empty();
00121 }
00122 
00129 template<typename EvType>
00130 inline unsigned int 
00131 EventSender<EvType>::getMinNumIgnored() 
00132 {
00133     return instance()._eventIgnored;
00134 }
00135        
00140 template<typename EvType>
00141 inline unsigned int 
00142 EventSender<EvType>::getNumListeners() 
00143 {
00144     return instance()._registry.size();
00145 }
00146         
00163 template<typename EvType>
00164 inline void 
00165 EventSender<EvType>::send(const EvType& event) 
00166 {
00167     instance().sendEvent(event);
00168 }
00169 
00173 template<typename EvType>
00174 inline bool 
00175 EventSender<EvType>::isSending() 
00176 {
00177     return instance()._isBusySending;
00178 }
00179 
00183 template<typename EvType>
00184 inline void 
00185 EventSender<EvType>::remove(TListener& listener) 
00186 {
00187     listener.TListener::ignoreEvents();
00188 }
00189 
00193 template<typename EvType>
00194 inline void 
00195 EventSender<EvType>::add(TListener& listener) 
00196 {
00197     listener.TListener::listenForEvents();
00198 }
00199 
00201 template<typename EvType>
00202 inline std::string 
00203 EventSender<EvType>::lisnrID(TListener* lisnr)
00204 const 
00205 {
00206     std::ostringstream lisnrStr;
00207     lisnrStr << typeid(TListener).name();
00208     if (lisnr) lisnrStr << ":" << lisnr;
00209     return lisnrStr.str();
00210 }
00211             
00212 #ifdef __GNUG__ 
00213   // because gcc doesn't handle separate template definition
00214   #include "EventSender.cc"
00215 #endif
00216 
00217 #endif // EVENT_SENDER_H
00218 

Generated on Sun Feb 16 16:56:21 2003 for C++ Event Handling Template Library by doxygen1.2.18