src/foundation/ZMessage.h

00001 /*  @(#) $Id: ZMessage.h,v 1.6 2006/04/10 20:44:20 agreen Exp $ */
00002 
00003 /* ------------------------------------------------------------
00004 Copyright (c) 2000 Andrew Green and Learning in Motion, Inc.
00005 http://www.zoolib.org
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a copy
00008 of this software and associated documentation files (the "Software"), to deal
00009 in the Software without restriction, including without limitation the rights
00010 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00011 copies of the Software, and to permit persons to whom the Software is
00012 furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00020 COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
00021 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
00022 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 ------------------------------------------------------------ */
00024 
00025 #ifndef __ZMessage__
00026 #define __ZMessage__ 1
00027 #include "zconfig.h"
00028 
00029 #include "ZCompat_NonCopyable.h"
00030 #include "ZTuple.h"
00031 
00032 // =================================================================================================
00033 #pragma mark -
00034 #pragma mark * ZMessage
00035 
00036 class ZMessage : public ZTuple
00037         {
00038 public:
00039         ZMessage() {}
00040         ZMessage(const ZMessage& iOther) : ZTuple(iOther) {}
00041         ZMessage(const ZTuple& iTuple) : ZTuple(iTuple) {}
00042         ~ZMessage() {}
00043 
00044 
00045         ZTuple AsTuple() const { return *this; }        
00046         };
00047 
00048 // =================================================================================================
00049 #pragma mark -
00050 #pragma mark * ZMessageLooper
00051 
00052 class ZMessageReceiver;
00053 class ZMessengerRep;
00054 
00055 class ZMessageLooper : ZooLib::NonCopyable
00056         {
00057 public:
00058         virtual ZMutexBase& GetLock() = 0;
00059         virtual bool DispatchMessage(const ZMessage& iMessage);
00060         virtual void QueueMessage(const ZMessage& iMessage)  = 0;
00061         void QueueMessageForReceiver(const ZMessage& iMessage, ZMessageReceiver* iReceiver);
00062         void QueueMessageForMessenger(const ZMessage& iMessage, ZRef<ZMessengerRep> iMessengerRep);
00063         };
00064 
00065 // =================================================================================================
00066 #pragma mark -
00067 #pragma mark * ZMessengerRep
00068 
00069 class ZMessengerRep : public ZRefCountedWithFinalization, ZooLib::NonCopyable
00070         {
00071 public:
00072         virtual ~ZMessengerRep();
00073 
00074         friend class ZMessageLooper;
00075         friend class ZMessageReceiver;
00076         friend class ZMessenger;
00077 
00078         ZMessengerRep(ZMessageReceiver* iReceiver);
00079 
00080         bool PostMessage(const ZMessage& iMessage);
00081         bool SendMessage(const ZMessage& iMessage, ZMessage* oReply);
00082 
00083         void ReceiverDisposed(ZMessageReceiver* iReceiver);
00084 
00085         void DispatchMessage(const ZMessage& iMessage);
00086 
00087         ZMessageReceiver* fReceiver;
00088         };
00089 
00090 // =================================================================================================
00091 #pragma mark -
00092 #pragma mark * ZMessenger
00093 
00094 class ZMessenger
00095         {
00096 public:
00097         ZMessenger();
00098         ZMessenger(const ZMessenger& inOther);
00099         ZMessenger(const ZRef<ZMessengerRep>& inRep);
00100         ~ZMessenger();
00101 
00102         ZMessenger& operator=(const ZMessenger& inOther);
00103 
00104         bool operator==(const ZMessenger& inOther) const;
00105 
00106         bool PostMessage(const ZMessage& iMessage);
00107         bool SendMessage(const ZMessage& iMessage, ZMessage* oReply);
00108 
00109 private:
00110         ZRef<ZMessengerRep> fRep;
00111         };
00112 
00113 // =================================================================================================
00114 #pragma mark -
00115 #pragma mark * ZMessageReceiver
00116 
00117 class ZMessageReceiver
00118         {
00119 private:
00120         ZMessageReceiver() {}
00121         ZMessageReceiver(const ZMessageReceiver&) {}
00122         ZMessageReceiver& operator=(const ZMessageReceiver&) { return *this; }
00123 
00124 protected:
00125         ZMessageReceiver(ZMessageLooper* iLooper);
00126         ~ZMessageReceiver();
00127 
00128 public:
00129         ZMessageLooper* GetLooper()
00130                 { return fLooper; }
00131 
00132         ZMessenger GetMessenger();
00133 
00134         void PostMessage(const ZMessage& iMessage);
00135 
00136         virtual void ReceivedMessage(const ZMessage& iMessage);
00137 
00138 private:
00139         ZMessageLooper* fLooper;
00140         ZRef<ZMessengerRep> fMessengerRep;
00141         };
00142 
00143 // =================================================================================================
00144 #if 0
00145 #pragma mark -
00146 #pragma mark * ZMessenger Inlines
00147 
00148 inlne ZMessenger::ZMessenger()
00149         {}
00150 
00151 inline ZMessenger::ZMessenger(const ZMessenger& inOther)
00152 :       fRep(inOther.fRep)
00153         {}
00154 
00155 inline ZMessenger::~ZMessenger()
00156         {}
00157 
00158 inline ZMessenger& ZMessenger::operator=(const ZMessenger& inOther)
00159         {
00160         fRep = inOther.fRep;
00161         return *this;
00162         }
00163 
00164 inline bool ZMessenger::operator==(const ZMessenger& inOther) const
00165         { return fRep == inOther.fRep; }
00166 
00167 #endif
00168 // =================================================================================================
00169 
00170 #endif // __ZMessage__

Generated on Thu Jul 26 11:21:51 2007 for ZooLib by  doxygen 1.4.7