TCMTMessageDispatcher.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCMTMessageDispatcher.h 895 2008-11-03 08:33:21Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TC_MT_MESSAGE_DISPATCHER_H_
00037 #define _TC_MT_MESSAGE_DISPATCHER_H_
00038 
00039 #include "TCMTMessage.h"
00040 
00041 namespace TC
00042 {
00043    namespace MT
00044    {
00060       class MessageDispatcher: protected NonCopyAble
00061       {
00062       public:
00063          class CallBack
00064          {
00065          public:
00066             virtual bool operator()(MessagePtr message) = 0;
00067             virtual ~CallBack() {}
00068          };
00069          typedef SharedPtr<CallBack> CallBackPtr;
00070 
00071          virtual void RegisterMessageCallback(uint32 id, CallBackPtr call_back) = 0;
00072 
00073          template <class MessageClass, class ObjectClass, typename CallBackMethod>
00074          void RegisterMessageCallback(uint32 id, SharedPtr<ObjectClass> class_instance, 
00075             CallBackMethod call_back);
00076 
00077          virtual bool DispatchMessage(MessagePtr message) = 0;
00078 
00079          virtual ~MessageDispatcher() {}
00080       };
00081 
00082       typedef SharedPtr<MessageDispatcher> MessageDispatcherPtr;
00083 
00084       template <class MessageClass, class ObjectClass>
00085       class ClassMethodeCallBack: public MessageDispatcher::CallBack
00086       {
00087       public:
00088          typedef bool (ObjectClass::*Method)(SharedPtr<MessageClass>);
00089 
00090          ClassMethodeCallBack(SharedPtr<ObjectClass> _class_instance, Method _method)
00091             :m_class_instance(_class_instance),
00092             m_method(_method)
00093          {
00094          };
00095 
00096          bool operator()(MessagePtr message)
00097          {
00098             ObjectClass* _class_instance = m_class_instance.operator->();
00099             return (_class_instance->*m_method)(SharedPtr<MessageClass>::StaticCast(message));
00100          };
00101 
00102       private:
00103          SharedPtr<ObjectClass> m_class_instance;
00104          Method m_method;
00105       };
00106 
00107       template <class MessageClass, class ObjectClass, typename CallBackMethod>
00108       void MessageDispatcher::RegisterMessageCallback(uint32 id, SharedPtr<ObjectClass> class_instance, 
00109          CallBackMethod call_back)
00110       {
00111          RegisterMessageCallback(id, 
00112             CallBackPtr(new ClassMethodeCallBack<MessageClass, ObjectClass>(class_instance, call_back)));
00113       }
00114 
00119    } // namespace MT
00120 } // namespace TC
00121 
00122 
00123 #endif // _TC_MT_MESSAGE_DISPATCHER_H_

Copyright (c) Thomas Goessler 2003 - 2008