Skip to content

Commit 850dcfa

Browse files
committed
Changed CSharpTypePrinter.IntPtrType to be a getter property.
1 parent 579df27 commit 850dcfa

File tree

4 files changed

+18
-12
lines changed

4 files changed

+18
-12
lines changed

src/Generator/Generators/CSharp/CSharpMarshal.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public override bool VisitArrayType(ArrayType array, TypeQualifiers quals)
9898
else
9999
supportBefore.WriteLineIndent(
100100
$@"{value}[i] = {finalArrayType}.{Helpers.CreateInstanceIdentifier}(({
101-
CSharpTypePrinter.IntPtrType}) {Context.ReturnVarName}[i]);");
101+
typePrinter.IntPtrType}) {Context.ReturnVarName}[i]);");
102102
}
103103
else
104104
{
@@ -313,7 +313,7 @@ public override bool VisitTemplateParameterSubstitutionType(TemplateParameterSub
313313
if (finalType.IsDependent)
314314
Context.Return.Write($"({param.ReplacedParameter.Parameter.Name}) (object) ");
315315
if (param.Replacement.Type.Desugar().IsPointerToPrimitiveType())
316-
Context.Return.Write($"({CSharpTypePrinter.IntPtrType}) ");
316+
Context.Return.Write($"({typePrinter.IntPtrType}) ");
317317
return base.VisitTemplateParameterSubstitutionType(param, quals);
318318
}
319319

@@ -385,7 +385,7 @@ private void MarshalArray(ArrayType array)
385385
Context.Before.WriteOpenBraceAndIndent();
386386
string element = Generator.GeneratedIdentifier("element");
387387
Context.Before.WriteLine($"var {element} = {Context.ReturnVarName}[i];");
388-
var intPtrZero = $"{CSharpTypePrinter.IntPtrType}.Zero";
388+
var intPtrZero = $"{typePrinter.IntPtrType}.Zero";
389389
Context.Before.WriteLine($@"{intermediateArray}[i] = {element} == {
390390
intPtrZero} ? null : {intermediateArrayType}.{
391391
Helpers.CreateInstanceIdentifier}({element});");
@@ -626,7 +626,7 @@ public override bool VisitTemplateParameterSubstitutionType(TemplateParameterSub
626626
{
627627
Context.Return.Write($"({replacement}) ");
628628
if (replacement.IsPointerToPrimitiveType())
629-
Context.Return.Write($"({CSharpTypePrinter.IntPtrType}) ");
629+
Context.Return.Write($"({typePrinter.IntPtrType}) ");
630630
Context.Return.Write("(object) ");
631631
}
632632
return base.VisitTemplateParameterSubstitutionType(param, quals);
@@ -822,7 +822,7 @@ private void MarshalArray(ArrayType arrayType)
822822
Context.Before.WriteLine($"var {element} = {Context.Parameter.Name}[i];");
823823
if (elementType.IsAddress())
824824
{
825-
var intPtrZero = $"{CSharpTypePrinter.IntPtrType}.Zero";
825+
var intPtrZero = $"{typePrinter.IntPtrType}.Zero";
826826
Context.Before.WriteLine($@"{intermediateArray}[i] = ReferenceEquals({
827827
element}, null) ? {intPtrZero} : {element}.{Helpers.InstanceIdentifier};");
828828
}

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ public override bool VisitClassDecl(Class @class)
420420
else
421421
{
422422
WriteLine("public {0} {1} {{ get; protected set; }}",
423-
CSharpTypePrinter.IntPtrType, Helpers.InstanceIdentifier);
423+
TypePrinter.IntPtrType, Helpers.InstanceIdentifier);
424424

425425
PopBlock(NewLineKind.BeforeNextBlock);
426426

@@ -942,7 +942,7 @@ private void GenerateFieldSetter(Field field, Class @class, QualifiedType fieldT
942942
Type pointee = type.GetFinalPointee();
943943
if (pointee.IsPrimitiveType())
944944
{
945-
Write($"({CSharpTypePrinter.IntPtrType}) ");
945+
Write($"({TypePrinter.IntPtrType}) ");
946946
var templateSubstitution = pointee.Desugar(false) as TemplateParameterSubstitutionType;
947947
if (templateSubstitution != null)
948948
Write("(object) ");
@@ -1137,7 +1137,7 @@ private void GenerateVariableGetter(Variable var)
11371137
else
11381138
ctx.ReturnVarName = $@"{elementType}.{
11391139
Helpers.CreateInstanceIdentifier}(new {
1140-
CSharpTypePrinter.IntPtrType}({ptr}))";
1140+
TypePrinter.IntPtrType}({ptr}))";
11411141

11421142
var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
11431143
var.QualifiedType.Visit(marshal);
@@ -1202,7 +1202,7 @@ private void GenerateFieldGetter(Field field, Class @class, QualifiedType return
12021202
Class typeClass;
12031203
if (field.Type.TryGetClass(out typeClass) && !typeClass.IsValueType &&
12041204
!ASTUtils.IsMappedToPrimitive(Context.TypeMaps, field.Type))
1205-
returnVar = $"new {CSharpTypePrinter.IntPtrType}(&{returnVar})";
1205+
returnVar = $"new {TypePrinter.IntPtrType}(&{returnVar})";
12061206
}
12071207

12081208
var ctx = new CSharpMarshalContext(Context, CurrentIndentation)

src/Generator/Generators/CSharp/CSharpTypePrinter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace CppSharp.Generators.CSharp
1313
{
1414
public class CSharpTypePrinter : TypePrinter
1515
{
16-
public const string IntPtrType = "global::System.IntPtr";
16+
public string IntPtrType => "global::System.IntPtr";
1717

1818
public BindingContext Context { get; set; }
1919

src/Generator/Types/Std/Stdlib.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,18 @@ public override Type CSharpSignatureType(TypePrinterContext ctx)
149149
return new CILType(typeof(string));
150150

151151
if (ctx.Parameter == null || ctx.Parameter.Name == Helpers.ReturnIdentifier)
152-
return new CustomType(CSharpTypePrinter.IntPtrType);
152+
{
153+
var typePrinter = new CSharpTypePrinter(Context);
154+
return new CustomType(typePrinter.IntPtrType);
155+
}
156+
153157
if (Context.Options.Encoding == Encoding.ASCII)
154158
return new CustomType("[MarshalAs(UnmanagedType.LPStr)] string");
159+
155160
if (Context.Options.Encoding == Encoding.Unicode ||
156161
Context.Options.Encoding == Encoding.BigEndianUnicode)
157162
return new CustomType("[MarshalAs(UnmanagedType.LPWStr)] string");
163+
158164
throw new System.NotSupportedException(
159165
$"{Context.Options.Encoding.EncodingName} is not supported yet.");
160166
}
@@ -287,7 +293,7 @@ public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
287293
{
288294
ctx.Return.Write($@"{qualifiedBasicString}Extensions.{
289295
Helpers.InternalStruct}.{assign.Name}(new {
290-
CSharpTypePrinter.IntPtrType}(&{
296+
typePrinter.IntPtrType}(&{
291297
ctx.ReturnVarName}), {ctx.Parameter.Name})");
292298
ctx.ReturnVarName = string.Empty;
293299
}

0 commit comments

Comments
 (0)