C# to C++ / Qt T4 generator – converter

8 Sep
2015

Original file: C# to C++ / Qt T4 generator

<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ output extension=".h" #>
<#@ assembly name="System" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="PATH_TO_REFERENCE_DLL.dll" #>

<# bool useQTTypes = true; List ccs = new List()
{
typeof(namespace.CLASS1),
typeof(namespace.CLASS2),
typeof(namespace.CLASS3)
};
GetPropertiesAll(ccs, useQTTypes);
#>
<#+ private void GetPropertiesAll(List ccs, bool useQTTypes)
{
foreach(Type cc in ccs)
{
GetProperties(ccs, cc,useQTTypes);
}
}
private void GetProperties(List ccs, Type t, bool useQTTypes)
{
string objectName = t.Name;
WriteLine(“//”);
WriteLine(“// Copyright © ” + DateTime.Now.ToString(“yyyy”) +”, Cihan T. (Generate Date: [” + DateTime.Now.ToString(“yyyy-MM-dd HH:mm:00″) +”])”);
WriteLine(“//”);
WriteLine(“#ifndef ” + objectName.ToUpper() + “_H”);
WriteLine(“#define ” + objectName.ToUpper() + “_H”);
WriteLine(“#include “);

if(useQTTypes)
{
WriteLine(“#include “);
}
if(t.IsClass || t.IsInterface)
GenerateClass(ccs, t, objectName, useQTTypes);
else if(t.IsEnum)
GenerateFromEnum(t, objectName, useQTTypes);

WriteLine(“#endif // ” + objectName.ToUpper() + “_H\r\n”);

SaveOutput(objectName + “.h”);
}
private void GenerateClass(List ccs,Type t, string objectName, bool useQTTypes)
{
GenerateIncludes(ccs, t, objectName, useQTTypes);
var properties = t.GetProperties().ToList();
WriteLine(“class ” + objectName);
WriteLine(“{“);
WriteLine(“public:”);
WriteLine(“\t” + objectName + “();”);

foreach(var p in properties){
WriteLine(“\tproperty<" + getAsQtProperty(p, useQTTypes) + "> ” + p.Name + “;”);
}
WriteLine(“};”);
}

private void GenerateIncludes(List ccs,Type t, string objectName, bool useQTTypes)
{
var properties = t.GetProperties().ToList();

foreach(var p in properties){
bool contains = ccs.Any(cc => cc.Name == p.PropertyType.Name);
if(contains)
WriteLine(“#include <" + p.PropertyType.Name + ".h>“);
}
}
private void GenerateFromEnum(Type t, string objectName, bool useQTTypes)
{
FieldInfo[] memberInfos = t.GetFields(BindingFlags.Public | BindingFlags.Static);

WriteLine(“enum class ” + objectName + “”);
WriteLine(“{“);

for(int i = 0; i < memberInfos.Length; i++){ Write("\t" + memberInfos[i].Name + " = " + memberInfos[i].GetRawConstantValue().ToString()); if(i < memberInfos.Length - 1) Write(","); Write("\r\n"); } WriteLine("};"); } private string getAsQtProperty(PropertyInfo p, bool useQTTypes) { switch(p.PropertyType.Name){ case "String": return useQTTypes?"QString":"string"; case "Int32": return "int"; case "Decimal": return "float"; case "Double": return "float"; case "Char": return "char"; case "Byte": return "char"; case "DateTime": return useQTTypes?"QDateTime":"DateTime"; case "Boolean": return useQTTypes?"bool":"BOOL"; case "List`1": return useQTTypes?"QList<" + getListTypes(p, useQTTypes) +">“:”vector<>“;
case “Dictionary`2”:
return useQTTypes?”QMap<" + getDictionaryTypes(p, useQTTypes) + ">“:”map<"+ getDictionaryTypes(p, useQTTypes) +">“;
default:
return p.PropertyType.Name;
}
}

private string getDictionaryTypes(System.Reflection.PropertyInfo p, bool useQTTypes)
{
return string.Empty;
/*
return string.Join( “, “, p.GetGenericArguments()[0].Select(c => c.GetType().Name).ToList());
*/
}
private string getListTypes(System.Reflection.PropertyInfo p, bool useQTTypes)
{
var a = p.GetType().GetGenericArguments().FirstOrDefault();

if(a != null) return a.Name;
else return useQTTypes?”QObject”:”object”;

}
void SaveOutput(string outputFileName)
{
string templateDirectory = Path.GetDirectoryName(Host.TemplateFile);
string outputFilePath = Path.Combine(templateDirectory, outputFileName);
File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString());

this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#>

Be Sociable, Share!

Comment Form

top