Getting the typeid of an object disguised under an interface

Tomer Filiba via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 28 04:58:17 PDT 2016


In continuation of an ancient thread: 
http://forum.dlang.org/post/k9gu6d$2glr$1@digitalmars.com

I wanted to do something like what Gor wanted, ended up with this:

Object interfaceToObject(I)(I obj) if (is(I == interface)) {
     import object;
     return cast(Object)(cast(void*)obj - 
((cast(Interface*)obj.__vptr[0]).offset));
}

writeln(interfaceToObject(ex.info))
// prints core.runtime.defaultTraceHandler.DefaultTraceInfo

By the way, the reason I need it is to get the actual callstack 
from the TraceInfo, so in reality it's much worse:

struct DefaultTraceInfo {
     int      numframes;
     void*[0] callstack;
}
auto traceInfo = 
cast(DefaultTraceInfo*)(cast(void*)(interfaceToObject(ex.info)) + 
(void*).sizeof * 2);

It would be really nice if TraceInfo exposed the callstack to the 
user. It only provides an opApply that converts to it string... 
adding a simple property such as

@property void*[] frames();

would be awesome.


More information about the Digitalmars-d mailing list