C ++ and C # oriented code generation tool

I have a collection of applications that are built using a combination of C # and C ++. We have a set of common objects between two languages, and instead of defining them separately in each language, I would rather use a code generation tool. Ideally, such a tool would be FOSS, although this is not an absolute requirement. The objects themselves are relatively simple, although there is inheritance from base classes, implementation of interfaces, containment of other types of objects, and collections of other types of objects.

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

Does anyone have any recommendations for a tool that can handle such a task?

Sample code:

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

      

+1


a source to share


7 replies


For any code generation problem, I'd have a pretty good look at T4 (text templating functionality introduced in VS.NET 2008).

A good place to start with T4 is ...

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

You need different templates for C ++ and C # and you will generate code from different metadata.

An alternative is to use the CodeDom API. It 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 are links to SO:

T4 vs. CodeDom vs. Oslo Is it possible to dynamically compile and execute C # code snippets?

+7


a source


cog is a code generation tool that allows you to embed python code to generate code into C ++ / C # files. Its advantage is that it is very easy to use, especially if you already know python.



+2


a source


Google Protobuf may be one of the best solutions available for free.

See page page .

+2


a source


You can also research CodeSmith . You can write templates in asp.net as a language to generate any code.

+2


a source


... Net classes for emitting source code, such as a GenerateCodeFromCompileUnit

base class method CodeDomProvider

, can be used to emit source code in different languages ​​from a single definition: for example, there CppCodeProvider

and also a CSharpCodeProvider

. This can only be controlled by C ++, but you said "relatively simple".

+1


a source


The way I have done this several times is to define my own LL language and parse it, and make it generate class definitions in any language, or write class definitions in one target language and then parse them to create another language.

+1


a source


I highly recommend using Ruby as your coding tool.

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

I use Ruby every day to create C ++ and C # code. This has significantly reduced the time it takes me to write the boring / tedious part of the application.

+1


a source







All Articles