Reflection enables a program to dynamically examine, analyze, and even modify itself. This facilitates adaptive behavior such as rebinding calls to different functions at runtime to integrate newly discovered components, or integrating flexible interpreted languages with compiled languages such as C++ or Fortran. Currently, however, reflection is not available in languages commonly used in high performance computing. This implementation of C++ reflection uses code generation to add metadata. It is fully compliant with the standard C++ specification.
ClassType is the metaclass used to represent user defined classes. Class information can be obtained as follows using ClassType.
ClassType *ct = ClassType::getClass(string("MyClass"));
string className = ct->name();
The actual target object (i.e., an instance of MyClass) can be created as follows.
obj = ct->createInstance();
Each data member in a class is represented by a DataMember object. This object maintains information such as name and type. The member function, ref() returns a reference to the represented object, allowing the user program to manipulate it as if it had direct access to the data member. The data member can be accessed as:
DataMember dm = ct->getDataMember("f1");
dm.ref<int>(&myClassObj) = 1234;
Here f1 is of type int and classAObj is an instance of MyClass. A reference to myClassObj is required, since DataMember only maintains meta-information about a particular data member in a class and does not have access to any specific instance. For example, in the code above, dm will know that f1 is of type int, but it will not know the value of f1 in the object myClassObj.
The library also has the class, BoundDataMember, which is bound to an object. For this type, there is no need to pass myClassObj as an argument since BoundDataMember is aware of the associated object. A significant characteristic of bound data members is that they are now completely type-independent of the underlying object. They can thus be passed around to code that is completely generic.
BoundDataMember bdm = ct->getBoundDataMember(&myClassObj, "f1");
// bdm is completely generic.
bdm.ref<int>() = 1234;
Analogous to data members, member functions are represented by MemberFunction classes. In addition to the name and return type of a member function, MemberFunction provides the following interfaces.
Arguments. A list of the arguments is maintained as a vector of generic Type objects. This enables the argument types to also be fundamental types, pointers, or arrays in addition to classes.
Invoke. Several types of interfaces are provided for invoking a member function. The first type requires the arguments to be passed individually along with a pointer to the
parent object of the member function. This type has several overloaded invoke() methods, each taking a different number of arguments.
Following is an example of invoking a member function.
MemberFunction mf =
The return type of m1 is int and the member function does not take any arguments. As in the case with data members, here also it is required to pass the target object to invoke, since MemberFunction does not maintain information about the target objects.
The second and third types take the arguments as a vector and therefore needs only a single interface to be defined for each type. The difference between the two types is that one of them takes an object pointer while the other takes a generic “Pointer Holder” object. The later is required in order to make the API more generic.
The analogous class to BoundDataMember for data members is BoundMemberFunction. Such an instance can be passed to generic code that is type-independent of the underlying types, which is an important characteristic for building generic adapters.
BoundMemberFunction bmf =
The reflection library can be downloaded from here.
C++ Reflection for High Performance Problem Solving Environments. Tharaka Devadithya, Kenneth Chiu, and Wei Lu. In Proceedings of High Performance Computing Symposium (HPC 2007). Norfolk, Virginia, March 25-29, 2007. (pdf).
Tharaka Devadithya (
Kenneth Chiu (SUNY
Wei Lu (