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. 127
      src/Generator/Generators/CLI/CLIHeadersTemplate.cs

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

@ -81,7 +81,7 @@ namespace Cxxi.Generators.CLI @@ -81,7 +81,7 @@ namespace Cxxi.Generators.CLI
bool needsNewline = false;
// 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];
@ -100,16 +100,7 @@ namespace Cxxi.Generators.CLI @@ -100,16 +100,7 @@ namespace Cxxi.Generators.CLI
needsNewline = false;
// Generate all the typedef declarations for the module.
foreach (var typedef in Module.Typedefs)
{
if (typedef.Ignore)
continue;
if (!GenerateTypedef(typedef))
continue;
NewLine();
}
GenerateTypedefs();
needsNewline = false;
@ -136,6 +127,28 @@ namespace Cxxi.Generators.CLI @@ -136,6 +127,28 @@ namespace Cxxi.Generators.CLI
if (needsNewline)
NewLine();
GenerateFunctions();
}
PopIndent();
}
public void GenerateTypedefs()
{
foreach (var typedef in Module.Typedefs)
{
if (typedef.Ignore)
continue;
if (!GenerateTypedef(typedef))
continue;
NewLine();
}
}
public void GenerateFunctions()
{
WriteLine("public ref class {0}{1}", SafeIdentifier(Library.Name),
Module.FileNameWithoutExtension);
WriteLine("{");
@ -152,9 +165,6 @@ namespace Cxxi.Generators.CLI @@ -152,9 +165,6 @@ namespace Cxxi.Generators.CLI
WriteLine("};");
}
PopIndent();
}
public void GenerateDeclarationCommon(Declaration T)
{
GenerateSummary(T.BriefComment);
@ -177,27 +187,8 @@ namespace Cxxi.Generators.CLI @@ -177,27 +187,8 @@ namespace Cxxi.Generators.CLI
Console.WriteLine("Unions are not yet implemented");
}
Write("public ");
if (@class.IsValueType)
Write("value struct ");
else
Write("ref class ");
Write("{0}", SafeIdentifier(@class.Name));
if (@class.IsOpaque)
{
WriteLine(";");
if (GenerateClassProlog(@class))
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);
@ -209,14 +200,33 @@ namespace Cxxi.Generators.CLI @@ -209,14 +200,33 @@ namespace Cxxi.Generators.CLI
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.
PushIndent();
WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), nativeType);
WriteLine("{0}({1} native);", SafeIdentifier(@class.Name), "System::IntPtr");
PopIndent();
}
if (@class.IsValueType)
public void GenerateClassFields(Class @class)
{
if (!@class.IsValueType)
return;
PushIndent();
foreach (var field in @class.Fields)
{
@ -230,33 +240,55 @@ namespace Cxxi.Generators.CLI @@ -230,33 +240,55 @@ namespace Cxxi.Generators.CLI
PopIndent();
}
// Generate a property for each field if class is not value type
if (@class.IsRefType)
public void GenerateClassMethods(Class @class)
{
PushIndent();
foreach (var field in @class.Fields)
foreach (var method in @class.Methods)
{
if (CheckIgnoreField(@class, field))
if (CheckIgnoreMethod(@class, method))
continue;
GenerateDeclarationCommon(field);
GenerateFieldProperty(field);
GenerateDeclarationCommon(method);
GenerateMethod(method);
}
PopIndent();
}
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 method in @class.Methods)
foreach (var field in @class.Fields)
{
if (CheckIgnoreMethod(@class, method))
if (CheckIgnoreField(@class, field))
continue;
GenerateDeclarationCommon(method);
GenerateMethod(method);
GenerateDeclarationCommon(field);
GenerateFieldProperty(field);
}
PopIndent();
WriteLine("};");
}
public void GenerateFieldProperty(Field field)
@ -266,7 +298,6 @@ namespace Cxxi.Generators.CLI @@ -266,7 +298,6 @@ namespace Cxxi.Generators.CLI
var type = field.Type.Visit(Type.TypePrinter);
WriteLine("property {0} {1};", type, field.Name);
NewLine();
}
public void GenerateMethod(Method method)

Loading…
Cancel
Save