-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCSharp2CPP
More file actions
115 lines (102 loc) · 4.36 KB
/
CSharp2CPP
File metadata and controls
115 lines (102 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<#@ 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 = false; string dllExport ="DECL_EXPORT"; List<Type> ccs = new List<Type>();
ccs.Add(typeof(namespace.CLASS1));
ccs.Add(typeof(namespace.CLASS2));
ccs.Add(typeof(namespace.CLASS3));
ccs.Add(typeof(namespace.CLASS4));
GetPropertiesAll(ccs, dllExport, useQTTypes);
#>
<#+ private void GetPropertiesAll(List<Type> ccs, string dllExport, bool useQTTypes)
{
foreach(Type cc in ccs)
{
GetProperties(ccs, cc, dllExport, useQTTypes);
}
}
private void GetProperties(List<Type> ccs, Type t, string dllExport, bool useQTTypes)
{
string classPrefix="C";
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== false)
{
WriteLine("#include \"stdafx.h\"");
WriteLine("#include \"property.h\"");
}
if(t.IsClass || t.IsInterface)
GenerateClass(ccs, t, classPrefix+objectName, dllExport, useQTTypes);
else if(t.IsEnum)
GenerateFromEnum(t, objectName, dllExport, useQTTypes);
WriteLine("#endif // " + objectName.ToUpper() + "_H\r\n");
SaveOutput(objectName + ".h");
}
private void GenerateClass(List<Type> ccs, Type t, string objectName, string dllExport, bool useQTTypes)
{
GenerateIncludes(ccs, t, objectName, useQTTypes);
WriteLine("");
var properties = t.GetProperties().ToList();
WriteLine("class " + dllExport + " " + objectName);
WriteLine("{");
WriteLine("public:");
WriteLine("\t" + objectName + "();");
WriteLine("\t~" + objectName + "();");
foreach(var p in properties){
WriteLine("\tproperty<" + getAsQtProperty(p, useQTTypes) + "> " + p.Name + ";");
}
WriteLine("};");
}
private void GenerateIncludes(List<Type> 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, string dllExport, bool useQTTypes)
{
FieldInfo[] memberInfos = t.GetFields(BindingFlags.Public | BindingFlags.Static);
WriteLine("enum " + 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":"std::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":"std::tm"; case "Boolean": return useQTTypes?"bool":"BOOL"; case "List`1": return useQTTypes?"QList<" + getListTypes(p, useQTTypes) +">":"vector<" + getListTypes(p, useQTTypes) +">";
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":"void*";
}
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);
}
#>