www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Extract typeinfo

reply "Namespace" <rswhite4 googlemail.com> writes:
Is there a way to extract the correct typeinfo of an array if I 
only have the pointer?

Code:
----
import std.stdio;

struct Foo {
	int i;
}

void foo(void* p) {
     // ???
}

void main() {
     Foo[] fs;
     fs ~= Foo();

     foo(fs.ptr);
}
----

Now that we have only the untyped void*: Is there a way how I can 
extract the right typeinfo? Something to rebuild the correct type 
out of the adress.
Mar 05 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
On Wednesday, 5 March 2014 at 19:15:12 UTC, Namespace wrote:
 Is there a way to extract the correct typeinfo of an array if I 
 only have the pointer?
Nope, typeinfos are only stored in runtime on classes. All other types get it only through the static info. Consider the following: char[10] a; char[] b = a[3..5];// from the middle of it void* c = b.ptr; Even if the info was stored with a, the slice would have no idea where to find it, so you couldn't get it from the pointer either. The way druntime does it with void pointers is to pass the typeinfo too: void useArray(void[] a, TypeInfo a_type);
Mar 05 2014
parent "Namespace" <rswhite4 googlemail.com> writes:
On Wednesday, 5 March 2014 at 19:21:39 UTC, Adam D. Ruppe wrote:
 On Wednesday, 5 March 2014 at 19:15:12 UTC, Namespace wrote:
 Is there a way to extract the correct typeinfo of an array if 
 I only have the pointer?
Nope, typeinfos are only stored in runtime on classes. All other types get it only through the static info. Consider the following: char[10] a; char[] b = a[3..5];// from the middle of it void* c = b.ptr; Even if the info was stored with a, the slice would have no idea where to find it, so you couldn't get it from the pointer either. The way druntime does it with void pointers is to pass the typeinfo too: void useArray(void[] a, TypeInfo a_type);
Ah thank you. :)
Mar 05 2014