1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-10 12:32:20 +02:00

Added System.Interop.FlexibleArary<>

This commit is contained in:
Brian Fiete 2022-08-27 09:24:46 -07:00
parent 8c15c3c627
commit c1266d2422
2 changed files with 53 additions and 2 deletions

View file

@ -522,7 +522,7 @@ namespace System
}
[AttributeUsage(.Method | .Constructor | .Class | .Struct | .Alias | .Property)]
[AttributeUsage(.Method | .Constructor | .Class | .Struct | .Alias | .Property | .Field)]
public struct ErrorAttribute : Attribute
{
public this(String error)
@ -531,7 +531,7 @@ namespace System
}
}
[AttributeUsage(.Method | .Constructor | .Class | .Struct | .Alias | .Property)]
[AttributeUsage(.Method | .Constructor | .Class | .Struct | .Alias | .Property | .Field)]
public struct WarnAttribute : Attribute
{
public this(String error)

View file

@ -1,3 +1,5 @@
using System.Reflection;
namespace System
{
namespace Interop
@ -29,5 +31,54 @@ namespace System
typealias c_ulong = uint64;
#endif
class FlexibleArary<T> where T : struct, new
{
typealias ElementType = comptype(GetElementType());
int32 mCount;
T* mVal;
static Type GetElementType()
{
var t = typeof(T);
if (t.IsGenericParam)
return typeof(void);
var field = t.GetField(t.FieldCount - 1).GetValueOrDefault();
if ((field.FieldType == null) || (!field.FieldType.IsSizedArray) || (field.FieldType.Size != 0))
Runtime.FatalError("Type must end in a zero-sized array");
return (field.FieldType as SizedArrayType).UnderlyingType;
}
public ref ElementType this[int index]
{
[Checked]
get
{
Runtime.Assert((uint)index < (uint)mCount);
return ref ((ElementType*)((uint8*)mVal + Math.Align(typeof(T).Size, typeof(ElementType).Align)))[index];
}
[Unchecked]
get
{
return ref ((ElementType*)((uint8*)mVal + Math.Align(typeof(T).Size, typeof(ElementType).Align)))[index];
}
}
public static ref T operator ->(Self self) => ref *self.mVal;
public ref T Value => ref *mVal;
public T* Ptr => mVal;
[AllowAppend]
public this(int count)
{
var val = append T();
var data = append ElementType[count]*;
mVal = val;
(void)data;
mCount = (.)count;
}
}
}
}