00001 #ifndef CSMESSAGE_LISTENER_H 00002 #define CSMESSAGE_LISTENER_H 00003 00004 #include <vector> 00005 #include "CSTypes.h" 00006 #include "CSLog.h" 00007 00008 class CSMessageListener; 00009 class CSMessageDispatchable; 00010 class CSMessage; 00011 00012 struct CSInternalMessage 00013 { 00014 CSMessageListener *listener; // never to be freed 00015 CSMessage *message; // never to be freed 00016 }; 00017 00018 typedef std::vector<CSInternalMessage*> CSInternalMessages; 00019 00020 /** 00021 This class is the base class for all message listeners. 00022 It also provides Singleton features for dispatching messages 00023 at a later time.<BR> 00024 00025 The method to dispatch a message to a (n allways given) listerner 00026 is:<PRE> 00027 void CSMessageListener::dispatchMessage(CSMessage *message) 00028 </PRE> 00029 The message itself "knows" if it is a message that must be dispatched 00030 at once - or at a later point in time.<BR> 00031 00032 If the message is to be dispatched at one, the method: 00033 <PRE> 00034 virtual void reactOnMessage(CSMessage *message) = 0; 00035 </PRE> 00036 will be called.<BR> 00037 (But this should not be called by a client, this method is called by 00038 CSMEessageDispatchable. The client, that wishes to "send a message" 00039 should allways use the method: 00040 <PRE> 00041 void sendMessage(CSMessage &message); 00042 </PRE> 00043 of the CSMEessageDispatchable class!)<BR> 00044 00045 00046 All classes implementing "CSMessageListener", must implement that method.<BR> 00047 00048 Otherwise (if the message is supposed to be dispatched later), the 00049 message AND the listener is added to a static Vector. 00050 At the apropriate time some "HANDLER" (main look...) must call: 00051 <PRE> 00052 void CSMessageListener::dispatchQueuedMessages() 00053 </PRE> 00054 At that point all pending messaged will be send to the corresponing 00055 listeners. 00056 00057 */ 00058 class CSMessageListener 00059 { 00060 friend CSMessageDispatchable; 00061 private: 00062 static CSInternalMessages mCSInternalMessages; 00063 00064 protected: 00065 static void addMessageToQueue(CSMessageListener *listener, CSMessage *message); 00066 00067 public: 00068 CSMessageListener(); 00069 virtual ~CSMessageListener(); 00070 static const char *CLASS; 00071 virtual std::string getType() {return (std::string) CLASS;} 00072 00073 static void dispatchQueuedMessages(); 00074 virtual void dispatchMessage(CSMessage *message); 00075 virtual void reactOnMessage(CSMessage *message) = 0; 00076 }; 00077 00078 00079 #endif // CSMESSAGE_LISTENER_H 00080