www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Static cast

reply Jason House <jason.james.house gmail.com> writes:
What is the proper way to implement static cast?

cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My
problem is that cast(T)cast(void*) does not honor vtable locations and
ClassInfo is only available at runtime.
Jun 25 2008
next sibling parent reply BCS <ao pathlink.com> writes:
Reply to Jason,

 What is the proper way to implement static cast?
 
 cast(T) to shift with an object/ interface hierarchy is dynamic and
 slow. My problem is that cast(T)cast(void*) does not honor vtable
 locations and ClassInfo is only available at runtime.
 

up cast or down cast? IIRC upcast /is/ non-dynamic and skipping the test on downcast is just a bad idea. Or am I not reading you correctly?
Jun 25 2008
parent reply Jason House <jason.james.house gmail.com> writes:
&BCS Wrote:

 Reply to Jason,
 
 What is the proper way to implement static cast?
 
 cast(T) to shift with an object/ interface hierarchy is dynamic and
 slow. My problem is that cast(T)cast(void*) does not honor vtable
 locations and ClassInfo is only available at runtime.
 


 up cast or down cast?

Both. Upcasting to an interface shows up in my profiler.
 IIRC upcast /is/ non-dynamic and skipping the test on downcast is just a 
 bad idea.

I've implemented algorithms to operate on interfaces. Operations called by the algorithm require casting. All the data operated on by an algorithm is the same type. In this case, it's safe and will speed up execution by at least 15%
Jun 25 2008
parent BCS <ao pathlink.com> writes:
Reply to Jason,

 &BCS Wrote:
 
 Reply to Jason,
 
 What is the proper way to implement static cast?
 
 cast(T) to shift with an object/ interface hierarchy is dynamic and
 slow. My problem is that cast(T)cast(void*) does not honor vtable
 locations and ClassInfo is only available at runtime.
 


 up cast or down cast?
 

 IIRC upcast /is/ non-dynamic and skipping the test on downcast is
 just a bad idea.
 

called by the algorithm require casting. All the data operated on by an algorithm is the same type. In this case, it's safe and will speed up execution by at least 15%

I'm not sure there /is/ a way to do non dynamic casts to/from interfaces. For one thing an interface reference does not reference the same address as the object. interface I {} class C : I {} C c = new C; assert(cast(void*) c != (cast(void*)cast(I) c); // not actually tested If you have a slew of casts from/to the _exact_same_types_ to do, you might cast the first one, find the pointer difference, and then just add that for the rest of the casts. But if any of your assumptions are off, your toast.
Jun 25 2008
prev sibling next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jason House" <jason.james.house gmail.com> wrote in message 
news:g3uedt$1vn0$1 digitalmars.com...
 What is the proper way to implement static cast?

 cast(T) to shift with an object/ interface hierarchy is dynamic and slow. 
 My problem is that cast(T)cast(void*) does not honor vtable locations and 
 ClassInfo is only available at runtime.

Doesn't honor vtable locations? Are you talking about trying to cast an interface to a class reference, or an interface to a derived interface? Since that's the only scenario I can think of where the resulting reference would have invalid vtbl locations. If you're casting from a base *class* to a derived *class*, there is no way AFAIK that it could end up with invalid vtbl entries.
Jun 25 2008
parent Jason House <jason.james.house gmail.com> writes:
Jarrett Billingsley Wrote:

 "Jason House" <jason.james.house gmail.com> wrote in message 
 news:g3uedt$1vn0$1 digitalmars.com...
 What is the proper way to implement static cast?

 cast(T) to shift with an object/ interface hierarchy is dynamic and slow. 
 My problem is that cast(T)cast(void*) does not honor vtable locations and 
 ClassInfo is only available at runtime.

Doesn't honor vtable locations? Are you talking about trying to cast an interface to a class reference, or an interface to a derived interface? Since that's the only scenario I can think of where the resulting reference would have invalid vtbl locations. If you're casting from a base *class* to a derived *class*, there is no way AFAIK that it could end up with invalid vtbl entries.

Yes, interfaces too. You're right that casts to/from an interface is the only problematic scenario.
Jun 25 2008
prev sibling parent reply Frank Benoit <keinfarbton googlemail.com> writes:
Jason House schrieb:
 What is the proper way to implement static cast?
 
 cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My
problem is that cast(T)cast(void*) does not honor vtable locations and
ClassInfo is only available at runtime.

I you need this only for special classes and not as a general thing... you can probably give those classes/interfaces a asType Method. interface I1 { T asT(); } interface I2 : I1 { } class A : I2 { T asT(){ return null; } } class B : A{} class T : A{ T asT(){ return this; } } Now the cast is only a virtual method call and no dynamic cast is needed and you get null for class which are not T.
Jun 25 2008
parent Jason House <jason.james.house gmail.com> writes:
Frank Benoit Wrote:

 Jason House schrieb:
 What is the proper way to implement static cast?
 
 cast(T) to shift with an object/ interface hierarchy is dynamic and slow. My
problem is that cast(T)cast(void*) does not honor vtable locations and
ClassInfo is only available at runtime.

I you need this only for special classes and not as a general thing... you can probably give those classes/interfaces a asType Method. interface I1 { T asT(); } interface I2 : I1 { } class A : I2 { T asT(){ return null; } } class B : A{} class T : A{ T asT(){ return this; } } Now the cast is only a virtual method call and no dynamic cast is needed and you get null for class which are not T.

I'd template the algorithms by the data type before doing that type of a solution. Templates are the fastest option anyway. It should also allow function inlining or removal of virtual function calls which may be a significant speed boost.
Jun 25 2008