Using GCCXML to automate C++ wrappers creation

GCCXML uses GCC as a front-end to parse C or C++ files. It then generates XML files for the interface, that is, it generates tags for the types and prototypes it parses. Then, pygccxml is a wrapper over it which parses the XML file to generate a Python object with every information one may need.

So I will quicly show here how it is possible to generate serialization/deserialization and then how to wrap functions with my custom serialization functions.

Using pygccxml

So first, I will parse the file whose name is in a variable named myfile. The config variable is only needed if you need additional include paths or stuff like this. Indeed, GCCXML crashes if one header is not readable (and thus accessible).

I’ve written these functions for the simple types (int, float, …) as well as for STL containers. It’s easy to write, so I won’t write them here (some lexical_cast<> are enough for the native types, and a for loop is needed for the container, that’s all). get_value() modifies the argument based on an XML element, and set_value() sets the XML Element according to the

Let’s say I’d like to get all type declarations in a header file that I’ve already parsed and I have know global_ns

def get_members(new_class):
members ={}for variable in new_class.variables():
if variable.access_type=="public":
members[variable.name]= variable.type.decl_stringreturn members

For each variable in a class, if it is public, I will add it to the member to serialize or deserialize. Its name is given by the property name and its complete type by the property type.decl_string.

Now that I can extract the scope of a class. This is not recursively until I hit the top_parent namespace.

Here, I browse the different class definitions GCCXML has found, and if the declaration was made in my header file (I have to check this because GCCXML populates the structure with the types in every included file), I extract its member and its namespace scope. This is then saved in a dictionary.

Now, to generate a serialization/deserialization functions couple, I can write their prototype and then the source code like this (for instance):

Of course, there are some pitfalls in what I’ve just written: I’ve assumed that the types inside my structure can be serialized, that I have no static member, … This is left as an exercice.

Wrapping functions

Wrapping functions with this can get to RPC, and thus SOAP, as I’m using here XML as channel.
In fact, there are not many differences between wrapping functions and wrapping types. The only difference is that I don’t search for the same things in pygccxml structure. Besides, functions can modify their parameters, there may be pointers, … I myself only dealt with functions modifying their parameters, but I will not add this complexity here.

Here I save inside a map another map of function members. Now, I can create the RPC functions. Depending on which side you are, you need a class with the same interface that will make the RPC call, thus serializing parameters and deserialize the result after, and a class called by the RPC server that will deserialize the parameters, make the actual method call and then serialize the result.

For instance, generating a method call for a class can be done this way, with inner being a pointer to the actual wrapped class instance:

Final word

I’m using pygccxml for some RPCs in a client/server environment for clusters, to know if a computation is done or how much time is remaining. It is easy to use SCons with pygccxml to create a Builder that will create the files on the fly (and everything stays in Python).