(续上)例三、用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 } / }; /
嘿嘿,就这么几个宏,就构造出一个消息数组来.
|