C++消息映射原理机制源代码[完全不是MFC中的消息映射机制]

完全不是MFC中的消息映射机制,即没有用到MFC中的一点消息机制。
但原理却是一样的,是个麻雀虽小五脏俱全的消息映射原理,简单实现了
SendMail(APPMSG_START, NULL, APPID_TEST1, 0)的源代码,经测试成功。
PostMail同理可自行实现。 这个程序的亮点是单例设计模式,数据结构的链表设计
和消息的传递方式,有兴趣的行家可以研究下。


发觉代码有点多,所以还是直接给出下载源代码的链接网址吧。
源代码下载  http://download.csdn.net/detail/hixi2007/9594104


代码如下:
MailMsg.cpp
// MailMsg.cpp : Defines the entry point for the console application.
//


#include "stdafx.h"


#include "apptest1.h"
#include "apptest2.h"
#include "apptest3.h"
#include "apptest4.h"


#include "uiappmanager.h"
#include "uimailserver.h"






int main(int argc, char* argv[])
{
UiGetMailServer()->RegisterForMail(UiGetAppManager());




UiGetAppManager()->AddApp(TestAppC::GetInstance()); 
UiGetAppManager()->AddApp(Test2AppC::GetInstance());
UiGetAppManager()->AddApp(Test3AppC::GetInstance());
UiGetAppManager()->AddApp(Test4AppC::GetInstance());




UiGetTestApp()->SendMail(APPMSG_START, NULL, APPID_TEST1, 0);
UiGetTestApp()->SendMail(APPMSG_RESUME, NULL, APPID_TEST1, 0);




UiGetTest2App()->SendMail(APPMSG_START, NULL, APPID_TEST2, 0);
UiGetTest2App()->SendMail(APPMSG_STOP, NULL, APPID_TEST2, 0);




UiGetTest3App()->SendMail(APPMSG_START, NULL, APPID_TEST3, 0);
UiGetTest3App()->SendMail(APPMSG_SUSPEND, NULL, APPID_TEST3, 0);




UiGetTest4App()->SendMail(APPMSG_START, NULL, APPID_TEST4, 0);
UiGetTest4App()->SendMail(APPMSG_SUSPEND, NULL, APPID_TEST4, 0);
UiGetTest4App()->SendMail(APPMSG_RESUME, NULL, APPID_TEST4, 0);
UiGetTest4App()->SendMail(APPMSG_STOP, NULL, APPID_TEST4, 0);




printf("Hello World!\n");




getchar();
return 0;
}




uimailserver.h
/*************************************************************
*
* This Software is the property of VIA Telecom, Inc. and may only be used pursuant to a license from VIA Telecom, Inc.  

* Any unauthorized use inconsistent with the terms of such license is strictly prohibited.

* Copyright (c) 2006-2010 VIA Telecom, Inc.  All rights reserved.
*
*************************************************************/
/**************************************************************************************************
* %version: 3 %  %instance: HZPT_1 %   %date_created: Fri Mar 23 15:18:20 2007 %  %created_by: jingzhang %  %derived_by: jingzhang %
**************************************************************************************************/


#ifndef UIMAILSERVER_H
#define UIMAILSERVER_H 
/******************************************************************************

* FILE NAME   : uimailserver.h
*
* DESCRIPTION :
*
*   This is the interface for the MailServerC class..
*
* HISTORY     :
*
*   See Log at end of file
*
******************************************************************************/
//#include "exeapi.h"
//#include "sysdefs.h"


#include "uidefs.h"
#include "uilinkedlist.h"
#include "uimailclient.h"




#define UI_EXTERNAL_MAIL_TYPE  EXE_MAILBOX_1
#define UI_INTERNAL_MAIL_TYPE  EXE_MAILBOX_2 
#define UI_PAINT_MAIL_TYPE  EXE_MAILBOX_3 


#define UI_EXTERNAL_MAILBOX_ID   EXE_MAILBOX_1_ID
#define UI_INTERNAL_MAILBOX_ID   EXE_MAILBOX_2_ID
#define UI_PAINT_MAILBOX_ID   EXE_MAILBOX_3_ID




//! Mail Server class define 
class MailServerC
{
public:
  virtual ~MailServerC();


  static MailServerC* GetInstance(void);


  void RegisterForMail(MailClientC* MailClientP);
  void UnregisterForMail(MailClientC* MailClientP);


  int SendMail(int MailMsgId, WinHandleT WinHandle = WIN_HANDLE_NULL, int ParamA = 0, int ParamB = 0);
  bool PostMail(int MailMsgId, WinHandleT WinHandle = WIN_HANDLE_NULL, int ParamA = 0, int ParamB = 0, ParamTypeT ParamType = BY_VALUE);


  void ProcessMail(int TaskIndex);
//  void EmptyMailbox(int TaskIndex, ExeMailboxIdT MailboxID);


#ifdef UI_DEBUG
  bool DumpData(int LogFile);
#endif


private:
  MailServerC();
  
  bool GetMail(int TaskIndex, MailMsgT* MailMsgP);
  int DispatchMail(const MailMsgT* MailMsgP);
  void FreeMail(int TaskIndex, MailMsgT* MailMsgP);


  MailMsgT* PrepareMail(int MailMsgId, WinHandleT WinHandle, int ParamA, int ParamB, ParamTypeT ParamType);


private:
  static MailServerC* _instance;
  static const int TASK_ID[2]; //ExeTaskIdT
  
  LinkedListC mRegisteredClients;
  void* mMailMsgP[2];
  int mEventStatus[2];
  int mLevelIndex[2];
  
#ifdef UI_DEBUG
  enum
  {
    MAX_MSG_HISTORY_ENTRIES = 10
  };
  
  MailMsgT mMsgHistory[2][MAX_MSG_HISTORY_ENTRIES];
#endif
};


//! Get the unique instance of the MailServerC
inline MailServerC* UiGetMailServer(void)
{
  return MailServerC::GetInstance();
}


/*****************************************************************************
* $Log: uimailserver.h $
* Revision 1.1  2006/11/26 22:12:08  yliu
* Initial revision
* Revision 1.6  2006/08/14 20:36:46  yliu
* change default handler from NULL to WIN_HANDLE_NULL
* Revision 1.5  2006/07/18 12:57:39  yliu
* WindowC implementation
* Revision 1.4  2006/06/27 21:28:47  dwang
* Optimize
* Revision 1.3  2006/06/26 21:34:35  yliu
* cosmetic change
* Revision 1.2  2006/06/22 14:59:38  yliu
* Add implementation of MailServerC
* Revision 1.1  2006/06/21 00:09:01  yliu
* Initial revision
*****************************************************************************/


/*****************************************************************************
* End of File
*****************************************************************************/
#endif
/**Log information: \main\vtui2_5x\1 2008-06-05 12:04:23 GMT yliu
** HREF#286**/
/**Log information: \main\vtui2_5x\2 2008-08-25 14:12:51 GMT yliu
** HREF#1489**/
/**Log information: \main\vtui2_5x\3 2008-08-26 02:56:15 GMT yliu
** HREF#1489**/


uimailclient.h
/*************************************************************
*
* This Software is the property of VIA Telecom, Inc. and may only be used pursuant to a license from VIA Telecom, Inc.  

* Any unauthorized use inconsistent with the terms of such license is strictly prohibited.

* Copyright (c) 2006-2010 VIA Telecom, Inc.  All rights reserved.
*
*************************************************************/
/**************************************************************************************************
* %version: 4 %  %instance: HZPT_1 %   %date_created: Fri Mar 23 15:18:11 2007 %  %created_by: jingzhang %  %derived_by: jingzhang %
**************************************************************************************************/


#ifndef UIMAILCLIENT_H
#define UIMAILCLIENT_H 
/******************************************************************************

* FILE NAME   : uimailclient.h
*
* DESCRIPTION :
*
*   This is the interface for the MailClientC class..
*
* HISTORY     :
*
*   See Log at end of file
*
******************************************************************************/
#include "uidefs.h"
typedef int WinHandleT;


class MailClientC;


typedef void (MailClientC::*MailHandlerT)(void);


//! mail handler type defines, the name indicate the handler's return value and param type.
enum MailHandlerTypeT
{
  MHType_v_v,
  MHType_v_vp,
  MHType_v_b,
  MHType_v_w,
  MHType_v_wl,
  MHType_v_ii,
  MHType_v_W,
  MHType_b_v,
  MHType_b_vp,
  MHType_b_w,
  MHType_b_wl,
  MHType_l_v,
  MHType_v_wp,
  MHType_END
};


//! MailMapEntry struct define
struct MailMapEntryT
{
  int MailMsgId; //!< the begin id of the mail message group
  int LastMsgId; //!< the end id of the mail message group
  MailHandlerTypeT Type; //!< mail message handler type
  MailHandlerT MailHandlerP; //!< the point to the mail messsage handler
};


//! MailMap struct define
struct MailMapT
{
  const MailMapT* BaseMapP; //!< point to the MailMapT of its base class 
  const MailMapEntryT* MailEntryP; //!< point to the mail map table
};


/*!
\brief Begins the definition of the mail map


\param ThisClass -[in] specifies the name of the class whose mail map this is
\param BaseClass -[in] specifies the name of the base class of ThisClass
*/
#define BEGIN_MAIL_MAP(ThisClass, BaseClass) \
 const MailMapT* ThisClass::GetMailMap() const \
  { return &ThisClass::MailMap; } \
 const MailMapT ThisClass::MailMap = \
 { &BaseClass::MailMap, &ThisClass::MailEntries[0] }; \
   const MailMapEntryT ThisClass::MailEntries[] = \
 { \


//! Ends the definiton of the mail map
#define END_MAIL_MAP() \
  {0, 0, MHType_END, (MailHandlerT)0 } \
 };


//! Declares the mail map
#define DECLARE_MAIL_MAP() \
private: \
 static const MailMapEntryT MailEntries[]; \
protected: \
 static const MailMapT MailMap; \
 virtual const MailMapT* GetMailMap() const;


//! Terminates the mail map
#define DEFINE_TERMINATING_MAIL_MAP(ThisClass) \
const MailMapT ThisClass::MailMap = \
{ \
  NULL, \
  &ThisClass::MailEntries[0] \
}; \
const MailMapT* ThisClass::GetMailMap() const \
{ \
  return &ThisClass::MailMap; \
} \
const MailMapEntryT ThisClass::MailEntries[] = \
{ \
  {0, 0, MHType_END, (MailHandlerT)0 } \
};


#define ON_MSG_VOID_VOID(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_v_v, \
    (MailHandlerT)(void (MailClientC::*)(void))&MailFunc },


#define ON_MSG_VOID_VOIDPTR(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_v_vp, \
    (MailHandlerT)(void (MailClientC::*)(void*))&MailFunc },


#define ON_MSG_VOID_WORD(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_v_w, \
    (MailHandlerT)(void (MailClientC::*)(int))&MailFunc },


#define ON_MSG_VOID_WORDLONG(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_v_wl, \
    (MailHandlerT)(void (MailClientC::*)(int, int))&MailFunc },


#define ON_MSG_BOOL_VOID(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_b_v, \
    (MailHandlerT)(bool (MailClientC::*)(void))&MailFunc },


#define ON_MSG_BOOL_VOIDPTR(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_b_vp, \
    (MailHandlerT)(bool (MailClientC::*)(void*))&MailFunc },


#define ON_MSG_BOOL_WORD(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_b_w, \
    (MailHandlerT)(bool (MailClientC::*)(int))&MailFunc },


#define ON_MSG_BOOL_WORDLONG(MailMsgId, MailFunc) \
 { MailMsgId, MailMsgId, MHType_b_wl, \
    (MailHandlerT)(bool (MailClientC::*)(int, int))&MailFunc },


#define ON_MSG_VOID_WORDPTR(MailMsgId, LastMailMsgId, MailFunc) \
  { MailMsgId, LastMailMsgId, MHType_v_wp, \
    (MailHandlerT)(void (MailClientC::*)(int,void*))&MailFunc },


//! Mail Client class define  
class MailClientC
{
public:
  MailClientC();
  virtual ~MailClientC();


  int SendMail(int MailMsgId, WinHandleT WinHandle = WIN_HANDLE_NULL, int ParamA = 0, int ParamB = 0);
  bool PostMail(int MailMsgId, WinHandleT WinHandle = WIN_HANDLE_NULL, int ParamA = 0, int ParamB = 0, ParamTypeT ParamType = BY_VALUE);


  virtual int ProcessMail(int MailMsgId, WinHandleT WinHandle, int ParamA, int ParamB);


  int GetMailMask(void) const;
  void SetMailMask(int MailMask);


protected:
  virtual int DefMailProc(int MailMsgId, WinHandleT WinHandle, int ParamA, int ParamB);


private:
  bool MailMapProc(int MailMsgId, int ParamA, int ParamB, int* ResultP);


  int mMailMask;


  DECLARE_MAIL_MAP()
};


/*!
\brief Get the mail mask flags


\retval the mail mask double word
*/
inline int MailClientC::GetMailMask(void) const
{
  return mMailMask;
}


/*!
\brief Set the mail mask flags


\param MailMask -[in] the mask to be set
*/ 
inline void MailClientC::SetMailMask(int MailMask)
{
  mMailMask = MailMask;
}


/*****************************************************************************
* $Log: uimailclient.h $
* Revision 1.1  2006/11/26 22:12:04  yliu
* Initial revision
* Revision 1.9  2006/10/09 10:15:41  dwang
* Add one mail type
* Revision 1.8  2006/08/14 20:36:45  yliu
* change default handler from NULL to WIN_HANDLE_NULL
* Revision 1.7  2006/07/18 12:57:38  yliu
* WindowC implementation
* Revision 1.6  2006/06/29 17:09:28  yliu
* add parameter to message map macros
* Revision 1.5  2006/06/29 11:26:40  yliu
* Add some message map macro
* Revision 1.4  2006/06/27 22:52:54  yliu
* Add partly implementation of MailMapProc
* Revision 1.3  2006/06/27 21:28:46  dwang
* Optimize
* Revision 1.2  2006/06/26 21:34:35  yliu
* cosmetic change
* Revision 1.1  2006/06/21 00:09:04  yliu
* Initial revision
*****************************************************************************/


/*****************************************************************************
* End of File
*****************************************************************************/
#endif




源代码下载  http://download.csdn.net/detail/hixi2007/9594104

你可能感兴趣的:(C++,c++,消息,原理机制,源代码,MFC)