September 21, 2007 Re: C++ Macro to D mixin template, delegates Please help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | Regan Heath schrieb: > 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?? > Probabely, because I am not genius... :-( I 'd better read it twice! Just one question : It seems that you use template overloading, or do I miss something. In other words : Can I use template overloading like function overloading ? Many thanks for your efforts, Regan and Henning ! Bjoern > 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 Probabely, because I am not genius... :-( I 'd better read it twice! Just one question : It seems that you use template overloading, or do I miss something. In other words is template overloading legal ? Many thanks for your efforts, Regan and Henning ! Bjoern |
September 21, 2007 Re: C++ Macro to D mixin template, delegates Please help | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | BLS wrote: > Regan Heath schrieb: >> 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?? >> > > Probabely, because I am not genius... :-( I 'd better read it twice! Join the club ;) > Just one question : It seems that you use template overloading, or do I miss something. The mixin mixes in a function "newMsgProc" which should overload a base class function of the same signature (whether mixed in also or typed). I think this works, but I am not 100% certain. The docs do say: "Templates cannot be used to add non-static members or functions to classes" on the templates page, but I'm not sure if that applies to "template mixins" too, or what. > In other words : Can I use template overloading like > function overloading ? I think so, however you can't mix template function and function overloading, i.e. class Foo { void foo(T)(T arg) { } void foo(float arg) { } } But Walter plans to make this work I believe. Regan |
September 21, 2007 Re: C++ Macro to D mixin template, delegates Please help | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | Thanks for all the compliments :) > Just one question : It seems that you use template overloading, or do I miss something. In other words : Can I use template overloading like function overloading ? Somehow you can do stuff like this for example: template Foo(int n, T) { // some stuff } template Foo(T) { // something else } You can even define templates differently for different types, but thats called "specialisation" not overloading. But I dont use such a thing here. What I do is defining a few templated structs and the structs (not instances of them, the actual type!) are given to MessageMap which calls the static functions. So technically, OnClose!(foo) and OnRange!(1, 3, bar) are *types* that contain aliases (think of compile time references) to the methods you want to call. The "mixin MessageMap!(...)" simply adds a new method to your class. Note that because it is run over types, the "foreach(mapping; Mappings) { .. }" is done at compile time, ie its body is repeated for every type (I called them Mapping's here), so all runtime overhead you have are the ifs that evaluate to false. If you have further questions, dont hesitate to ask :) Henning -- 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 |
September 21, 2007 Re: C++ Macro to D mixin template, delegates Please help | ||||
---|---|---|---|---|
| ||||
Posted in reply to Henning Hasemann | Henning Hasemann schrieb: > Thanks for all the compliments :) > > >> Just one question : It seems that you use template overloading, or do >> I miss something. In other words : Can I use template overloading >> like function overloading ? > > Somehow you can do stuff like this for example: > > template Foo(int n, T) { > // some stuff > } > > template Foo(T) { > // something else > } > > You can even define templates differently for different types, but > thats called "specialisation" not overloading. > > But I dont use such a thing here. What I do is defining a few templated > structs and the structs (not instances of them, the actual type!) are > given to MessageMap which calls the static functions. > > So technically, OnClose!(foo) and OnRange!(1, 3, bar) are *types* that > contain aliases (think of compile time references) to the methods you > want to call. > > The "mixin MessageMap!(...)" simply adds a new method to your class. > Note that because it is run over types, the "foreach(mapping; Mappings) > { .. }" is done at compile time, ie its body is repeated for every type > (I called them Mapping's here), so all runtime overhead you have are > the ifs that evaluate to false. > > If you have further questions, dont hesitate to ask :) I am afraid that will happen :-) > > Henning > Just to show you what I am translating : http://www.beyondata.com/pwc.html A really smart GUI. Download if you like the Pretty WinApi Class, it is a small one. (I need as GUI toolkit for my IDE project) However, thanks to your and Regan's help I am ready to give it a go. Many thanks ! Bjoern |
September 21, 2007 Re: C++ Macro to D mixin template, delegates Please help | ||||
---|---|---|---|---|
| ||||
Posted in reply to BLS | > Just to show you what I am translating : > http://www.beyondata.com/pwc.html > A really smart GUI. > Download if you like the Pretty WinApi Class, it is a small one. > (I need as GUI toolkit for my IDE project) Thanks but as a linux user and for-linux-and-windows coder I dont have a real use for a win-only gui lib... I'd be more happy about a good gtk wrapper with active development. Henning -- 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 |
Copyright © 1999-2021 by the D Language Foundation