Thursday, August 30, 2007

I finally have the redesigned functionality for reflection. It's not done certainly, but the following will compile and work correctly. I'll be committing this to the SVN repository soon.

Here's how it works:

/////////////////////////////////////
// In your code that reflects some class out:

class MyClass {
public:
  int myFunction();
}
// For terseness
using namespace boost::reflections;
generic_reflector * exportMyClass() {
  reflector * myClassReflector =
  new reflector;
  // reflect an arg-less constructor
  myClassReflector->reflect_constructor();
  // reflect the member function
  // with some info about it
  // note that like boost::extensions,
  // the info parameter is templated
  // with a string as the default
  myClassReflector->reflect_method(&MyClass::myFunction, "myFunction");
  return myClassReflector;
}


///////////////////////////////
// Now in the code that will use the reflection
// (This code could be in a DLL, or the top code could be in a DLL etc)
using namespace boost::reflections;
  int main(int argc, char * argv[]) {
  // A reflection must be created with a pointer
  // to a generic_reflector
  // The reflection will destroy the generic_reflector
  reflection myReflection(exportMyClass());
  // Create an instance with a default constructor
  instance inst = myReflection.get_constructor().call();
  // Find the function with an int return value and
  // info of "myFunction" and call it.
  function func =
       myReflection.get_method("myFunction");
  // Call func on inst
  func(inst);
}



////////

No comments: