OffsetTypeInfo

Chad J gamerChad at _spamIsBad_gmail.com
Sat Aug 18 01:55:49 PDT 2007


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;
   }
}



More information about the Digitalmars-d mailing list