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

Regan Heath regan at netmail.co.nz
Fri Sep 21 02:44:45 PDT 2007


BLS wrote:
> Regan Heath schrieb:
>> Henning Hasemann wrote:

I don't understand.  What doesn't the code Henning posted do, that you 
need it to do??

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

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

expands to:

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

   void newMsgProc(uint uID) {      //expands just like "BEGIN_MSG_MAP"
      foreach(mapping; Mappings) {  //loops 2x
        if(mapping.matches(uID))    //just like "if (uID == WM_CLOSE)"
          mapping.executeAction();  //just like "vfunc();"
      }
   }
}

Change MessageMap to expand to the function fignature you want, i.e.

template MessageMap(Mappings ...) {
   override BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM 
lParam, ref LRESULT lResult) {
      foreach(mapping; Mappings) {
        if(mapping.matches(uID))
          mapping.executeAction();
      }
   }
}

Change MessageMap to return the result of the mapping.executeAction just 
like "return lResult;" in the original C++.

template MessageMap(Mappings ...) {
   override BOOL NewMsgProc(HWND hWnd, UINT uID, WPARAM wParam, LPARAM 
lParam, ref LRESULT lResult) {
      foreach(mapping; Mappings) {
        if(mapping.matches(uID)) {
          lResult = mapping.executeAction();
          return cast(BOOL)lResult;
        }
      }
   }
}

I think it's ok to use mixins as you're desribing, but why do you need 
to do it that way?

Regan


More information about the Digitalmars-d-learn mailing list