www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - OffsetTypeInfo

reply Ender KaShaea <astrothayne gmail.com> writes:
How do you use the OffsetTypeInfo class, or the offti function of the classinfo
or typeinfo classes? I was expecting them to give the type and offfset of each
member of a class or struct, though I can't figure out how to get that
information.
Aug 17 2007
next sibling parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Ender KaShaea wrote:
 How do you use the OffsetTypeInfo class, or the offti function of the
classinfo or typeinfo classes? I was expecting them to give the type and
offfset of each member of a class or struct, though I can't figure out how to
get that information.
I'm assuming you've already tried, and it didn't work. You may want to have a look at bug 1348. http://d.puremagic.com/issues/show_bug.cgi?id=1348 Otherwise, if you just don't know how to use it at all, well, I'm sorry but I don't know for sure either. They don't work, so I have no way of testing! Anyhow, I was trying to use them to iterate through members of an arbitrary class and, based on the type of a member, do something with it. Here's some untested code: // This code is placed inside the said "arbitrary class" OffsetTypeInfo[] otis = typeid(typeof(this)).offTi; foreach( oti; otis ) { char[] typeName = oti.toString(); if ( typeName !is null ) { // Just a tricky/hackey way of turning a typeinfo into a classinfo. ClassInfo ci = ClassInfo.find( oti.toString() ); // Suppose I want to know if this member class is a direct or indirect descendant of some class type known as "Foo" // Here's how it's done. ClassInfo temp = ci; bool descendsFoo = false; while ( temp !is Object.classinfo && temp !is Foo.classinfo ) temp = temp.base; if ( temp is Foo.classinfo ) descendsFoo = true; } }
Aug 18 2007
prev sibling next sibling parent Carlos Santander <csantander619 gmail.com> writes:
Ender KaShaea escribió:
 How do you use the OffsetTypeInfo class, or the offti function of the
 classinfo or typeinfo classes? I was expecting them to give the type and
 offfset of each member of a class or struct, though I can't figure out how to
 get that information.
Maybe I'm misunderstanding you, but have you checked if .offsetof does what you want to do? -- Carlos Santander Bernal
Aug 18 2007
prev sibling next sibling parent reply Ender KaShae <astrothayne gmail.com> writes:
Carlos Santander Wrote:

 Ender KaShaea escribió:
 How do you use the OffsetTypeInfo class, or the offti function of the
 classinfo or typeinfo classes? I was expecting them to give the type and
 offfset of each member of a class or struct, though I can't figure out how to
 get that information.
Maybe I'm misunderstanding you, but have you checked if .offsetof does what you want to do? -- Carlos Santander Bernal
what is .offsetof I have not heard of it
Aug 18 2007
parent Carlos Santander <csantander619 gmail.com> writes:
Ender KaShae escribió:
 Carlos Santander Wrote:
 
 Ender KaShaea escribió:
 How do you use the OffsetTypeInfo class, or the offti function of the
 classinfo or typeinfo classes? I was expecting them to give the type and
 offfset of each member of a class or struct, though I can't figure out how to
 get that information.
Maybe I'm misunderstanding you, but have you checked if .offsetof does what you want to do? -- Carlos Santander Bernal
what is .offsetof I have not heard of it
Look for "Field Properties" in http://www.digitalmars.com/d/class.html -- Carlos Santander Bernal
Aug 18 2007
prev sibling next sibling parent Ender KaShae <astrothayne gmail.com> writes:
ChOtherwise, if you just don't know how to use it at all, well, I'm sorry 
 but I don't know for sad J Wrote:
 I'm assuming you've already tried, and it didn't work.
 You may want to have a look at bug 1348. 
 http://d.puremagic.com/issues/show_bug.cgi?id=1348
 
this bug descibes the behavior perfectly
Aug 18 2007
prev sibling parent reply Ender KaShae <astrothayne gmail.com> writes:
Chad J Wrote:

 Ender KaShaea wrote:
 How do you use the OffsetTypeInfo class, or the offti function of the
classinfo or typeinfo classes? I was expecting them to give the type and
offfset of each member of a class or struct, though I can't figure out how to
get that information.
I'm assuming you've already tried, and it didn't work. You may want to have a look at bug 1348. http://d.puremagic.com/issues/show_bug.cgi?id=1348
this is rather depressing as it had some rather neat reflection capabilities if it worked the way I thought it would, ex: a deep copy: Type copy(Type: Object)(Type t){ OffsetTypeInfo[] ot = t.classinfo.offTi; void* ptr = cast(void*)t; Type result = new result;// don't know how else to create instance void* v = cast(void*) result; foreach(o; ot){ if(!is(typeof(o.ti): TypeInfo_Class)){ v[o.offset] = ptr[o.offset]; break; } v[o.offset] = copy(cast(Object)ptr); } return result; } I'm not sure if that would work(especially indexing a void pointer, i'm not sure if that's allowed or how it counts) and could have added functionality (like support for structs and arrays) but it shows how useful it could be (it would be even more useful if there was a way to dynamically cast using a typeinfo (maybe by using templates and alias's?)
Aug 19 2007
parent Chad J <gamerChad _spamIsBad_gmail.com> writes:
Ender KaShae wrote:
 Chad J Wrote:
 
 Ender KaShaea wrote:
 How do you use the OffsetTypeInfo class, or the offti function of the
classinfo or typeinfo classes? I was expecting them to give the type and
offfset of each member of a class or struct, though I can't figure out how to
get that information.
I'm assuming you've already tried, and it didn't work. You may want to have a look at bug 1348. http://d.puremagic.com/issues/show_bug.cgi?id=1348
this is rather depressing as it had some rather neat reflection capabilities if it worked the way I thought it would, ex: a deep copy: Type copy(Type: Object)(Type t){ OffsetTypeInfo[] ot = t.classinfo.offTi; void* ptr = cast(void*)t; Type result = new result;// don't know how else to create instance void* v = cast(void*) result; foreach(o; ot){ if(!is(typeof(o.ti): TypeInfo_Class)){ v[o.offset] = ptr[o.offset]; break; } v[o.offset] = copy(cast(Object)ptr); } return result; } I'm not sure if that would work(especially indexing a void pointer, i'm not sure if that's allowed or how it counts) and could have added functionality (like support for structs and arrays) but it shows how useful it could be (it would be even more useful if there was a way to dynamically cast using a typeinfo (maybe by using templates and alias's?)
I agree this stuff would be quite useful if it worked.
Aug 20 2007