www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static cast template

reply "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
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?
Feb 20 2012
next sibling parent "Daniel Murphy" <yebblies nospamgmail.com> writes:
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 gmail.com> wrote in message 
news:vmsydblfjawogbxsguab 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? 

Feb 20 2012
prev sibling parent "Rene Zwanenburg" <renezwanenburg gmail.com> writes:
Thanks. Using an abstract base class isn't a problem at the 
moment, the message types only implement the interface.

However, in case there's a situation in the future where we would 
have to use an interface I'll leave the question open.

On Monday, 20 February 2012 at 16:21:37 UTC, Daniel Murphy wrote:
 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 gmail.com> wrote in message
 news:vmsydblfjawogbxsguab 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?


Feb 20 2012