Can anyone point me to some documentation on how to write scripts in Python (or Perl or any other Linux friendly script language) that generate C++ code from XML or py files from the command line. I'd like to be able to write up some xml files and then run a shell command that reads these files and generates .h files with fully inlined functions, e.g. streaming operators, constructors, etc.
I actually do some work with a legacy "4GL" development environment that does something similar, and a lot of models on the old 4GL paradigm use C and C++ as the language that they output their generated code in.
That being said, 4GL is pretty much resigned to the dust heap of history. The problem is, when you're machine generating the C code, you're losing the performance gains that would have resulted from using C in the first place, and the code is extremely difficult to maintain. Might as well just write the program in Python in the first place.
You could take a look at Shedskin, a project that generates C++ code from Python code.
Depending on what your reasons are, it may be a little pointless as Satanicpuppy pointed out.
I'm afraid you will not find an already-built in solution that takes your particular xml or python files and transforms them onto your required output "out of the box".
You will have to implement the parsing, the data treatment and output yourself. Not all by yourself, though; here are some pointers regarding the parsing and output.
Phython comes with 2 different XML parsers (SAX and DOM -scroll down to see some examples). You will have to use one of them in order to read the source files.
For generating the output more easily, you can probably use a templating library, such as StringTemplate, or just generate the code manually, if it's small.
A few years ago I worked on a project to simplify interprocess shared memory management for large scale simulation systems. We used a related approach where the layout of data in shared memory was defined in XML files and a code generator, written in python, read the XML and spit out a set of header files defining structures and associated functions/operators/etc to match the XML description. At the time, I looked at several templating engines and, to my surprise, found it was easier and very striaight-forward to just do it "by hand".
As you read the XML, just populate a set of datastructures that match your code. Header file objects contain classes and clsses contain variables (which may be of other class types). Give each object a printSelf()
method that iterates over it's contents and calls printSelf()
for each object it contains.
It seems a little daunting at first but once you get started, it's pretty straight forward. Oh, and one tip that helps with the generated code, add an indentation argument to printSelf()
and increase it at each level. It makes the generated code much easier to read.