Click here to Skip to main content
15,881,172 members
Articles / Desktop Programming / WTL
Article

Good bye, BEGIN_MSG_MAP!

Rate me:
Please Sign up or sign in to vote.
3.46/5 (17 votes)
19 Jul 20052 min read 70.8K   384   18   12
A replacement for BEGIN_MSG_MAP macros, using the Boost.MPL library.

Preface

Four yeas ago, I made a program. Everything WTL originally had was useless except Win32 thin wrappers. As CUpdateUI was the one of them, I made the replacement by macros. Later, I read the book, C++ Template Metaprogramming, and I was inspired by the sample code, the finite state machine. That could remove the macros and the result was named ketchup, intended to replace BEGIN_MSG_MAP of ATL/WTL.

In time, the experience of making biscuit gave me the way of avoiding compile-time crashes. Now that ketchup is the type-safe synonym of BEGIN_MSG_MAP to help WTL catch up the modern programming.

Introduction

Ketchup is a message map generator implemented using class templates. The templates allow us to write type-safe BEGIN_MSG_MAP.

A simple BEGIN_MSG_MAP macro snippet:

BEGIN_MSG_MAP(CMainFrame)
  MESSAGE_HANDLER(WM_CREATE, OnCreate)
  COMMAND_ID_HANDLER(ID_APP_EXIT, OnFileExit)
  COMMAND_ID_HANDLER(ID_FILE_NEW, OnFileNew)
  COMMAND_ID_HANDLER(ID_APP_ABOUT, OnAppAbout)
  CHAIN_MSG_MAP(WTL::CFrameWindowImpl<CMainFrame>)
END_MSG_MAP()

is approximated using Ketchup's facilities as seen in this code snippet:

begin_msg_map
<
  message_handler<WM_CREATE, &_::OnCreate>,
  command_id_handler<ID_APP_EXIT, &_::OnFileExit>,
  command_id_handler<ID_FILE_NEW, &_::OnFileNew>,
  command_id_handler<ID_APP_ABOUT, &_::OnAppAbout>,
  chain_msg_map< WTL::CFrameWindowImpl<CMainFrame> >
>
end_msg_map;

Requirements

Microsoft Visual C++ .NET version 7.1, WTL 7.5 and Boost C++ libraries (no build required). I did test the demo in Visual C++ .NET standard edition with Visual C++ toolkit 2003. Ketchup itself is a header-only template library.

Quick Start

  1. Include a header and derive from ketchup::message_processor<CMainFrame>:
    #include "ketchup/ketchup.hpp"
    
    class CMainFrame : 
      public WTL::CFrameWindowImpl<CMainFrame>,
      public WTL::CUpdateUI<CMainFrame>,
      public WTL::CMessageFilter, public WTL::CIdleHandler,
      public ketchup::message_processor<CMainFrame>
    {
  2. Define message handlers:
    private:
      LRESULT OnCreate(UINT /*uMsg*/, WPARAM /*wParam*/, 
                     LPARAM /*lParam*/, BOOL& /*bHandled*/)
      {
        // create command bar window
        HWND hWndCmdBar = m_CmdBar.Create(m_hWnd, rcDefault, 
                        NULL, ATL_SIMPLE_CMDBAR_PANE_STYLE);
        // attach menu
        m_CmdBar.AttachMenu(GetMenu());
        // ...
      }
      // ...
  3. Let CMainFrame a message map:
    public:
      begin_msg_map
      <
        message_handler<WM_CREATE, &_::OnCreate>,
        command_id_handler<ID_APP_EXIT, &_::OnFileExit>,
        command_id_handler<ID_FILE_NEW, &_::OnFileNew>,
        command_id_handler<ID_VIEW_TOOLBAR, &_::OnViewToolBar>,
        command_id_handler<ID_VIEW_STATUS_BAR, &_::OnViewStatusBar>,
        command_id_handler<ID_APP_ABOUT, &_::OnAppAbout>,
        chain_msg_map< WTL::CUpdateUI<CMainFrame> >,
        chain_msg_map< WTL::CFrameWindowImpl<CMainFrame> >
      >
      end_msg_map;
  4. Finally, override CMessageMap::ProcessWindowMessage as BEGIN_MSG_MAP insidiously does, using process_window_message provided by ketchup::message_processor<CMainFrame>:
      virtual BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg,
        WPARAM wParam, LPARAM lParam, LRESULT& lResult, DWORD dwMsgMapID = 0)
      {
        return process_window_message(hWnd, uMsg, wParam, 
                            lParam, lResult, dwMsgMapID);
      }
    };

Bear in mind that declarations of message handlers should be placed before the entry and C++ Standard doesn't allow you to abbreviate the syntax of member function pointers.

Points of interest

The last point is the performance. The program size seems not to be a problem. VC++ 7.1 generates the same size program as the original one, because Ketchup's message map is almost same as BEGIN_MSG_MAP. But VC++ 7.1 can't inline message handlers, unlike BEGIN_MSG_MAP. Could this be a problem of the speed?

Well, I did not intend to emulate the appearance of BEGIN_MSG_MAP. It is not just a syntax sugar but the coincidence as a result of naming consistency with ATL/WTL. It was an amazing discovery for me.

By the way, Ketchup must be the first application using Boost.Xpressive for the implementation.

References

History

  • 23rd May, 2005 - version 0.910 (Initial release).
  • 27th May, 2005 - version 0.940.
  • 30th May, 2005 - version 0.950 (class_trace and debugging_entry added).
  • 12th Jun, 2005 - version 0.951 (debugging_entry removed; empty_entry and debug_entry added).
  • 20th Jul, 2005 - version 0.9992.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Japan Japan
I am worried about my poor English...

Comments and Discussions

 
AnswerI don't get it Pin
auge__29-Jan-13 22:48
auge__29-Jan-13 22:48 
General"&_::",too ugly Pin
ZhangLi16-May-08 21:23
ZhangLi16-May-08 21:23 
GeneralFor the newest version of ketchup message map library, click here! Pin
mb2sync6-Jul-05 19:15
mb2sync6-Jul-05 19:15 
GeneralI really like your idea! Pin
yoco31513-Jun-05 14:02
yoco31513-Jun-05 14:02 
GeneralRe: I really like your idea! Pin
mb2sync14-Jun-05 6:09
mb2sync14-Jun-05 6:09 
GeneralThoughts Pin
peterchen11-Jun-05 13:50
peterchen11-Jun-05 13:50 
GeneralRe: Thoughts Pin
mb2sync11-Jun-05 14:55
mb2sync11-Jun-05 14:55 
GeneralThoughts Pin
Jörgen Sigvardsson28-May-05 10:29
Jörgen Sigvardsson28-May-05 10:29 
GeneralRe: Thoughts Pin
mb2sync28-May-05 11:49
mb2sync28-May-05 11:49 
GeneralRe: Thoughts Pin
Jörgen Sigvardsson28-May-05 11:54
Jörgen Sigvardsson28-May-05 11:54 
GeneralRe: Thoughts Pin
mb2sync28-May-05 17:06
mb2sync28-May-05 17:06 
GeneralGOOD BYE BOOST TMP! Pin
Anonymous27-May-05 23:44
Anonymous27-May-05 23:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.