00001 //================================================================================================== 00002 // Copyright (C) 2010 Brian Tietz sdbtietz at yahoo dot com 00003 // 00004 // This program is free software; you can redistribute it and/or modify it under the terms of the 00005 // GNU General Public License as published by the Free Software Foundation, version 2.0 of the 00006 // License. 00007 // 00008 // This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 00009 // even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00010 // General Public License for more details. 00011 // 00012 // You should have received a copy of the GNU General Public License along with this program; if 00013 // not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00014 // 02110-1301, USA. 00015 // 00016 // For commercial software, the copyright holder (Brian Tietz, email sdbtietz at yahoo dot com) 00017 // reserves the right and is willing to waive the proprietary source code disclosure aspects of that 00018 // license as applied to the UT library in exchange for either substantial contributions to the 00019 // development of the UT library or other forms of compensation. Any such waiver must be 00020 // established in writing between the copyright holder and the commercial entity obtaining such a 00021 // waiver. 00022 //================================================================================================== 00023 00024 00025 #ifndef _UT_MESSAGE_RECEIVER_H_ 00026 #define _UT_MESSAGE_RECEIVER_H_ 00027 00028 00029 //================================================================================================== 00030 //=== Project headers 00031 //================================================================================================== 00032 #include "UT.h" 00033 00034 00035 //================================================================================================== 00036 //=== Forward name declarations 00037 //================================================================================================== 00038 class Message_t; 00039 class Task_t; 00040 00041 00042 // \cond DOXYGEN_DOCUMENT_NEVER 00043 //================================================================================================== 00044 class UT_EXPORT AbstractMessageReceiver_t 00045 //================================================================================================== 00046 { 00047 //---------------------------------------------------------------------------------------------- 00048 protected: 00049 //---------------------------------------------------------------------------------------------- 00050 inline AbstractMessageReceiver_t(); 00051 virtual ~AbstractMessageReceiver_t(); 00052 00053 //---------------------------------------------------------------------------------------------- 00054 protected: 00055 //---------------------------------------------------------------------------------------------- 00056 virtual void AbstractMessageReceived( const Message_t* message, Task_t* task ) = 0; 00057 00058 friend class MessageLoop_t; 00059 }; 00060 // \endcond 00061 00064 00065 00066 //================================================================================================== 00067 template <class MESSAGE_TYPE> 00068 class MessageTypeReceiver_t 00069 //================================================================================================== 00070 : public AbstractMessageReceiver_t 00073 { }; 00074 00075 00076 //================================================================================================== 00077 template < class TASK_TYPE, class MESSAGE_TYPE > 00078 class MessageReceiver_t 00079 //================================================================================================== 00080 : public MessageTypeReceiver_t<MESSAGE_TYPE> 00091 { 00092 //---------------------------------------------------------------------------------------------- 00093 public: 00094 //---------------------------------------------------------------------------------------------- 00095 inline MessageReceiver_t( void (TASK_TYPE::*receiver_function)(const MESSAGE_TYPE*) ); 00098 00099 inline void SetReceiverFunction( void (TASK_TYPE::*receiver_function)(const MESSAGE_TYPE*) ); 00101 00102 //---------------------------------------------------------------------------------------------- 00103 private: 00104 //---------------------------------------------------------------------------------------------- 00105 virtual void AbstractMessageReceived( const Message_t* message, Task_t* task ); 00106 void (TASK_TYPE::*m_receiver_function)(const MESSAGE_TYPE*); 00107 }; 00108 00109 00110 00111 00112 //================================================================================================== 00113 //================================================================================================== 00114 //=== 00115 //=== Inline function implementations: class AbstractMessageReceiver_t 00116 //=== 00117 //================================================================================================== 00118 //================================================================================================== 00119 00120 // \cond DOXYGEN_DOCUMENT_NEVER 00121 00122 inline 00123 AbstractMessageReceiver_t::AbstractMessageReceiver_t() 00124 { } 00125 00126 //\endcond 00127 00128 00129 00130 00131 //================================================================================================== 00132 //================================================================================================== 00133 //=== 00134 //=== Template function implementations: class MessageReceiver_t 00135 //=== 00136 //================================================================================================== 00137 //================================================================================================== 00138 template < class TASK_TYPE, class MESSAGE_TYPE > 00139 inline 00140 MessageReceiver_t<TASK_TYPE,MESSAGE_TYPE>::MessageReceiver_t( 00141 void (TASK_TYPE::*receiver_function)(const MESSAGE_TYPE*) ) 00142 { 00143 m_receiver_function = receiver_function; 00144 } 00145 00146 00147 template < class TASK_TYPE, class MESSAGE_TYPE > 00148 inline void 00149 MessageReceiver_t<TASK_TYPE,MESSAGE_TYPE>::SetReceiverFunction( 00150 void (TASK_TYPE::*receiver_function)(const MESSAGE_TYPE*) ) 00151 { 00152 m_receiver_function = receiver_function; 00153 } 00154 00155 00156 template < class TASK_TYPE, class MESSAGE_TYPE > 00157 void 00158 MessageReceiver_t<TASK_TYPE,MESSAGE_TYPE>::AbstractMessageReceived( const Message_t* a_message, 00159 Task_t* a_task ) 00160 { 00161 TASK_TYPE* task; 00162 const MESSAGE_TYPE* message; 00163 #if DEBUG 00164 assert(a_task && a_message); 00165 task = dynamic_cast<TASK_TYPE*>(a_task); 00166 message = dynamic_cast<const MESSAGE_TYPE*>(a_message); 00167 assert(task && message); 00168 #else 00169 task = static_cast<TASK_TYPE*>(a_task); 00170 message = static_cast<const MESSAGE_TYPE*>(a_message); 00171 #endif 00172 00173 (task->*m_receiver_function)(message); 00174 } 00175 00176 00177 #endif // _UT_MESSAGE_RECEIVER_H_