www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - ClassInfo, derived types, and object.Interface

reply "Kapps" <opantm2+spam gmail.com> writes:
I'm trying to implement a runtime reflection module, and am 
having some issues with figuring out the function pointer to 
invoke an interface method on a class that derives from the one 
that implements the interface.

For example, given the following code, we can call an interface 
function using a delegate: http://pastie.org/8255612 - but even 
with the derived instance passed in it will invoke Bar's 
implementation as we're using Bar's object.Interface instance and 
thus vtable. I figured typeid(DerivedBar).interfaces would
return it's own Interface instance for DerivedBar, but it returns 
an empty array and instead Bar's has to be used.

The ABI page doesn't seem to mention much about inheriting from a 
class that implements an interface and overrides it's functions. 
Is there a way to get the function pointer to the overridden 
version of foo on DerivedBar using Bar's object.Interface 
instance for Foo? How does the compiler know what method to call 
in this situation?

I'm hoping to accomplish the below without storing data for 
DerivedBar:
auto metadata = createMetadata!Foo;
DerivedBar instance = ... // DerivedBar is not known at 
compile-time.
metadata.invokeMethod("bar", instance, params);
Aug 25 2013
parent reply "Kapps" <opantm2+spam gmail.com> writes:
Anyone have any suggestions for this? Essentially trying to
figure out, given an object.Interface instance and an index
within it's vtbl property, how we can get the function pointer
for that method on a class that overrides the method. I assume
this is a better place to ask than the D.learn forum as it's more
of an ABI question than a question about learning D.
Sep 01 2013
next sibling parent "Ludovit Lucenic" <llucenic gmail.com> writes:
On Monday, 2 September 2013 at 06:21:55 UTC, Kapps wrote:
 Anyone have any suggestions for this? Essentially trying to
 figure out, given an object.Interface instance and an index
 within it's vtbl property, how we can get the function pointer
 for that method on a class that overrides the method. I assume
 this is a better place to ask than the D.learn forum as it's 
 more
 of an ABI question than a question about learning D.
Hi, Maybe not fully hitting your point, as I cannot recall the correct answer anymore. But I have found using GDB command line and its memory inspection to be quite useful in hunting dereferences in the class info stuff. See especially the Class Object Layout table at http://dlang.org/abi.html . Let me know, if you get stuck. Cheers, Ludovit
Sep 02 2013
prev sibling next sibling parent Benjamin Thaut <code benjamin-thaut.de> writes:
Am 02/09/2013 08:21, schrieb Kapps:
 Anyone have any suggestions for this? Essentially trying to
 figure out, given an object.Interface instance and an index
 within it's vtbl property, how we can get the function pointer
 for that method on a class that overrides the method. I assume
 this is a better place to ask than the D.learn forum as it's more
 of an ABI question than a question about learning D.
I stronlgy recommend that you read the casting routins insde the druntime. They basically contain all the information how to get to the object pointer from a interface. This might also help: http://3d.benjamin-thaut.de/?p=25#more-25 Kind Regards Benjamin Thaut
Sep 02 2013
prev sibling parent "Kapps" <opantm2+spam gmail.com> writes:
Awesome, thanks for the help guys, everything seems to work well 
now. Essentially instead of using the vtable of the initial 
Interface instance, we can just take the offset of the interface 
from the instance pointer, which becomes a void*** that contains 
the actual vtable.

Updated initial code: http://pastie.org/8292653
Sep 02 2013