compile time class introspection?

Max Samukha samukha at voliacable.com.removethis
Thu Jun 14 06:59:41 PDT 2007


On Thu, 14 Jun 2007 09:27:50 -0400, serg <why.you.need at my.email>
wrote:

>What really want is, to have a template mixin method that depending on class it inserted into, could call internally a member method if it available.
>
>Something like this:
>
>template dispatchT(T)
>{
>    void dispatch(int msg)
>    {
>        switch (msg)
>        {
>        case WM_CLOSE:
>            static if (OnClose in T) // <-- T has desired method
>                return OnClose();
>            static else
>                break;
>        case WM_DESTROY:
>            static if (T.OnDestroy) // <-- same, but different syntax
>                return OnDestroy();
>            static else
>                break;
>        default:
>        }
>        return DefWndProc();
>    }
>}
>
>class Window
>{
>    void OnClose(){}
>    void OnDestroy(){}
>    mixin dispatchT!(Window);
>}
>
>
>-- serg.

You might do it like this:

template dispatchT(T)
{
    void dispatch(int msg)
    {
        switch (msg)
        {
            static if (is(typeof(T.OnClose)))
            {
                case WM_CLOSE:
                    return T.OnClose();                    
            }
            else static if (is(typeof(T.OnDestroy)))
            {
                case WM_DESTROY:
                    return T.OnDestroy();                  
            }
        default:
	
        }
    }
}


More information about the Digitalmars-d-learn mailing list