struct mbuf
{
struct m_hdr mhdr;
union {
struct
{
struct pkthdr MH_pkthdr; /* M_PKTHDR set */
union
{
struct m_ext MH_ext; /* M_EXT set */
char MH_databuf[MHLEN];
} MH_dat;
} MH;
char M_databuf[MLEN]; /* !M_PKTHER, !M_EXT*/
} M_dat;
};
#define m_next m_hdr.mh_next #define m_len m_hdr.mh_len #define m_data m_hdr.mh_data ... ... #define m_pkthdr M_dat.MH.MH_pkthdr #define m_pktdat M_dat.MH.MH_dat.MH_databuf ... ...
#ifndef _CRTAPI1 #if _MSC_VER >= 800 && _M_IX86 >= 300 #define _CRTAPI1 __cdecl #else /* _MSC_VER >= 800 && _M_IX86 >= 300 */ #define _CRTAPI1 #endif /* _MSC_VER >= 800 && _M_IX86 >= 300 */ #endif /* _CRTAPI1 */ #ifndef _SIZE_T_DEFINED typedef unsigned int size_t; #define _SIZE_T_DEFINED #endif /* _SIZE_T_DEFINED */ #ifndef _MAC #ifndef _WCHAR_T_DEFINED typedef unsigned short wchar_t; #define _WCHAR_T_DEFINED #endif /* _WCHAR_T_DEFINED */ #endif /* _MAC */ #ifndef _NLSCMP_DEFINED #define _NLSCMPERROR 2147483647 /* currently == INT_MAX */ #define _NLSCMP_DEFINED #endif /* _NLSCMP_DEFINED */
请问,这些指示宏如何取代呢?如果真的是没有了这些宏,实现起来就更麻烦了吧。
例三、用C宏,自动生成代码这方面的例子也是多得很,不过有鉴于很多朋友不用很多编译器,不做嵌入式的开发,我就举个win平台的例子吧。我们知道MFC实现了Windows的消息映射,比如:
ON_COMMAND(IDM_ABOUT, OnAbout) ON_COMMAND(IDM_FILENEW, OnFileNew)
它是如何实现的IDM_ABOUT和OnAbout的关联的呢?这要用到几个宏。
#define DECLARE_MESSAGE_MAP()
private:
static const AFX_MSGMAP_ENTRY _messageEntries[];
protected:
static AFX_DATA const AFX_MSGMAP messageMap;
virtual const AFX_MSGMAP* GetMessageMap() const;
#define BEGIN_MESSAGE_MAP(theClass, baseClass)
const AFX_MSGMAP* theClass::GetMessageMap() const
{ return &theClass::messageMap; }
AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap =
{ &baseClass::messageMap, &theClass::_messageEntries[0] };
AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] =
{
#define ON_COMMAND(id, memberFxn)
{ WM_COMMAND, 0, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)memberFxn },
#define END_MESSAGE_MAP()
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
};
#define DECLARE_MESSAGE_MAP()
private:
static const AFX_MSGMAP_ENTRY _messageEntries[];
protected:
static AFX_DATA const AFX_MSGMAP messageMap;
virtual const AFX_MSGMAP* GetMessageMap() const;
#define BEGIN_MESSAGE_MAP(theClass, baseClass)
const AFX_MSGMAP* theClass::GetMessageMap() const
{ return &theClass::messageMap; }
AFX_COMDAT AFX_DATADEF const AFX_MSGMAP theClass::messageMap =
{ &baseClass::messageMap, &theClass::_messageEntries[0] };
AFX_COMDAT const AFX_MSGMAP_ENTRY theClass::_messageEntries[] =
{
#define ON_COMMAND(id, memberFxn)
{ WM_COMMAND, 0, (WORD)id, (WORD)id, AfxSig_vv, (AFX_PMSG)memberFxn },
#define END_MESSAGE_MAP()
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
};





