views:

370

answers:

3

If you have a simple enough schema with basic boolean string fields, how to go about writing a code generator in C++. If you ever wrote , how did you start. Articles/recommendation welcome.

EDIT: Note that this is not the standard CORBA idl.

A: 

Comet is pretty good library to implement COM.

Vadim Ferderer
+1  A: 

In summary:

  1. Convert something by hand,
  2. copy that output into a string literal and then
  3. generalise with loops and variables instead of fixed names.

This is a particularly good problem to tackle with TDD - start by setting up a framework such as CPPUnit (or one of many alternatives) with stupidly simple tests that just ensure you can load a file and iterate its contents by writing them out.

Approach the solution very incrementally - get something simple and general output, with a test to confirm it works, then add a more sophisticated variation. eg: handle single parameter functions first.

For code up to a moderate range of complexity, just have a long set of output statements using the standard C++ ostream classes which will allow you to write lots of code mixing literals with any variables.

Andy Dent
A: 

Unfortunately your question lacks details about what exactly you are trying to generate. However I will take a stab and recommend that you look at the C++ boost library spirit (http://spirit.sourceforge.net). With Spirit you can create parsers. So assuming you have written a spirit parser for your DSL (domain specific language - your IDL) you will get an AST (abstract syntax tree) after parsing your IDL file(s) that you can walk with the visitor pattern and by creating appropriate visitors you can generate whatever output you need, e.g. generating code.

lothar