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

129
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,6 +127,28 @@ namespace Cxxi.Generators.CLI
if (needsNewline) if (needsNewline)
NewLine(); 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), WriteLine("public ref class {0}{1}", SafeIdentifier(Library.Name),
Module.FileNameWithoutExtension); Module.FileNameWithoutExtension);
WriteLine("{"); WriteLine("{");
@ -152,9 +165,6 @@ namespace Cxxi.Generators.CLI
WriteLine("};"); WriteLine("};");
} }
PopIndent();
}
public void GenerateDeclarationCommon(Declaration T) public void GenerateDeclarationCommon(Declaration T)
{ {
GenerateSummary(T.BriefComment); GenerateSummary(T.BriefComment);
@ -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,16 +200,35 @@ 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)
{ {
if (!@class.IsValueType)
return;
PushIndent(); PushIndent();
foreach(var field in @class.Fields) foreach (var field in @class.Fields)
{ {
if (field.Ignore) continue; if (field.Ignore) continue;
@ -230,33 +240,55 @@ namespace Cxxi.Generators.CLI
PopIndent(); PopIndent();
} }
// Generate a property for each field if class is not value type public void GenerateClassMethods(Class @class)
if (@class.IsRefType)
{ {
PushIndent(); PushIndent();
foreach (var field in @class.Fields) foreach (var method in @class.Methods)
{ {
if (CheckIgnoreField(@class, field)) if (CheckIgnoreMethod(@class, method))
continue; continue;
GenerateDeclarationCommon(field); GenerateDeclarationCommon(method);
GenerateFieldProperty(field); GenerateMethod(method);
} }
PopIndent(); 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(); PushIndent();
foreach (var method in @class.Methods) foreach (var field in @class.Fields)
{ {
if (CheckIgnoreMethod(@class, method)) if (CheckIgnoreField(@class, field))
continue; continue;
GenerateDeclarationCommon(method); GenerateDeclarationCommon(field);
GenerateMethod(method); GenerateFieldProperty(field);
} }
PopIndent(); PopIndent();
WriteLine("};");
} }
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