www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Determine the "type" of a delegate

reply teo <teo.ubuntu yahoo.com> writes:
Having a delegate d, I can use d.ptr to get a void* pointer to the 
environment used to construct the delegate. How can I determine from that 
pointer whether that is a class object?
Mar 26 2011
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Sat, 26 Mar 2011 06:46:22 -0400, teo <teo.ubuntu yahoo.com> wrote:

 Having a delegate d, I can use d.ptr to get a void* pointer to the
 environment used to construct the delegate. How can I determine from that
 pointer whether that is a class object?

AFAIK, you can't. -Steve
Mar 28 2011
parent reply teo <teo.ubuntu yahoo.com> writes:
On Mon, 28 Mar 2011 08:48:18 -0400, Steven Schveighoffer wrote:

 On Sat, 26 Mar 2011 06:46:22 -0400, teo <teo.ubuntu yahoo.com> wrote:
 
 Having a delegate d, I can use d.ptr to get a void* pointer to the
 environment used to construct the delegate. How can I determine from
 that pointer whether that is a class object?

AFAIK, you can't. -Steve

I tried to find a solution, but since I don't exactly know the anatomy of the class object it is not an easy task. Is there any info on how the class object is laid out in memory? Basically I casted the pointer to an Object and then dumped the first 64 bytes looking for hints. Here is the code: import std.stdio; struct S { void t() { writeln("S"); } } class C { void t() { writeln("C"); } } void main() { auto c = new C(); S s; auto t1 = &c.t; auto t2 = &s.t; writeln("the class:"); dump(t1.ptr); writeln("the struct:"); dump(t2.ptr); } void dump(void* p) { dump(cast(Object)p); } void dump(Object o) { auto size = size_t.sizeof * 8; writeln("object:"); auto b1 = (cast(ubyte*)(cast(size_t*)o))[0 .. size]; dump(b1); writeln("classinfo:"); auto b2 = (cast(ubyte*)(cast(size_t*)o.classinfo))[0 .. size]; dump(b2); } void dump(ubyte[] buffer) { auto l = 16; auto n = buffer.length / l + (buffer.length % l > 0 ? 1 : 0); for (int i = 0; i < n; i++) { auto s = i * l; auto e = s + l; writefln(" %02x", buffer[s .. (e > $) ? $ : e]); } } BTW: I am getting strange results from writefln - the format is not obeyed the class: object: [f0, e4, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [75, 74, 66, 2d, 33, 32, 6c, 65, 0, 0, 0, 0, 0, 0, 0, 8] [a0, 1b, 4a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [75, 74, 66, 2d, 31, 36, 6c, 65, 0, 0, 0, 0, 0, 0, 0, 8] classinfo: [b0, f9, 49, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [10, 0, 0, 0, 0, 0, 0, 0, d0, e4, 49, 0, 0, 0, 0, 0] [06, 00, 00, 00, 00, 00, 00, 00, e0, e4, 49, 00, 00, 00, 00, 00] [07, 00, 00, 00, 00, 00, 00, 00, f0, e4, 49, 00, 00, 00, 00, 00] the struct: object: [00, 21, 0e, 7b, 57, 7f, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00] [80, be, fc, 7a, 57, 7f, 0, 0, 78, c1, 45, 0, 0, 0, 0, 0] [40, 72, a6, 10, ff, 7f, 0, 0, 2c, c1, 45, 0, 0, 0, 0, 0] [a0, 72, a6, 10, ff, 7f, 0, 0, 45, 3b, 46, 0, 0, 0, 0, 0] classinfo: Segmentation fault
Mar 28 2011
parent Peter Alexander <peter.alexander.au gmail.com> writes:
On 28/03/11 8:47 PM, teo wrote:
 I tried to find a solution, but since I don't exactly know the anatomy of
 the class object it is not an easy task. Is there any info on how the
 class object is laid out in memory? Basically I casted the pointer to an
 Object and then dumped the first 64 bytes looking for hints.

The D ABI is described here: http://digitalmars.com/d/2.0/abi.html It describes the class layout.
Mar 28 2011