Browse Source

Extended the type maps for primitive strings to C++/CLI.

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1158/head
Dimitar Dobrev 6 years ago committed by João Matos
parent
commit
f4673f5d7f
  1. 51
      src/Generator/Generators/CLI/CLIMarshal.cs
  2. 16
      src/Generator/Generators/CLI/CLITypePrinter.cs
  3. 57
      src/Generator/Types/Std/Stdlib.cs

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

@ -60,10 +60,11 @@ namespace CppSharp.Generators.CLI @@ -60,10 +60,11 @@ namespace CppSharp.Generators.CLI
// const char* and const char[] are the same so we can use a string
if (array.Type.Desugar().IsPrimitiveType(PrimitiveType.Char) &&
array.QualifiedType.Qualifiers.IsConst)
return VisitPointerType(new PointerType
{
QualifiedPointee = array.QualifiedType
}, quals);
{
var pointer = new PointerType { QualifiedPointee = array.QualifiedType };
Context.ReturnType = new QualifiedType(pointer);
return this.VisitPointerType(pointer, quals);
}
goto case ArrayType.ArraySize.Variable;
case ArrayType.ArraySize.Variable:
@ -100,13 +101,6 @@ namespace CppSharp.Generators.CLI @@ -100,13 +101,6 @@ namespace CppSharp.Generators.CLI
return true;
}
if (pointer.IsConstCharString())
{
Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName,
pointer.Pointee.Desugar() as BuiltinType));
return true;
}
PrimitiveType primitive;
var param = Context.Parameter;
if (param != null && (param.IsOut || param.IsInOut) &&
@ -167,29 +161,6 @@ namespace CppSharp.Generators.CLI @@ -167,29 +161,6 @@ namespace CppSharp.Generators.CLI
return pointer.QualifiedPointee.Visit(this);
}
private string MarshalStringToManaged(string varName, BuiltinType type)
{
var encoding = type.Type == PrimitiveType.Char ?
Encoding.ASCII : Encoding.Unicode;
if (Equals(encoding, Encoding.ASCII))
encoding = Context.Context.Options.Encoding;
string param;
if (Equals(encoding, Encoding.ASCII))
param = "E_UTF8";
else if (Equals(encoding, Encoding.Unicode) ||
Equals(encoding, Encoding.BigEndianUnicode))
param = "E_UTF16";
else
throw new NotSupportedException(string.Format("{0} is not supported yet.",
Context.Context.Options.Encoding.EncodingName));
return string.Format(
"({0} == 0 ? nullptr : clix::marshalString<clix::{1}>({0}))",
varName, param);
}
public override bool VisitMemberPointerType(MemberPointerType member,
TypeQualifiers quals)
{
@ -513,18 +484,6 @@ namespace CppSharp.Generators.CLI @@ -513,18 +484,6 @@ namespace CppSharp.Generators.CLI
var pointee = pointer.Pointee.Desugar();
if ((pointee.IsPrimitiveType(PrimitiveType.Char) ||
pointee.IsPrimitiveType(PrimitiveType.WideChar)) &&
pointer.QualifiedPointee.Qualifiers.IsConst)
{
Context.Before.WriteLine(
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
Context.ArgName, Context.Parameter.Name);
Context.Return.Write("_{0}.c_str()", Context.ArgName);
return true;
}
if (pointee is FunctionType)
{
var cppTypePrinter = new CppTypePrinter();

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

@ -107,6 +107,19 @@ namespace CppSharp.Generators.CLI @@ -107,6 +107,19 @@ namespace CppSharp.Generators.CLI
public override TypePrinterResult VisitPointerType(PointerType pointer,
TypeQualifiers quals)
{
TypeMap typeMap;
if (Context.TypeMaps.FindTypeMap(pointer.Desugar(), out typeMap))
{
var typePrinterContext = new TypePrinterContext
{
Kind = ContextKind,
MarshalKind = MarshalKind,
Type = pointer
};
return typeMap.CLISignatureType(typePrinterContext).Visit(this);
}
var pointee = pointer.Pointee.Desugar();
if (pointee is FunctionType)
@ -115,9 +128,6 @@ namespace CppSharp.Generators.CLI @@ -115,9 +128,6 @@ namespace CppSharp.Generators.CLI
return string.Format("{0}^", function.Visit(this, quals));
}
if (pointer.IsConstCharString())
return "System::String^";
// From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
// Any of the following types may be a pointer type:
// * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.

57
src/Generator/Types/Std/Stdlib.cs

@ -90,9 +90,58 @@ namespace CppSharp.Types.Std @@ -90,9 +90,58 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("const char*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char*")]
public class ConstCharPointer : TypeMap
{
public override Type CLISignatureType(TypePrinterContext ctx)
{
return new CILType(typeof(string));
}
public override void CLIMarshalToNative(MarshalContext ctx)
{
ctx.Before.WriteLine(
"auto _{0} = clix::marshalString<clix::E_UTF8>({1});",
ctx.ArgName, ctx.Parameter.Name);
ctx.Return.Write("_{0}.c_str()", ctx.ArgName);
}
public override void CLIMarshalToManaged(MarshalContext ctx)
{
if (ctx.Parameter != null && !ctx.Parameter.IsOut &&
!ctx.Parameter.IsInOut)
{
ctx.Return.Write(ctx.Parameter.Name);
return;
}
Type type = ctx.ReturnType.Type.Desugar();
Type pointee = type.GetPointee().Desugar();
var isChar = type.IsPointerToPrimitiveType(PrimitiveType.Char) ||
(pointee.IsPointerToPrimitiveType(PrimitiveType.Char) &&
ctx.Parameter != null &&
(ctx.Parameter.IsInOut || ctx.Parameter.IsOut));
var encoding = isChar ? Encoding.ASCII : Encoding.Unicode;
if (Equals(encoding, Encoding.ASCII))
encoding = Context.Options.Encoding;
string param;
if (Equals(encoding, Encoding.ASCII))
param = "E_UTF8";
else if (Equals(encoding, Encoding.Unicode) ||
Equals(encoding, Encoding.BigEndianUnicode))
param = "E_UTF16";
else
throw new System.NotSupportedException(
$"{Context.Options.Encoding.EncodingName} is not supported yet.");
ctx.Return.Write(
$@"({ctx.ReturnVarName} == 0 ? nullptr : clix::marshalString<clix::{
param}>({ctx.ReturnVarName}))");
}
public override Type CSharpSignatureType(TypePrinterContext ctx)
{
if (ctx.Kind == TypePrinterContextKind.Managed)
@ -178,17 +227,17 @@ namespace CppSharp.Types.Std @@ -178,17 +227,17 @@ namespace CppSharp.Types.Std
}
}
[TypeMap("const char[]", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char[]")]
public class ConstCharArray : ConstCharPointer
{
}
[TypeMap("const wchar_t*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const wchar_t*")]
public class ConstWCharTPointer : ConstCharPointer
{
}
[TypeMap("const char16_t*", GeneratorKind = GeneratorKind.CSharp)]
[TypeMap("const char16_t*")]
public class ConstChar16TPointer : ConstCharPointer
{
}

Loading…
Cancel
Save