Call method if declared only
Виталий Фадеев
vital.fadeev at gmail.com
Fri Feb 28 08:08:59 UTC 2020
On Friday, 28 February 2020 at 06:12:37 UTC, Виталий Фадеев wrote:
> Searching solution for idea !
>
> Goal is to get System message, dispatch/route to method !
> If method implemented only !
>
> I dream on in future write clean code of a derived widgets like
> this :
>
> class Base
> {
> // dispatch
> void On( message ... )
> {
> // call On<message>()
> // example: call OnKeyUp() - if method OnKeyUp() is
> exists only
> }
> }
>
> class Derived : Base
> {
> // method implementation
> void OnKeyUp( ... )
> {
> //
> }
> }
>
>
>
> I tryed code like this:
>
> import core.sys.windows.windows;
> import std.stdio;
>
>
> class Base
> {
> LRESULT On( UINT message, WPARAM wParam, LPARAM lParam )
> {
> switch ( message )
> {
> case WM_KEYDOWN:
> OnWM_KEYDOWN( wParam, lParam );
> // <-- but it required declared OnWM_KEYDOWN(...) in base
> class Base. How will be without declaring in Base ?
> break;
>
> default:
> }
> }
> }
>
>
> class Button : Base
> {
> LRESULT OnWM_KEYDOWN( WPARAM wParam, LPARAM lParam )
> {
> writeln( "WM_KEYDOWN" );
> }
> }
>
>
> May be other than derived ? May be templating ?
> How to implement ?
Now I go in ths way:
import std.stdio;
import std.traits;
class Base
{
void On( alias THIS )( int message )
{
static if (
__traits( hasMember, THIS, "OnKey" ) &&
isCallable!( __traits( getMember, THIS,
"OnKey" ) )
)
{
__traits( getMember, THIS, "OnKey" )();
}
}
void run( alias THIS )()
{
On!THIS( 1 );
}
}
class A : Base
{
void OnKey()
{
writeln( "A.OnKey()" );
}
}
void main()
{
auto a = new A();
a.run!a();
}
Searching for beauty readable code...
More information about the Digitalmars-d-learn
mailing list