From 2c3596ce8b13eaee14cd1b0d94e6924991ce774e Mon Sep 17 00:00:00 2001 From: triton Date: Thu, 24 Jan 2013 01:06:27 +0000 Subject: [PATCH] Extract a couple of methods in CLIHeadersTemplate.cs to make the code more readable. --- .../Generators/CLI/CLIHeadersTemplate.cs | 173 +++++++++++------- 1 file changed, 102 insertions(+), 71 deletions(-) diff --git a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs index 0b2625a4..7e31c67e 100644 --- a/src/Generator/Generators/CLI/CLIHeadersTemplate.cs +++ b/src/Generator/Generators/CLI/CLIHeadersTemplate.cs @@ -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 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,23 +127,42 @@ namespace Cxxi.Generators.CLI if (needsNewline) NewLine(); - WriteLine("public ref class {0}{1}", SafeIdentifier(Library.Name), - Module.FileNameWithoutExtension); - WriteLine("{"); - WriteLine("public:"); - PushIndent(); + GenerateFunctions(); + } + + PopIndent(); + } - // Generate all the function declarations for the module. - foreach (var function in Module.Functions) - { - GenerateFunction(function); - } + public void GenerateTypedefs() + { + foreach (var typedef in Module.Typedefs) + { + if (typedef.Ignore) + continue; - PopIndent(); - WriteLine("};"); + if (!GenerateTypedef(typedef)) + 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(); + WriteLine("};"); } public void GenerateDeclarationCommon(Declaration T) @@ -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,42 +200,48 @@ 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) - { - PushIndent(); - foreach(var field in @class.Fields) - { - if (field.Ignore) continue; - - GenerateDeclarationCommon(field); - if (@class.IsUnion) - WriteLine("[FieldOffset({0})]", field.Offset); - WriteLine("{0} {1};", field.Type, SafeIdentifier(field.Name)); - } - PopIndent(); - } + public void GenerateClassFields(Class @class) + { + if (!@class.IsValueType) + return; - // Generate a property for each field if class is not value type - if (@class.IsRefType) + PushIndent(); + foreach (var field in @class.Fields) { - PushIndent(); - foreach (var field in @class.Fields) - { - if (CheckIgnoreField(@class, field)) - continue; - - GenerateDeclarationCommon(field); - GenerateFieldProperty(field); - } - PopIndent(); + if (field.Ignore) continue; + + GenerateDeclarationCommon(field); + if (@class.IsUnion) + WriteLine("[FieldOffset({0})]", field.Offset); + WriteLine("{0} {1};", field.Type, SafeIdentifier(field.Name)); } + PopIndent(); + } + public void GenerateClassMethods(Class @class) + { PushIndent(); foreach (var method in @class.Methods) { @@ -255,8 +252,43 @@ namespace Cxxi.Generators.CLI GenerateMethod(method); } 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) @@ -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)