Browse Source

Extract a couple of methods in CLIHeadersTemplate.cs to make the code more readable.

pull/1/head
triton 13 years ago
parent
commit
2c3596ce8b
  1. 173
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs

173
src/Generator/Generators/CLI/CLIHeadersTemplate.cs

@ -81,7 +81,7 @@ namespace Cxxi.Generators.CLI
bool needsNewline = false; bool needsNewline = false;
// Generate all the enum declarations for the module. // Generate all the enum declarations for the module.
for (int i = 0; i < Module.Enums.Count; ++i) for (var i = 0; i < Module.Enums.Count; ++i)
{ {
var @enum = Module.Enums[i]; var @enum = Module.Enums[i];
@ -100,16 +100,7 @@ namespace Cxxi.Generators.CLI
needsNewline = false; needsNewline = false;
// Generate all the typedef declarations for the module. // Generate all the typedef declarations for the module.
foreach (var typedef in Module.Typedefs) GenerateTypedefs();
{
if (typedef.Ignore)
continue;
if (!GenerateTypedef(typedef))
continue;
NewLine();
}
needsNewline = false; needsNewline = false;
@ -136,23 +127,42 @@ namespace Cxxi.Generators.CLI
if (needsNewline) if (needsNewline)
NewLine(); NewLine();
WriteLine("public ref class {0}{1}", SafeIdentifier(Library.Name), GenerateFunctions();
Module.FileNameWithoutExtension); }
WriteLine("{");
WriteLine("public:"); PopIndent();
PushIndent(); }
// Generate all the function declarations for the module. public void GenerateTypedefs()
foreach (var function in Module.Functions) {
{ foreach (var typedef in Module.Typedefs)
GenerateFunction(function); {
} if (typedef.Ignore)
continue;
PopIndent(); if (!GenerateTypedef(typedef))
WriteLine("};"); continue;
NewLine();
}
}
public void GenerateFunctions()
{
WriteLine("public ref class {0}{1}", SafeIdentifier(Library.Name),
Module.FileNameWithoutExtension);
WriteLine("{");
WriteLine("public:");
PushIndent();
// Generate all the function declarations for the module.
foreach (var function in Module.Functions)
{
GenerateFunction(function);
} }
PopIndent(); PopIndent();
WriteLine("};");
} }
public void GenerateDeclarationCommon(Declaration T) public void GenerateDeclarationCommon(Declaration T)
@ -177,27 +187,8 @@ namespace Cxxi.Generators.CLI
Console.WriteLine("Unions are not yet implemented"); Console.WriteLine("Unions are not yet implemented");
} }
Write("public "); if (GenerateClassProlog(@class))
if (@class.IsValueType)
Write("value struct ");
else
Write("ref class ");
Write("{0}", SafeIdentifier(@class.Name));
if (@class.IsOpaque)
{
WriteLine(";");
return; return;
}
if (@class.HasBase)
Write(" : {0}", SafeIdentifier(@class.Bases[0].Class.Name));
WriteLine(string.Empty);
WriteLine("{");
WriteLine("public:");
var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName); var nativeType = string.Format("::{0}*", @class.QualifiedOriginalName);
@ -209,42 +200,48 @@ namespace Cxxi.Generators.CLI
NewLine(); NewLine();
} }
GenerateClassConstructors(@class, nativeType);
GenerateClassFields(@class);
// Generate a property for each field if class is not value type
if (@class.IsRefType)
GenerateClassProperties(@class);
GenerateClassMethods(@class);
WriteLine("};");
}
public void GenerateClassConstructors(Class @class, string nativeType)
{
// Output a default constructor that takes the native pointer. // Output a default constructor that takes the native pointer.
PushIndent(); PushIndent();
WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), nativeType); WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), nativeType);
WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), "System::IntPtr"); WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), "System::IntPtr");
PopIndent(); PopIndent();
}
if (@class.IsValueType) public void GenerateClassFields(Class @class)
{ {
PushIndent(); if (!@class.IsValueType)
foreach(var field in @class.Fields) return;
{
if (field.Ignore) continue;
GenerateDeclarationCommon(field);
if (@class.IsUnion)
WriteLine("[FieldOffset({0})]", field.Offset);
WriteLine("{0} {1};", field.Type, SafeIdentifier(field.Name));
}
PopIndent();
}
// Generate a property for each field if class is not value type PushIndent();
if (@class.IsRefType) foreach (var field in @class.Fields)
{ {
PushIndent(); if (field.Ignore) continue;
foreach (var field in @class.Fields)
{ GenerateDeclarationCommon(field);
if (CheckIgnoreField(@class, field)) if (@class.IsUnion)
continue; WriteLine("[FieldOffset({0})]", field.Offset);
WriteLine("{0} {1};", field.Type, SafeIdentifier(field.Name));
GenerateDeclarationCommon(field);
GenerateFieldProperty(field);
}
PopIndent();
} }
PopIndent();
}
public void GenerateClassMethods(Class @class)
{
PushIndent(); PushIndent();
foreach (var method in @class.Methods) foreach (var method in @class.Methods)
{ {
@ -255,8 +252,43 @@ namespace Cxxi.Generators.CLI
GenerateMethod(method); GenerateMethod(method);
} }
PopIndent(); PopIndent();
}
WriteLine("};"); public bool GenerateClassProlog(Class @class)
{
Write("public ");
Write(@class.IsValueType ? "value struct " : "ref class ");
Write("{0}", SafeIdentifier(@class.Name));
if (@class.IsOpaque)
{
WriteLine(";");
return true;
}
if (@class.HasBase)
Write(" : {0}", SafeIdentifier(@class.Bases[0].Class.Name));
WriteLine(string.Empty);
WriteLine("{");
WriteLine("public:");
return false;
}
public void GenerateClassProperties(Class @class)
{
PushIndent();
foreach (var field in @class.Fields)
{
if (CheckIgnoreField(@class, field))
continue;
GenerateDeclarationCommon(field);
GenerateFieldProperty(field);
}
PopIndent();
} }
public void GenerateFieldProperty(Field field) public void GenerateFieldProperty(Field field)
@ -266,7 +298,6 @@ namespace Cxxi.Generators.CLI
var type = field.Type.Visit(Type.TypePrinter); var type = field.Type.Visit(Type.TypePrinter);
WriteLine("property {0} {1};", type, field.Name); WriteLine("property {0} {1};", type, field.Name);
NewLine();
} }
public void GenerateMethod(Method method) public void GenerateMethod(Method method)

Loading…
Cancel
Save