www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C++ MSG_MAP marcro -> Mixin

reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 19 2007
parent reply BLS <nanali nospam-wanadoo.fr> writes:
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;
Sep 19 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Sep 19 2007
parent reply BLS <nanali nospam-wanadoo.fr> writes:
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
Sep 20 2007
parent BLS <nanali nospam-wanadoo.fr> writes:
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.
Sep 20 2007