[GCC-XML] C++ Reflection Using GCCXML?

Bryan Ischo bji-gccxml at ischo.com
Thu Sep 1 02:39:56 EDT 2005

Hi all.  I've been working on a project for some time now which uses a
form of reflection for C++, which I have implemented using GCCXML.  My
goal was rather more narrow than a full reflection system for C++ - I just
wanted to implement automatic marshalling of C++ objects into a portable
binary format - but in implementing my system I came to realize the full
potential of GCCXML in such an application.

So I thought I'd throw the idea out here and see what others think.

The concept is pretty simple: GCCXML can be used to generate, via a tool
which reads in the GCCXML output and constructs a representation of the
class hierarchy and class structures thus parsed by GCCXML, a set of
"tables" describing all of the data relevent to reflection for the input
C++ classes.  These tables can be used at runtime by a library of methods
for looking up information describing any C++ class.

The kinds of information that such a reflection-generating tool could
output into static tables include, for any given class, the set of parent
classes it inherits from (and from these the set of grandparent classes
that it inherits from, etc), the set of methods that the class implements,
including information about the return type and argument types of these
methods, and information about the fields of the class in addition to
low-level info about the layout in memory of the class.  Class factory
methods could be generated as well and allow the construction of "default"
instances of any C++ class, as well as destructor methods.

The result (I've already done some of this for my purposes but hide it all
behind an object marshalling framework, not a general C++ reflection
framework) would be the ability during run-time of querying for the
properties of any given class, so that all of the functionality made
possible by a true reflection framework could be realized.  RTTI could be
used to identify classes and match them up to the generated reflection
tables.  This would in essence be a way of augmenting the existing C++
RTTI with a much, much richer set of information.

I've not really used reflection APIs before, except some early Java stuff
(implements etc) which were mostly useful due to architectural features of
the Java language.  So I'm wondering if a C++ API that would let you query
an object for its members, set them by name, call methods of objects whose
actual types you don't know, by name, etc, would be useful to anyone.  I
actually can't think of too many ways that I would use such an API in the
kids of code that I write.  Like I said I find object serialization to be
a worthwhile goal so I have implemented enough of the above system to, in
my code, generate tables which allow the automated marshalling to and
unmarshalling from a binary serialization format for C++ classes.

Just curious what others think of this idea.  Was it obvious already?  I
haven't heard of anyone doing anything similar before, and I'm not sure if
that's because no one's thought of it before, or because no one's thought
that it would be useful enough to do.

All the best,

Bryan Ischo             bryan at ischo.com                          N, R, 6
New York, NY, USA       http://www.ischo.com            RedHat Linux 7.3

More information about the gccxml mailing list