C++ Reflection

LCG Project | LCG Applications Area

SEAL Project | Project Portal

 

$Date: 2005/01/05 08:10:34 $

Functionality

The C++ programming language does not support inherent reflective information through its standard up to now.  The SEAL C++ Refection libraries provide the functionality for building and accessing the C++ refection information. The package consists of two libraries: ReflectionBuider and Reflection. The first one provides the functionality for building the reflection in memory at run time. The second one  provides the API to be used by clients to the access and interact generically with instances of described classes:

The main classes provided by Reflection are: Namespace, Class, Method, Field, PropertyList

The command lcgdict  (based on gcc_xml) is available to generated the ReflectionBuilder code directly from C++ header files.

Using C++ Reflection

Client Code

In order to use the functionality of the Reflection library the Reflection.h header file needs to be included (see code 1)

1: #include "Reflection/Reflection.h"
Code 1: Include statement

After the successful loading of the dictionary library containing information about c++ objects one may start interacting with this information. The first step will be in most cases to retrieve the information about a certain C++ class (see Code 2, line 1). Further steps may include
1: const Class* mc = Class::forName("MyClass");
2:
3: bool isBasic = mc->isPrimitive();
4: bool isContainer = mc->isContainer();
5:
6: const std::vector<const Field*> fields = mc->fields(NOMODIFIER);
7: const std::vector<const Method*> methods =  mc->methods(NOMODIFIER);
8: const std::map<const Class*, std::pair<int, int(*)(void*) > > baseClasses = mc->superClasses();
Code 2: Basic operations


Once the fields of a given class have been retrieved one may start interacting with them

 1: const Field* mf = mc->field("myVar",NOMODIFIER); 
 2: std::cout << mf->name() << " " << mf->type->name() << std::endl;
 4:
 5: void* baseOfClass = new MyClass();
 6: int val = mf->get(baseOfClass, int());
 7: mf->set(baseOfClass, 4711);
Code 3: Interacting with fields

As one can interact with fields it is also possible to interact with the methods of a class
 1: const std::vector < const Method * >::const_iterator mIt;
 2: const std::vector < const Class * >::const_iterator cIt;
 3: for (mIt = methods.begin(); mIt != methods.end(); ++mIt) {
 4:   const std::vector < const Class * > args =  (*mIt)->argumentTypes();
 5:   std::cout << (*mIt)->returnType()->name() << `` `` << (*mIt)->name() << ``(``;
 6:   for (cIt = args.begin(); cIt != args.end(); ++cIt) {
 7:     std::cout << (*cIt)->name() << `` ``;
 8:   }
 9:   std::cout << std::endl;
10: }
11: 
12: const Method * mm = mc->method(``myMethod'');
13: int ret = mm->invoke(baseOfClass, int());
Code 4: Interacting with methods

Compiling and Linking

The compile statement shall include the include path of the SEAL installation

-I<installation_dir>/SEAL_X-X-X/include

The link statement shall include the path to the SEAL release area and the names of the two Reflection libraries

-L<installation_dir>/SEAL_X_X_X/rh73_gcc323/lib -llcg_Reflection -llcg_ReflectionBuilder


Detailed Documentation


Contact: Stefan Roiser