YAP: variadic templates via arrays and static foreach

Derek Parnell derek at nomail.afraid.org
Wed Aug 9 18:07:43 PDT 2006


On Thu, 10 Aug 2006 02:37:44 +0300, Serg Kovrov wrote:

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

Let me introduce you to associative arrays ...

//-------------
import std.c.windows.windows;
import std.stdio;

alias int function() hdlr;
hdlr[int] Handlers; // -- AA of handlers.

static this()
{
    // Initialize the AA
    Handlers[WM_MOVE] = &onMove;
    Handlers[WM_CREATE] = &onCreate;
    Handlers[WM_CLOSE] = &onClose;
}

class MyWindow
{
  int handleMessage(int msg, int cmd)
  {
      int res;
      hdlr* h;

    if ((h = (msg in Handlers)) !is null)
        res = (*h)();

    return res;
  }
}

// Example functions.
int onMove()   { return 1; }
int onCreate() { return 2; }
int onClose()  { return 3; }

void main()
{
    auto w = new MyWindow;

    std.stdio.writefln("%s %s", WM_MOVE, w.handleMessage(WM_MOVE, 0));
    std.stdio.writefln("%s %s", WM_CREATE, w.handleMessage(WM_CREATE, 0));
    std.stdio.writefln("%s %s", WM_CLOSE, w.handleMessage(WM_CLOSE, 0));
}
//-----------


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
10/08/2006 11:05:38 AM



More information about the Digitalmars-d mailing list