Thread overview
C++ MSG_MAP marcro -> Mixin
Sep 19, 2007
BLS
Sep 19, 2007
BLS
Sep 20, 2007
Daniel Keep
Sep 20, 2007
BLS
Sep 20, 2007
BLS
September 19, 2007
Hi,
MFC and wxWidgets are supporting MESSAGEMAPS macros. I would like to port them to D.

class CMsg
{
public:

	virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM lParam,LRESULT lResult)
	{
		return FALSE;
	}
};

Okay, here iy goes ....
#define BEGIN_MSG_MAP() \
public: \
	virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM lParam,LRESULT& lResult) \
	{ \
		
#define ON_MESSAGE(Msg, vfunc) \
	if(uID == Msg) \
	{ \
		vfunc(uID, wParam, lParam); \
		lResult = 0; \
		return TRUE; \
	}

#define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
	if(uID >= MsgF && uID <= MsgL) \
	{ \
		lResult=vfunc(uID, wParam, lParam); \
		return TRUE; \
	}

#define ON_COMMAND_CONTROL(iControl, iEvent, vfunc) \
	if(uID == WM_COMMAND && iControl == LOWORD(wParam) && iEvent == HIWORD(wParam)) \
	{ \
		vfunc(HIWORD(wParam), LOWORD(wParam), (HWND)lParam); \
		lResult = 0; \
		return TRUE; \
	}

and so on....

Is it possible to replace this C++ Macros with D2 Mixins and compile time manipulation of strings ?
How ?
Thanks, Bjoern
September 19, 2007
BLS schrieb:
> Hi,
> MFC and wxWidgets are supporting MESSAGEMAPS macros. I would like to port them to D.
> 
> class CMsg
> {
> public:
> 
>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM lParam,LRESULT lResult)
>     {
>         return FALSE;
>     }
> };
> 
> Okay, here iy goes ....
> #define BEGIN_MSG_MAP() \
> public: \
>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM lParam,LRESULT& lResult) \
>     { \
>        #define ON_MESSAGE(Msg, vfunc) \
>     if(uID == Msg) \
>     { \
>         vfunc(uID, wParam, lParam); \
>         lResult = 0; \
>         return TRUE; \
>     }
> 
> #define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
>     if(uID >= MsgF && uID <= MsgL) \
>     { \
>         lResult=vfunc(uID, wParam, lParam); \
>         return TRUE; \
>     }
> 
> #define ON_COMMAND_CONTROL(iControl, iEvent, vfunc) \
>     if(uID == WM_COMMAND && iControl == LOWORD(wParam) && iEvent == HIWORD(wParam)) \
>     { \
>         vfunc(HIWORD(wParam), LOWORD(wParam), (HWND)lParam); \
>         lResult = 0; \
>         return TRUE; \
>     }
> 
> and so on....
> 
> Is it possible to replace this C++ Macros with D2 Mixins and compile time manipulation of strings ?
> How ?
> Thanks, Bjoern

And I really wonder myself how to (probabely) replace this stuff using Tango's Signal Slot implementation.... but I guess I miss something;
September 20, 2007

BLS wrote:
> BLS schrieb:
>> Hi,
>> MFC and wxWidgets are supporting MESSAGEMAPS macros. I would like to
>> port them to D.
>>
>> class CMsg
>> {
>> public:
>>
>>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
>> lParam,LRESULT lResult)
>>     {
>>         return FALSE;
>>     }
>> };
>>
>> Okay, here iy goes ....
>> #define BEGIN_MSG_MAP() \
>> public: \
>>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
>> lParam,LRESULT& lResult) \
>>     { \
>>        #define ON_MESSAGE(Msg, vfunc) \
>>     if(uID == Msg) \
>>     { \
>>         vfunc(uID, wParam, lParam); \
>>         lResult = 0; \
>>         return TRUE; \
>>     }
>>
>> #define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
>>     if(uID >= MsgF && uID <= MsgL) \
>>     { \
>>         lResult=vfunc(uID, wParam, lParam); \
>>         return TRUE; \
>>     }
>>
>> #define ON_COMMAND_CONTROL(iControl, iEvent, vfunc) \
>>     if(uID == WM_COMMAND && iControl == LOWORD(wParam) && iEvent ==
>> HIWORD(wParam)) \
>>     { \
>>         vfunc(HIWORD(wParam), LOWORD(wParam), (HWND)lParam); \
>>         lResult = 0; \
>>         return TRUE; \
>>     }
>>
>> and so on....
>>
>> Is it possible to replace this C++ Macros with D2 Mixins and compile
>> time manipulation of strings ?
>> How ?
>> Thanks, Bjoern
> 
> And I really wonder myself how to (probabely) replace this stuff using Tango's Signal Slot implementation.... but I guess I miss something;

With enough CTFE, you could do it.  I've actually been thinking about making a generator ctfe compiler (but haven't got *any* free time at the moment).

If I were doing this, I'd probably try something along these lines:

mixin(wxMsgMap(`
	on(id1) handler1;
	on(id2) handler2;
	on(id3..id6) handler3;
`));

Parsing that shouldn't be too difficult.  Of course, there are other ways of constructing this:

mixin wxMsgMap!(
    OnMessage!(id1, handler1),
    OnMessage!(id2, handler2),
    OnMessage!(id3, id6, handler3)
);

Where wxMsgMap takes a tuple of templated OnMessage structs.

Just some ideas.

	-- Daniel
September 20, 2007
Daniel Keep schrieb:
> 
> BLS wrote:
>> BLS schrieb:
>>> Hi,
>>> MFC and wxWidgets are supporting MESSAGEMAPS macros. I would like to
>>> port them to D.
>>>
>>> class CMsg
>>> {
>>> public:
>>>
>>>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
>>> lParam,LRESULT lResult)
>>>     {
>>>         return FALSE;
>>>     }
>>> };
>>>
>>> Okay, here iy goes ....
>>> #define BEGIN_MSG_MAP() \
>>> public: \
>>>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
>>> lParam,LRESULT& lResult) \
>>>     { \
>>>        #define ON_MESSAGE(Msg, vfunc) \
>>>     if(uID == Msg) \
>>>     { \
>>>         vfunc(uID, wParam, lParam); \
>>>         lResult = 0; \
>>>         return TRUE; \
>>>     }
>>>
>>> #define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
>>>     if(uID >= MsgF && uID <= MsgL) \
>>>     { \
>>>         lResult=vfunc(uID, wParam, lParam); \
>>>         return TRUE; \
>>>     }
>>>
>>> #define ON_COMMAND_CONTROL(iControl, iEvent, vfunc) \
>>>     if(uID == WM_COMMAND && iControl == LOWORD(wParam) && iEvent ==
>>> HIWORD(wParam)) \
>>>     { \
>>>         vfunc(HIWORD(wParam), LOWORD(wParam), (HWND)lParam); \
>>>         lResult = 0; \
>>>         return TRUE; \
>>>     }
>>>
>>> and so on....
>>>
>>> Is it possible to replace this C++ Macros with D2 Mixins and compile
>>> time manipulation of strings ?
>>> How ?
>>> Thanks, Bjoern
>> And I really wonder myself how to (probabely) replace this stuff using
>> Tango's Signal Slot implementation.... but I guess I miss something;
> 
> With enough CTFE, you could do it.  I've actually been thinking about
> making a generator ctfe compiler (but haven't got *any* free time at the
> moment).
> 
> If I were doing this, I'd probably try something along these lines:
> 
> mixin(wxMsgMap(`
> 	on(id1) handler1;
> 	on(id2) handler2;
> 	on(id3..id6) handler3;
> `));
> 
> Parsing that shouldn't be too difficult.  Of course, there are other
> ways of constructing this:
> 
> mixin wxMsgMap!(
>     OnMessage!(id1, handler1),
>     OnMessage!(id2, handler2),
>     OnMessage!(id3, id6, handler3)
> );
> 
> Where wxMsgMap takes a tuple of templated OnMessage structs.
> 
> Just some ideas.
> 
> 	-- Daniel

Thanks Daniel,
I am afraid I am not able to follow your suggestion...Can you figure it out please ? // I mean your second solution

However, let's say I'll use D template mixins instead of C macros

// The known parameters as follows
virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
lParam,LRESULT lResult)

So :
#define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
     if(uID >= MsgF && uID <= MsgL) \
     { \
        lResult=vfunc(uID, wParam, lParam); \
         return TRUE; \
     }


// Replaced with Template Mixin

Template ON_MESSAGE_RANGE(M1, M2, F) (M1 MsgF, M2 MsgL, F function(...) dg )
{
    typeof(dg) dg_m;
    if(uID >= MsgF && uID <= MsgL)
     {
        lResult = dg_m(uID, wParam, lParam);
         return TRUE;
     }
}

is a valid ?  I guess - F function(...) dg is not.
Bjoern
September 20, 2007
BLS schrieb:
> Daniel Keep schrieb:
>>
>> BLS wrote:
>>> BLS schrieb:
>>>> Hi,
>>>> MFC and wxWidgets are supporting MESSAGEMAPS macros. I would like to
>>>> port them to D.
>>>>
>>>> class CMsg
>>>> {
>>>> public:
>>>>
>>>>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
>>>> lParam,LRESULT lResult)
>>>>     {
>>>>         return FALSE;
>>>>     }
>>>> };
>>>>
>>>> Okay, here iy goes ....
>>>> #define BEGIN_MSG_MAP() \
>>>> public: \
>>>>     virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
>>>> lParam,LRESULT& lResult) \
>>>>     { \
>>>>        #define ON_MESSAGE(Msg, vfunc) \
>>>>     if(uID == Msg) \
>>>>     { \
>>>>         vfunc(uID, wParam, lParam); \
>>>>         lResult = 0; \
>>>>         return TRUE; \
>>>>     }
>>>>
>>>> #define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
>>>>     if(uID >= MsgF && uID <= MsgL) \
>>>>     { \
>>>>         lResult=vfunc(uID, wParam, lParam); \
>>>>         return TRUE; \
>>>>     }
>>>>
>>>> #define ON_COMMAND_CONTROL(iControl, iEvent, vfunc) \
>>>>     if(uID == WM_COMMAND && iControl == LOWORD(wParam) && iEvent ==
>>>> HIWORD(wParam)) \
>>>>     { \
>>>>         vfunc(HIWORD(wParam), LOWORD(wParam), (HWND)lParam); \
>>>>         lResult = 0; \
>>>>         return TRUE; \
>>>>     }
>>>>
>>>> and so on....
>>>>
>>>> Is it possible to replace this C++ Macros with D2 Mixins and compile
>>>> time manipulation of strings ?
>>>> How ?
>>>> Thanks, Bjoern
>>> And I really wonder myself how to (probabely) replace this stuff using
>>> Tango's Signal Slot implementation.... but I guess I miss something;
>>
>> With enough CTFE, you could do it.  I've actually been thinking about
>> making a generator ctfe compiler (but haven't got *any* free time at the
>> moment).
>>
>> If I were doing this, I'd probably try something along these lines:
>>
>> mixin(wxMsgMap(`
>>     on(id1) handler1;
>>     on(id2) handler2;
>>     on(id3..id6) handler3;
>> `));
>>
>> Parsing that shouldn't be too difficult.  Of course, there are other
>> ways of constructing this:
>>
>> mixin wxMsgMap!(
>>     OnMessage!(id1, handler1),
>>     OnMessage!(id2, handler2),
>>     OnMessage!(id3, id6, handler3)
>> );
>>
>> Where wxMsgMap takes a tuple of templated OnMessage structs.
>>
>> Just some ideas.
>>
>>     -- Daniel
> 
> Thanks Daniel,
> I am afraid I am not able to follow your suggestion...Can you figure it out please ? // I mean your second solution
> 
> However, let's say I'll use D template mixins instead of C macros
> 
> // The known parameters as follows
> virtual BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM
> lParam,LRESULT lResult)
> 
> So :
> #define ON_MESSAGE_RANGE(MsgF, MsgL, vfunc) \
>      if(uID >= MsgF && uID <= MsgL) \
>      { \
>         lResult=vfunc(uID, wParam, lParam); \
>          return TRUE; \
>      }
> 
> 
> // Replaced with Template Mixin
> 
> Template ON_MESSAGE_RANGE(M1, M2, F) (M1 MsgF, M2 MsgL, F function(...) dg )
> {
>     typeof(dg) dg_m;
>     if(uID >= MsgF && uID <= MsgL)
>      {
>         lResult = dg_m(uID, wParam, lParam);
>          return TRUE;
>      }
> }
> 
> is a valid ?  I guess - F function(...) dg is not.
> Bjoern
Okay, I think I have to works with delegates instead, bause I have to work on class-members.
probabely :

Template ON_MESSAGE_RANGE(M1, M2, D) (M1 MsgF, M2 MsgL, D delegate(...) dg )
{
    if(uID >= MsgF && uID <= MsgL)
     {
        lResult = dg(uID, wParam, lParam);
         return TRUE;
     }
}

Is a reasonable solution...
However, I am not sure.