Static cast template

Daniel Murphy yebblies at nospamgmail.com
Mon Feb 20 08:21:34 PST 2012


Does message _have_ to be an interface?  With an abstract class the offset 
will always be zero, so

T my_cast(T : Message)(Message m)
{
    debug
        return cast(T)m;
    else
        return cast(T)cast(void*)m;
}

"Rene Zwanenburg" <renezwanenburg at gmail.com> wrote in message 
news:vmsydblfjawogbxsguab at forum.dlang.org...
>A part of the project I'm currently working on relies heavily on messages 
>being sent between objects. There's some sort of container object which 
>receives the messages, and notifies any listeners registered to it. We 
>can't send the messages to the final recipients directly because the sender 
>doesn't know about the receivers existence and vice versa.
>
> There are many types of messages, but they all implement a Message 
> interface. The messageListeners cast the Message to some ConcreteMessage, 
> but the casting is starting to take a significant percentage of execution 
> time. Due to the way MessageListeners are registered, we can be sure a 
> MessageListener only receives the type of messages it's interested in, so 
> the cast will never fail.
>
> I'm looking for a static_cast construct, which will be checked during 
> debug, but in release mode it should minimize overhead by simply adding 
> the interface offset to the pointer. I've tried to make use off 
> Type.classinfo's interfaces member, but for some reason a Type's classinfo 
> isn't available at compile time.
>
> So my question comes down to: does anyone know of a way to get static_cast 
> behavior for upcasts? 




More information about the Digitalmars-d mailing list