From f77dd1c13ca88430bfe8df364b6d2ed984191ac7 Mon Sep 17 00:00:00 2001 From: Pd Date: Thu, 2 Nov 2023 21:13:11 +0800 Subject: [PATCH] feat: Add predefined types --- Directory.Build.props | 2 +- src/Generation/ITypeReferenceProvider.cs | 2 +- src/Unmanaged/STL/StdString.cs | 17 ++++++++++++-- src/Unmanaged/STL/StdVector.cs | 30 ++++++++++++++++++------ 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 76c2ae7..c9c0491 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,5 +1,5 @@ - 1.0.3 + 1.0.4 diff --git a/src/Generation/ITypeReferenceProvider.cs b/src/Generation/ITypeReferenceProvider.cs index 76786a7..3dd5f71 100644 --- a/src/Generation/ITypeReferenceProvider.cs +++ b/src/Generation/ITypeReferenceProvider.cs @@ -6,5 +6,5 @@ public interface ITypeReferenceProvider { public static abstract Regex Regex { get; } - public static abstract Type Matched(Regex regex); + public static abstract Type? Matched(Match match); } diff --git a/src/Unmanaged/STL/StdString.cs b/src/Unmanaged/STL/StdString.cs index 3241a18..a8933aa 100644 --- a/src/Unmanaged/STL/StdString.cs +++ b/src/Unmanaged/STL/StdString.cs @@ -1,18 +1,23 @@ using System.Collections; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using Hosihikari.NativeInterop.Generation; using Hosihikari.NativeInterop.Layer; using Hosihikari.NativeInterop.Utils; +using static Hosihikari.NativeInterop.Generation.ITypeReferenceProvider; namespace Hosihikari.NativeInterop.Unmanaged.STL; -public unsafe class StdString : +[PredefinedType( + NativeTypeName = "basic_string, class std::allocator>", + NativeTypeNamespace = "std")] +public unsafe partial class StdString : IDisposable, ICppInstance, IMoveableCppInstance, ICopyableCppInstance, IEnumerable { - [StructLayout(LayoutKind.Explicit, Size = 32)] public readonly struct StdStringFiller : INativeTypeFiller { @@ -26,6 +31,14 @@ static StdStringFiller() public static void Destruct(StdStringFiller* @this) => DestructInstance(new(@this)); + public void Destruct() + { + fixed (StdStringFiller* ptr = &this) + { + Destruct(ptr); + } + } + public static implicit operator StdString(in StdStringFiller filler) { fixed (void* ptr = &filler) diff --git a/src/Unmanaged/STL/StdVector.cs b/src/Unmanaged/STL/StdVector.cs index 088a9cb..8d44139 100644 --- a/src/Unmanaged/STL/StdVector.cs +++ b/src/Unmanaged/STL/StdVector.cs @@ -1,12 +1,14 @@ using System.Runtime.CompilerServices; using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using Hosihikari.NativeInterop.Generation; using Hosihikari.NativeInterop.Layer; using size_t = System.UInt64; namespace Hosihikari.NativeInterop.Unmanaged.STL; [StructLayout(LayoutKind.Sequential)] -internal unsafe struct CxxVectorDesc +public unsafe struct CxxVectorDesc { public void* begin; @@ -16,17 +18,33 @@ internal unsafe struct CxxVectorDesc public void* end_cap; } -public unsafe class StdVector : +public struct Unknown { } + +public unsafe partial class StdVector : IDisposable, ICppInstance>, IMoveableCppInstance>, - ICopyableCppInstance> + ICopyableCppInstance>, + ITypeReferenceProvider where T : unmanaged { - [StructLayout(LayoutKind.Explicit, Size = 24)] - public readonly struct StdVectorFiller : INativeTypeFiller> +#if LINUX + internal static partial Regex StdVectorRegex() => throw new NotImplementedException(); +#else + [GeneratedRegex("^std::vector<(?.*), class std::allocator<(\\k)>>")] + internal static partial Regex StdVectorRegex(); +#endif + + public static Regex Regex => StdVectorRegex(); + + public static Type? Matched(Match match) => + typeof(StdVector.StdVectorFiller); + + public struct StdVectorFiller : INativeTypeFiller> { + public CxxVectorDesc cxxVector; + static StdVectorFiller() { if (sizeof(StdVectorFiller) != 24) @@ -35,8 +53,6 @@ static StdVectorFiller() } } - [FieldOffset(0)] - private readonly long _alignment_member; public static void Destruct(StdVectorFiller* @this) => DestructInstance(new(@this));