YAP: variadic templates via arrays and static foreach

Serg Kovrov kovrov at no.spam
Wed Aug 9 16:37:44 PDT 2006


Hi All,
Sorry for bothering, but another proposal...
Idea is to use some sort of 'static foreach' for array parameters.

> struct MSG
> {
>   int msg;
>   int function() hdlr;
> }
> 
> template msgHdlr(MSG[] T)
> {
>   int handleMessage(int msg, int cmd)
>   {
>     switch (msg)
>     {
>       case WM_MOVE:
>         onMove();
>         break;
>       static foreach(MSG m; T)  //  <-- here
>       {
>         case mixin!(m.msg):     //  <-- here
>           mixin!(m.hdlr)();     //  <-- here
>           break;
>       }
>       default:
>     }
>   }
> }
> 
> static MSG[] messages
> [
>   { WM_CREATE, &onCreate },
>   { WM_CLOSE,  &onClose },
> ]
> 
> class MyWindow
> {
>   mixin msgHdlr!(messages) //  <-- and finally here
> }

This code should result same as followed:

> class MyWindow
> {
>   int handleMessage(int msg, int cmd)
>   {
>     switch (msg)
>     {
>       case WM_MOVE:
>         onMove();
>         break;
>       case WM_CREATE:
>         onCreate();
>         break;
>       case WM_CLOSE:
>         onClose();
>         break;
>       default:
>     }
>   }
> }

Inspired by message map macros in WTL/MFC

-- 
serg.



More information about the Digitalmars-d mailing list