From 2b7e8b9b90e19805b3113a66f3fa5d44605b911a Mon Sep 17 00:00:00 2001 From: triton Date: Wed, 22 May 2013 16:35:03 +0100 Subject: [PATCH] Improved type maps for C++ string and vector standard library support. --- src/Generator/Types/Std/Stdlib.cs | 23 +++++++---- src/Runtime/StdVector.cs | 68 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 8 deletions(-) create mode 100644 src/Runtime/StdVector.cs diff --git a/src/Generator/Types/Std/Stdlib.cs b/src/Generator/Types/Std/Stdlib.cs index 81cb0d96..a9382d59 100644 --- a/src/Generator/Types/Std/Stdlib.cs +++ b/src/Generator/Types/Std/Stdlib.cs @@ -34,19 +34,17 @@ namespace CppSharp.Types.Std public override string CSharpSignature(CSharpTypePrinterContext ctx) { - if (ctx.CSharpKind == CSharpTypePrinterContextKind.ManagedPointer) - return "System.IntPtr"; return "Std.String"; } public override void CSharpMarshalToNative(MarshalContext ctx) { - ctx.Return.Write("new Std.String().Instance"); + ctx.Return.Write("new Std.String()"); } public override void CSharpMarshalToManaged(MarshalContext ctx) { - ctx.Return.Write("new Std.String({0})", ctx.ReturnVarName); + ctx.Return.Write(ctx.ReturnVarName); } } @@ -174,18 +172,27 @@ namespace CppSharp.Types.Std public override string CSharpSignature(CSharpTypePrinterContext ctx) { - return string.Format("System.Collections.Generic.List<{0}>", - ctx.GetTemplateParameterList()); + if (ctx.CSharpKind == CSharpTypePrinterContextKind.Native) + return "Std.Vector"; + + return string.Format("Std.Vector<{0}>", ctx.GetTemplateParameterList()); } public override void CSharpMarshalToNative(MarshalContext ctx) { - ctx.Return.Write("null"); + var templateType = Type as TemplateSpecializationType; + var type = templateType.Arguments[0].Type; + + ctx.Return.Write("Std.Vector.Create({0})", ctx.Parameter.Name); } public override void CSharpMarshalToManaged(MarshalContext ctx) { - ctx.Return.Write("null"); + var templateType = Type as TemplateSpecializationType; + var type = templateType.Arguments[0].Type; + + ctx.Return.Write("Std.Vector.Create<{0}>({1})", type, + ctx.ReturnVarName); } } diff --git a/src/Runtime/StdVector.cs b/src/Runtime/StdVector.cs new file mode 100644 index 00000000..60373b6f --- /dev/null +++ b/src/Runtime/StdVector.cs @@ -0,0 +1,68 @@ +/* Copyright (c) 2013 Xamarin, Inc and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ + +using System; +using System.Collections; +using System.Collections.Generic; + +namespace Std +{ + public struct Vector : ICollection + { + public IEnumerator GetEnumerator() + { + throw new NotImplementedException(); + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + public void Add(T item) + { + throw new NotImplementedException(); + } + + public void Clear() + { + throw new NotImplementedException(); + } + + public bool Contains(T item) + { + throw new NotImplementedException(); + } + + public void CopyTo(T[] array, int arrayIndex) + { + throw new NotImplementedException(); + } + + public bool Remove(T item) + { + throw new NotImplementedException(); + } + + public int Count { get; private set; } + public bool IsReadOnly { get; private set; } + } +}