www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get actual classinfo through an interface?

reply BC <NOTmi_emayl_adrez hotmail.com.remove.not> writes:
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
next sibling parent Christopher Wright <dhasenan gmail.com> writes:
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
prev sibling next sibling parent reply Robert Fraser <fraserofthenight gmail.com> writes:
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
parent reply Christopher Wright <dhasenan gmail.com> writes:
Robert Fraser wrote:
 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);
Error: foo is used as a type.
Nov 21 2007
next sibling parent reply Ary Borenszweig <ary esperanto.org.ar> writes:
Christopher Wright wrote:
 Robert Fraser wrote:
 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);
Error: foo is used as a type.
Probably typeid(typeof(foo))
Nov 22 2007
parent Christopher Wright <dhasenan gmail.com> writes:
Ary Borenszweig wrote:
 Christopher Wright wrote:
 Robert Fraser wrote:
 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);
Error: foo is used as a type.
Probably typeid(typeof(foo))
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.
Nov 22 2007
prev sibling parent Robert Fraser <fraserofthenight gmail.com> writes:
Christopher Wright wrote:
 Robert Fraser wrote:
 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);
Error: foo is used as a type.
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.
Nov 22 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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