C++ Macro to D mixin template, delegates Please help

Henning Hasemann hhasemann at web.de
Thu Sep 20 13:44:34 PDT 2007



Does this help you?


import std.stdio;

template MessageMap(Mappings ...) {
  void newMsgProc(uint uID) {
     foreach(mapping; Mappings) {
       if(mapping.matches(uID))
         mapping.executeAction();
     }
  }
}

struct OnClose(alias fn) {
  alias fn executeAction;
  static bool matches(uint uid) { return uid == 5; }
}

struct OnRange(uint a, uint b, alias fn) {
  alias fn executeAction;
  static bool matches(uint uid) { return uid >= a && uid <= b; }
}


class Foo {
  void foo() {
    writefln("foo called");
  }
  void bar() {
    writefln("bar called");
  }

  mixin MessageMap!(
    OnClose!(foo),
    OnRange!(1, 3, bar)
  );

}

void main() {
  auto f = new Foo;
  f.newMsgProc(5);
  f.newMsgProc(2);
}

-- 
GPG Public Key:
http://keyserver.ganneff.de:11371/pks/lookup?op=get&search=0xDDD6D36D41911851
Fingerprint: 344F 4072 F038 BB9E B35D  E6AB DDD6 D36D 4191 1851


More information about the Digitalmars-d-learn mailing list