Version 5 (modified by martin, 7 years ago)

--

Drew Whitehouse

OSG newbie, interested in a Tasks/ReflectionFramework.

Reflection Notes

Greenspun's Tenth Rule of Programming: "Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified bug-ridden slow implementation of half of Common Lisp."

Robert, here are some thoughts about what sort of reflection would be useful for OSG. I have used two different scenegraph toolkits that have reflective aspects, Cosmo3d and Intrinsics Alchemy. Alchemy is a really amazing piece of software engineering, but unfortunately closed source and now pretty much locked up since Vicarious Visions scooped up Intrinsic when they hit the wall.

The general characteristic of both of the toolkits is that they have the notion of containers that contain fields (Cosmo3d terminogy). osg::Object would typically be the root of a container hierachy. The fields are typically data types like float, Vec3f etc as well as fields that are references to other containers and possibly references to an array of a particular type of field.

Here are some of the essential reflective properties that I used to make a python interface to the above mentioned toolkits, in Cosmo3d pseodo code ..

  • containers can tell you their type ...
        csContainer *c = static_cast<csContainer*>(new csTransform);
        csType *t = c->getType();
    

or

    csType *tt = csTransform::getClassType();
  • enumerate the types known by the system
    for (int i = 0 ; i < csType::getNumTypes() ; i++) 
    {
      cout << csType::getType(i)->getName());
    }
  • given the type describing the container, create a new instance of the container (Alchemy)
        csType *t = csTransform::getClassType();
        csTransform *xf = t->createInstance();
    
  • compare the types for equality, inheritance relationships etc
        if (t->isDerivedFrom(csContainer::getClassType())) {   ... } etc
    
  • tell you what fields they contain
        for  (int j = 0; j < t->getFieldInfoCount() ; j++)
        {
          cout << t->getFieldInfo(j)->getName() << endl;
        }
    
  • Allow you to set/get the values of fields
        csTransform *c = new csTransform;
        csField *f = c->getField("scale");  
        csType  *ft = f->fieldType();
        assert(ft==csSFVec3f::getClassType());
        // getting
        csVec3f v;
        ((csSFVec3f*)f)->get(v);
    
        // setting
        ((csSFVec3f*)f)->set(csVec3f(1,1,1));
    
  • One type of field is for enumerations, so you can extract a mapping of name<->int without having to enumerate them in a wrapping code.
  • something that reflected on the methods of objects would be really nice, neither of the toolkits provided that.