Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A macro

21 views
Skip to first unread message

Alf P. Steinbach

unread,
Dec 3, 2016, 3:56:18 AM12/3/16
to
I just sat down and started coding a little Windows desktop program
again, some days ago.

I remember that the macro I used for dispatching API level window
messages to C++ member functions, in C++03, was supported by some
complicated templates and was so horrendous that I posted it on Facebook
as a shock-and-awe spaghetti code joke.

With modern C++ it's much cleaner. Like the C++03 version this macro
also just delegates the extraction of function arguments from the
message data, to a message-specific macro from the mostly now
undocumented <windowsx.h> header, e.g. the HANDLE_WM_PAINT macro for the
WM_PAINT message. But all that old complexity for matching the
<windowsx.h> macro's call to my member function with one fewer arguments
(namely, no window handle argument at the start) is now done by
`std::mem_fn`. :)

[code]
#define WINAPI_DISPATCH_TO_MEMFN( message_name, memfunc, m ) \
HANDLE_ ## message_name ( \
this, m.wparam, m.lparam, std::mem_fn( &memfunc ) \
)

namespace my {
using cppx::No_copy_or_move;
namespace basic_main_window = winapi::basic_main_window;

class Main_window
: public No_copy_or_move
, private winapi::Message_handler
{
private:
winapi::Subclassing subclassing_;

void on_wm_paint()
{
PAINTSTRUCT state;
HDC const dc = ::BeginPaint( api_handle(), &state );
::Ellipse( dc, 10, 10, 200, 80 );
::EndPaint( api_handle(), &state );
}

auto on( winapi::Message const& m ) noexcept
-> winapi::Lresult override
{
switch( m.message_id )
{
//case WM_PAINT: return HANDLE_WM_PAINT(
// this,
// m.wparam, m.lparam,
// std::mem_fn( &Main_window::on_wm_paint )
// );

case WM_PAINT: return WINAPI_DISPATCH_TO_MEMFN(
WM_PAINT, Main_window::on_wm_paint, m
);
}
return subclassing_.original_processing( m );
}
[/code]

Of course it would be more nice without a macro, at all, and without
having to qualify the member function name.

I don't see solutions?


Cheers!,

- Alf

Öö Tiib

unread,
Dec 3, 2016, 7:13:43 AM12/3/16
to
On Saturday, 3 December 2016 10:56:18 UTC+2, Alf P. Steinbach wrote:
>
> I don't see solutions?

I don't really see problems. Macro looks fine. The whole puzzle of
message binding is meta-programming problem and macros are often used
in meta-programming.
0 new messages