Browse Source

Use `PushWriteBlock` to improve code readability

295227ce-f4a3-466b-a694-65c571873871
josetr 3 years ago
parent
commit
b024a92570
  1. 78
      src/Generator/Generators/CSharp/CSharpSources.cs
  2. 36
      src/Generator/Utils/BlockGenerator.cs

78
src/Generator/Generators/CSharp/CSharpSources.cs

@ -113,13 +113,10 @@ namespace CppSharp.Generators.CSharp
group spec by module.OutputNamespace into @group group spec by module.OutputNamespace into @group
select @group) select @group)
{ {
PushBlock(BlockKind.Namespace); using (!string.IsNullOrEmpty(group.Key)
if (!string.IsNullOrEmpty(group.Key)) ? PushWriteBlock(BlockKind.Namespace, $"namespace {group.Key}", NewLineKind.BeforeNextBlock)
: default)
{ {
WriteLine($"namespace {group.Key}");
WriteOpenBraceAndIndent();
}
foreach (var template in from s in @group foreach (var template in from s in @group
group s by s.TemplatedDecl.TemplatedClass into template group s by s.TemplatedDecl.TemplatedClass into template
select template) select template)
@ -145,10 +142,7 @@ namespace CppSharp.Generators.CSharp
foreach (var declarationContext in declarationContexts) foreach (var declarationContext in declarationContexts)
UnindentAndWriteCloseBrace(); UnindentAndWriteCloseBrace();
} }
}
if (!string.IsNullOrEmpty(group.Key))
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
} }
if (Options.GenerationOutputMode == GenerationOutputMode.FilePerUnit) if (Options.GenerationOutputMode == GenerationOutputMode.FilePerUnit)
@ -194,22 +188,11 @@ namespace CppSharp.Generators.CSharp
var shouldGenerateNamespace = !@namespace.IsInline && !isTranslationUnit && var shouldGenerateNamespace = !@namespace.IsInline && !isTranslationUnit &&
context.Declarations.Any(d => d.IsGenerated || (d is Class && !d.IsIncomplete)); context.Declarations.Any(d => d.IsGenerated || (d is Class && !d.IsIncomplete));
if (shouldGenerateNamespace) using var _ = shouldGenerateNamespace
{ ? PushWriteBlock(BlockKind.Namespace, $"namespace {context.Name}", NewLineKind.BeforeNextBlock)
PushBlock(BlockKind.Namespace); : default;
WriteLine("namespace {0}", context.Name);
WriteOpenBraceAndIndent();
}
var ret = base.VisitNamespace(@namespace);
if (shouldGenerateNamespace)
{
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
}
return ret; return base.VisitNamespace(@namespace);
} }
public override bool VisitDeclContext(DeclarationContext context) public override bool VisitDeclContext(DeclarationContext context)
@ -316,14 +299,13 @@ namespace CppSharp.Generators.CSharp
private void GenerateClassTemplateSpecializationsInternals(Class template, private void GenerateClassTemplateSpecializationsInternals(Class template,
IList<ClassTemplateSpecialization> specializations) IList<ClassTemplateSpecialization> specializations)
{ {
PushBlock(BlockKind.Namespace); var namespaceName = string.Format("namespace {0}{1}",
var generated = GetGeneratedClasses(template, specializations);
WriteLine("namespace {0}{1}",
template.OriginalNamespace is Class && template.OriginalNamespace is Class &&
!template.OriginalNamespace.IsDependent ? !template.OriginalNamespace.IsDependent ?
template.OriginalNamespace.Name + '_' : string.Empty, template.OriginalNamespace.Name + '_' : string.Empty,
template.Name); template.Name);
WriteOpenBraceAndIndent(); using var _ = PushWriteBlock(BlockKind.Namespace, namespaceName, NewLineKind.BeforeNextBlock);
var generated = GetGeneratedClasses(template, specializations);
foreach (var nestedTemplate in template.Classes.Where( foreach (var nestedTemplate in template.Classes.Where(
c => c.IsDependent && !c.Ignore && c.Specializations.Any(s => !s.Ignore))) c => c.IsDependent && !c.Ignore && c.Specializations.Any(s => !s.Ignore)))
@ -342,22 +324,19 @@ namespace CppSharp.Generators.CSharp
if (nested != null) if (nested != null)
GenerateNestedInternals(group.Key, GetGeneratedClasses(nested, group)); GenerateNestedInternals(group.Key, GetGeneratedClasses(nested, group));
} }
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateNestedInternals(string name, IEnumerable<Class> nestedClasses) private void GenerateNestedInternals(string name, IEnumerable<Class> nestedClasses)
{ {
WriteLine($"namespace {name}"); using (WriteBlock($"namespace {name}"))
WriteOpenBraceAndIndent(); {
foreach (var nestedClass in nestedClasses) foreach (var nestedClass in nestedClasses)
{ {
GenerateClassInternals(nestedClass); GenerateClassInternals(nestedClass);
foreach (var nestedInNested in nestedClass.Classes) foreach (var nestedInNested in nestedClass.Classes)
GenerateNestedInternals(nestedInNested.Name, new[] { nestedInNested }); GenerateNestedInternals(nestedInNested.Name, new[] { nestedInNested });
} }
UnindentAndWriteCloseBrace(); }
NewLine(); NewLine();
} }
@ -2256,14 +2235,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
if (!Options.GenerateFinalizerFor(@class)) if (!Options.GenerateFinalizerFor(@class))
return; return;
PushBlock(BlockKind.Finalizer); using (PushWriteBlock(BlockKind.Finalizer, $"~{@class.Name}()", NewLineKind.BeforeNextBlock))
WriteLine("~{0}()", @class.Name);
WriteOpenBraceAndIndent();
WriteLine($"Dispose(false, callNativeDtor : { Helpers.OwnsNativeInstanceIdentifier} );"); WriteLine($"Dispose(false, callNativeDtor : { Helpers.OwnsNativeInstanceIdentifier} );");
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
} }
private void GenerateDisposeMethods(Class @class) private void GenerateDisposeMethods(Class @class)
@ -2273,16 +2246,12 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
// Generate the IDispose Dispose() method. // Generate the IDispose Dispose() method.
if (!hasBaseClass) if (!hasBaseClass)
{ {
PushBlock(BlockKind.Method); using (PushWriteBlock(BlockKind.Method, "public void Dispose()", NewLineKind.BeforeNextBlock))
WriteLine("public void Dispose()"); {
WriteOpenBraceAndIndent();
WriteLine($"Dispose(disposing: true, callNativeDtor : { Helpers.OwnsNativeInstanceIdentifier} );"); WriteLine($"Dispose(disposing: true, callNativeDtor : { Helpers.OwnsNativeInstanceIdentifier} );");
if (Options.GenerateFinalizerFor(@class)) if (Options.GenerateFinalizerFor(@class))
WriteLine("GC.SuppressFinalize(this);"); WriteLine("GC.SuppressFinalize(this);");
}
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
} }
// Declare partial method that the partial class can implement to participate // Declare partial method that the partial class can implement to participate
@ -2292,13 +2261,8 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
PopBlock(NewLineKind.BeforeNextBlock); PopBlock(NewLineKind.BeforeNextBlock);
// Generate Dispose(bool, bool) method // Generate Dispose(bool, bool) method
PushBlock(BlockKind.Method); var ext = !@class.IsValueType ? (hasBaseClass ? "override " : "virtual ") : string.Empty;
Write("internal protected "); using var _ = PushWriteBlock(BlockKind.Method, $"internal protected {ext}void Dispose(bool disposing, bool callNativeDtor )", NewLineKind.BeforeNextBlock);
if (!@class.IsValueType)
Write(hasBaseClass ? "override " : "virtual ");
WriteLine("void Dispose(bool disposing, bool callNativeDtor )");
WriteOpenBraceAndIndent();
if (@class.IsRefType) if (@class.IsRefType)
{ {
@ -2391,8 +2355,6 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
WriteLineIndent("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier); WriteLineIndent("Marshal.FreeHGlobal({0});", Helpers.InstanceIdentifier);
WriteLine("{0} = IntPtr.Zero;", Helpers.InstanceIdentifier); WriteLine("{0} = IntPtr.Zero;", Helpers.InstanceIdentifier);
UnindentAndWriteCloseBrace();
PopBlock(NewLineKind.BeforeNextBlock);
} }
private bool GenerateDestructorCall(Method dtor) private bool GenerateDestructorCall(Method dtor)

36
src/Generator/Utils/BlockGenerator.cs

@ -298,6 +298,42 @@ namespace CppSharp
return FindBlocks(kind).SingleOrDefault(); return FindBlocks(kind).SingleOrDefault();
} }
internal PushedBlock PushWriteBlock(BlockKind kind, string msg, NewLineKind next)
{
PushBlock(kind);
WriteLine(msg);
WriteOpenBraceAndIndent();
return new PushedBlock(this, next);
}
internal TextBlock WriteBlock(string msg)
{
WriteLine(msg);
WriteOpenBraceAndIndent();
return new TextBlock(this);
}
internal ref struct PushedBlock
{
private readonly BlockGenerator generator;
private readonly NewLineKind next;
public PushedBlock(BlockGenerator generator, NewLineKind next)
{
this.generator = generator;
this.next = next;
}
public void Dispose()
{
if (generator == null)
return;
generator.UnindentAndWriteCloseBrace();
generator.PopBlock(next);
}
}
#endregion #endregion
#region ITextGenerator implementation #region ITextGenerator implementation

Loading…
Cancel
Save