Browse Source

Generator: Customization for const char[]

Allow the user to choose whether `const char[]` should be marshalled as
`string` or a normal `char` array in C#.

A new option `MarshalConstCharArrayAsString` is added, and is `true`
by default.

This helps in situations where the original C++ API distinguishes
between C-strings and char arrays using the two different notations.
pull/1748/head
Trung 2 years ago
parent
commit
fbdfa37c9a
No known key found for this signature in database
GPG Key ID: 8C6357127C5190F6
  1. 3
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 3
      src/Generator/Generators/CLI/CLITypePrinter.cs
  3. 3
      src/Generator/Generators/CSharp/CSharpMarshal.cs
  4. 2
      src/Generator/Generators/CSharp/CSharpSources.cs
  5. 3
      src/Generator/Generators/CSharp/CSharpTypePrinter.cs
  6. 5
      src/Generator/Options.cs

3
src/Generator/Generators/CLI/CLIMarshal.cs

@ -61,7 +61,8 @@ namespace CppSharp.Generators.CLI @@ -61,7 +61,8 @@ namespace CppSharp.Generators.CLI
break;
case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string
if (array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
if (Context.Context.Options.MarshalConstCharArrayAsString &&
array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
{
var pointer = new PointerType { QualifiedPointee = array.QualifiedType };

3
src/Generator/Generators/CLI/CLITypePrinter.cs

@ -35,7 +35,8 @@ namespace CppSharp.Generators.CLI @@ -35,7 +35,8 @@ namespace CppSharp.Generators.CLI
TypeQualifiers quals)
{
// const char* and const char[] are the same so we can use a string
if (array.SizeType == ArrayType.ArraySize.Incomplete &&
if (Context.Options.MarshalConstCharArrayAsString &&
array.SizeType == ArrayType.ArraySize.Incomplete &&
array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
return VisitCILType(new CILType(typeof(string)), quals);

3
src/Generator/Generators/CSharp/CSharpMarshal.cs

@ -116,7 +116,8 @@ namespace CppSharp.Generators.CSharp @@ -116,7 +116,8 @@ namespace CppSharp.Generators.CSharp
break;
case ArrayType.ArraySize.Incomplete:
// const char* and const char[] are the same so we can use a string
if (array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
if (Context.Context.Options.MarshalConstCharArrayAsString &&
array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
{
var pointer = new PointerType { QualifiedPointee = array.QualifiedType };

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

@ -972,7 +972,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat @@ -972,7 +972,7 @@ internal static bool {Helpers.TryGetNativeToManagedMappingIdentifier}(IntPtr nat
string ptr = Generator.GeneratedIdentifier("ptr");
if (arrayType != null)
{
if (arrayType.Type.IsPrimitiveType(PrimitiveType.Char) && arrayType.SizeType != ArrayType.ArraySize.Constant)
if (Context.Options.MarshalConstCharArrayAsString && arrayType.Type.IsPrimitiveType(PrimitiveType.Char) && arrayType.SizeType != ArrayType.ArraySize.Constant)
WriteLine($"var {ptr} = {location};");
else
WriteLine($"var {ptr} = ({arrayType.Type.Visit(TypePrinter)}*){location};");

3
src/Generator/Generators/CSharp/CSharpTypePrinter.cs

@ -111,7 +111,8 @@ namespace CppSharp.Generators.CSharp @@ -111,7 +111,8 @@ namespace CppSharp.Generators.CSharp
}
// const char* and const char[] are the same so we can use a string
if (array.SizeType == ArrayType.ArraySize.Incomplete &&
if (Context.Options.MarshalConstCharArrayAsString &&
array.SizeType == ArrayType.ArraySize.Incomplete &&
arrayType.IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
return "string";

5
src/Generator/Options.cs

@ -115,7 +115,7 @@ namespace CppSharp @@ -115,7 +115,7 @@ namespace CppSharp
/// <summary>
/// Enable this option to enable generation of finalizers. Works in both CLI and
/// C# backends.
/// C# backends.
/// </summary>
/// <remarks>
/// Use <see cref="GenerateFinalizersFilter"/> to specify a filter so that
@ -125,7 +125,7 @@ namespace CppSharp @@ -125,7 +125,7 @@ namespace CppSharp
/// <summary>
/// A filter that can restrict the classes for which finalizers are generated when
/// <see cref="GenerateFinalizers"/> is <c>true</c>.
/// <see cref="GenerateFinalizers"/> is <c>true</c>.
/// </summary>
/// <remarks>
/// The default filter performs no filtering so that whenever <see
@ -189,6 +189,7 @@ namespace CppSharp @@ -189,6 +189,7 @@ namespace CppSharp
public readonly List<string> DependentNameSpaces = new List<string>();
public bool MarshalCharAsManagedChar { get; set; }
public bool MarshalConstCharArrayAsString { get; set; } = true;
/// <summary>
/// Use Span Struct instead of Managed Array

Loading…
Cancel
Save