views:

419

answers:

7

I have a set of applications that are being built using a combination of C# and C++. We have a set of shared objects between the two languages, and rather than define each one separately in each language, I would prefer to use a code generation tool. Ideally such a tool would be FOSS, although that's not an absolute requirement. The objects themselves are relatively simple, although there is inheritance from baseclasses, implementation of interfaces, containment of other object types, and collections of other object types.

The C++ target environment is Visual C++ 2008.

Does anyone have any recommendations for a tool that can handle this kind of task?

Example code:

public class Tax 
{ 
private static Dictionary<string, double> _TaxRates; 
public Dictionary<string,double> TaxRates { get { return _TaxRates; } }
}
+6  A: 

For any code generation problem I'd take a good look at T4 (the text templating functionality that appeared in VS.NET 2008).

A good place to start with T4 is...

http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx

You would need different templates for C++ and C#, and would drive your code generation from some other metadata.

The alternative is to use the CodeDom API. This has two CodeDomProviders (CppCodeProvider and CSharpCodeProvider) that can target each language.

For more information:

http://msdn.microsoft.com/en-us/library/system.codedom.compiler.aspx

There's a related links on SO:

http://stackoverflow.com/questions/491900/t4-vs-codedom-vs-oslo http://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-code-fragments

Martin Peck
Thanks I'll take a look.
endian
+1  A: 

The .Net classes for emitting source code, for example the GenerateCodeFromCompileUnit method of the CodeDomProvider base class, can be used to emit source code in various languages from a single definition: for example, there's a CppCodeProvider as well as a CSharpCodeProvider. It may only be managed C++, but you did say "relatively simple".

ChrisW
+2  A: 

cog is a code-generation tool that lets you embed code-generation python code in your C++/C# files. Its advantage is that it's really easy to use, especially if you already know python.

Ryan Ginstrom
+1  A: 

Google Protobuf might one of the best solution available for free.

See the language page.

Klaim
Thanks, but does it allow you to target C# as an output?
endian
Yes - see the link i added.
Klaim
+1  A: 

You can also investigate CodeSmith. You can write templates in asp.net like language to generate any sort of code.

Conrad
A: 

The way I've done this multiple times is to define my own LL language and parse it, and have it generate class definitions in either language, OR write the class definitions in one target language, and then parse them to generate the other language.

Mike Dunlavey
A: 

I can highly recommend using Ruby as a code generator tool.

It is easy to create internal DSL's in Ruby and to generate code from it.

I use Ruby every day to generate both C++ and C# code. It has dramatically reduced the time I need to write the boring/tedious part of the application.

MB