Skip to content

Commit

Permalink
feat: Add predefined types
Browse files Browse the repository at this point in the history
  • Loading branch information
Pd233 committed Nov 2, 2023
1 parent 04aab72 commit f77dd1c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Generation/ITypeReferenceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
17 changes: 15 additions & 2 deletions src/Unmanaged/STL/StdString.cs
Original file line number Diff line number Diff line change
@@ -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<char, struct std::char_traits<char>, class std::allocator<char>>",
NativeTypeNamespace = "std")]
public unsafe partial class StdString :
IDisposable,
ICppInstance<StdString>,
IMoveableCppInstance<StdString>,
ICopyableCppInstance<StdString>,
IEnumerable<byte>
{

[StructLayout(LayoutKind.Explicit, Size = 32)]
public readonly struct StdStringFiller : INativeTypeFiller<StdStringFiller, StdString>
{
Expand All @@ -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)
Expand Down
30 changes: 23 additions & 7 deletions src/Unmanaged/STL/StdVector.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,17 +18,33 @@ internal unsafe struct CxxVectorDesc
public void* end_cap;
}

public unsafe class StdVector<T> :
public struct Unknown { }

public unsafe partial class StdVector<T> :
IDisposable,
ICppInstance<StdVector<T>>,
IMoveableCppInstance<StdVector<T>>,
ICopyableCppInstance<StdVector<T>>
ICopyableCppInstance<StdVector<T>>,
ITypeReferenceProvider
where T : unmanaged
{

[StructLayout(LayoutKind.Explicit, Size = 24)]
public readonly struct StdVectorFiller : INativeTypeFiller<StdVectorFiller, StdVector<T>>
#if LINUX
internal static partial Regex StdVectorRegex() => throw new NotImplementedException();
#else
[GeneratedRegex("^std::vector<(?<class_type>.*), class std::allocator<(\\k<class_type>)>>")]
internal static partial Regex StdVectorRegex();
#endif

public static Regex Regex => StdVectorRegex();

public static Type? Matched(Match match) =>
typeof(StdVector<Unknown>.StdVectorFiller);

public struct StdVectorFiller : INativeTypeFiller<StdVectorFiller, StdVector<T>>
{
public CxxVectorDesc cxxVector;

static StdVectorFiller()
{
if (sizeof(StdVectorFiller) != 24)
Expand All @@ -35,8 +53,6 @@ static StdVectorFiller()
}
}

[FieldOffset(0)]
private readonly long _alignment_member;

public static void Destruct(StdVectorFiller* @this) => DestructInstance(new(@this));

Expand Down

0 comments on commit f77dd1c

Please sign in to comment.