Browse Source

Unify the namespace and declaration context handling.

pull/1/head
triton 13 years ago
parent
commit
74404222c2
  1. 37
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs

37
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -112,7 +112,7 @@ namespace CppSharp.Generators.CSharp
WriteStartBraceIndent(); WriteStartBraceIndent();
} }
GenerateNamespace(TranslationUnit); GenerateDeclContext(TranslationUnit);
if (Options.GenerateLibraryNamespace) if (Options.GenerateLibraryNamespace)
WriteCloseBraceIndent(); WriteCloseBraceIndent();
@ -132,18 +132,21 @@ namespace CppSharp.Generators.CSharp
WriteLine("//----------------------------------------------------------------------------"); WriteLine("//----------------------------------------------------------------------------");
} }
private void GenerateNamespace(Namespace @namespace) private void GenerateDeclContext(DeclarationContext context)
{ {
bool isGlobalNamespace = @namespace is TranslationUnit; var isNamespace = context is Namespace;
var isTranslationUnit = context is TranslationUnit;
if (!isGlobalNamespace) var shouldGenerateNamespace = isNamespace && !isTranslationUnit;
if (shouldGenerateNamespace)
{ {
WriteLine("namespace {0}", @namespace.Name); WriteLine("namespace {0}", context.Name);
WriteStartBraceIndent(); WriteStartBraceIndent();
} }
// Generate all the enum declarations for the module. // Generate all the enum declarations.
foreach (var @enum in @namespace.Enums) foreach (var @enum in context.Enums)
{ {
if (@enum.Ignore || @enum.IsIncomplete) if (@enum.Ignore || @enum.IsIncomplete)
continue; continue;
@ -153,8 +156,8 @@ namespace CppSharp.Generators.CSharp
NeedNewLine(); NeedNewLine();
} }
// Generate all the typedef declarations for the module. // Generate all the typedef declarations.
foreach (var typedef in @namespace.Typedefs) foreach (var typedef in context.Typedefs)
{ {
if (typedef.Ignore) continue; if (typedef.Ignore) continue;
@ -166,8 +169,8 @@ namespace CppSharp.Generators.CSharp
NeedNewLine(); NeedNewLine();
} }
// Generate all the struct/class declarations for the module. // Generate all the struct/class declarations.
foreach (var @class in @namespace.Classes) foreach (var @class in context.Classes)
{ {
if (@class.Ignore || @class.IsIncomplete) if (@class.Ignore || @class.IsIncomplete)
continue; continue;
@ -180,24 +183,24 @@ namespace CppSharp.Generators.CSharp
NeedNewLine(); NeedNewLine();
} }
if (@namespace.HasFunctions) if (context.HasFunctions)
{ {
NewLineIfNeeded(); NewLineIfNeeded();
WriteLine("public partial class {0}{1}", SafeIdentifier(Options.LibraryName), WriteLine("public partial class {0}{1}", SafeIdentifier(Options.LibraryName),
TranslationUnit.FileNameWithoutExtension); TranslationUnit.FileNameWithoutExtension);
WriteStartBraceIndent(); WriteStartBraceIndent();
// Generate all the function declarations for the module. // Generate all the function declarations.
foreach (var function in @namespace.Functions) foreach (var function in context.Functions)
GenerateFunction(function); GenerateFunction(function);
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }
foreach(var childNamespace in @namespace.Namespaces) foreach(var childNamespace in context.Namespaces)
GenerateNamespace(childNamespace); GenerateDeclContext(childNamespace);
if (!isGlobalNamespace) if (shouldGenerateNamespace)
WriteCloseBraceIndent(); WriteCloseBraceIndent();
} }

Loading…
Cancel
Save