C++ Macro to D mixin template, delegates Please help
Regan Heath
regan at netmail.co.nz
Fri Sep 21 01:16:26 PDT 2007
Henning Hasemann wrote:
>
> 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);
> }
>
Genius! :)
See BLS I told you someone else would come up with something. Sadly my
mind doesn't operate in a very templatey fashion .. yet .. I need more
practice.
Regan
More information about the Digitalmars-d-learn
mailing list