digitalmars.D.learn - get actual classinfo through an interface?
- BC (4/4) Nov 21 2007 new question. since IInterface.classinfo returns IInterface, how to get
- Christopher Wright (23/26) Nov 21 2007 Do you have a variable that's passed as IInterface?
- Robert Fraser (7/10) Nov 21 2007 typeid.
- Christopher Wright (2/14) Nov 21 2007 Error: foo is used as a type.
- Ary Borenszweig (3/18) Nov 22 2007 Probably
- Christopher Wright (9/29) Nov 22 2007 Yes, but then you have:
- Robert Fraser (4/19) Nov 22 2007 Oops, shouldn't shoot my mouth off unless I actually know what I'm
- Jarrett Billingsley (5/7) Nov 21 2007 Something to keep in mind is that an interface doesn't necessarily point...
new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class? -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Nov 21 2007
BC wrote:new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?Do you have a variable that's passed as IInterface? --- interface IFoo {} class Foo : IFoo {} void whatever (IFoo foo) { assert (foo.classinfo is Foo.classinfo); } --- That should, I think, work. It fails. I believe that .classinfo is a static field, which is why it fails. It needs to be accessed in a virtual way. Since typeof() is compile-time, it doesn't help. You can do the ultra-cruddy way, if you're just looking for equality with some known type, which is naturally completely untested: bool isType!(T)(Object o) { auto vtbl = (*(cast(void*[]*)&o))[0]; return vtbl == T.classinfo.vtbl.ptr; } If you have a classinfo dictionary, that doesn't help, unless you start storing by vtbl ptr instead. Of course, Walter's free to change the layout of objects any time he wishes, so this is quite version-specific.
Nov 21 2007
BC wrote:new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?typeid. interface Foo { } class Bar : Foo { } // ... Foo foo = new Bar(); TypeInfo ti = typeid(foo);
Nov 21 2007
Robert Fraser wrote:BC wrote:Error: foo is used as a type.new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?typeid. interface Foo { } class Bar : Foo { } // ... Foo foo = new Bar(); TypeInfo ti = typeid(foo);
Nov 21 2007
Christopher Wright wrote:Robert Fraser wrote:Probably typeid(typeof(foo))BC wrote:Error: foo is used as a type.new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?typeid. interface Foo { } class Bar : Foo { } // ... Foo foo = new Bar(); TypeInfo ti = typeid(foo);
Nov 22 2007
Ary Borenszweig wrote:Christopher Wright wrote:Yes, but then you have: --- void whatever (IFoo foo) { writefln(typeof(foo).stringof); } whatever(new Foo()); // prints IFoo --- typeof is strictly compile-time. BC's looking for a runtime check.Robert Fraser wrote:Probably typeid(typeof(foo))BC wrote:Error: foo is used as a type.new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?typeid. interface Foo { } class Bar : Foo { } // ... Foo foo = new Bar(); TypeInfo ti = typeid(foo);
Nov 22 2007
Christopher Wright wrote:Robert Fraser wrote:Oops, shouldn't shoot my mouth off unless I actually know what I'm talking about. Attempt 2: A series of casts and .classinfo should work.BC wrote:Error: foo is used as a type.new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?typeid. interface Foo { } class Bar : Foo { } // ... Foo foo = new Bar(); TypeInfo ti = typeid(foo);
Nov 22 2007
"BC" <NOTmi_emayl_adrez hotmail.com.remove.not> wrote in message news:op.t15gsgmttn71h3 hal9000xp...new question. since IInterface.classinfo returns IInterface, how to get the actual value of the concrete class?Something to keep in mind is that an interface doesn't necessarily point to a D class, which is probably why this information isn't available in the general case.
Nov 21 2007