1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-08 19:48:20 +02:00

Added System.Interop.FlexibleArrayAttribute

This commit is contained in:
Brian Fiete 2022-08-28 06:49:35 -07:00
parent 0b162c0ebd
commit 9ba19e4cd7

View file

@ -70,6 +70,7 @@ namespace System
public ref T Value => ref *mVal;
public T* Ptr => mVal;
/// Initialize for creating
[AllowAppend]
public this(int count)
{
@ -79,6 +80,36 @@ namespace System
(void)data;
mCount = (.)count;
}
/// Initialize for reading
public this(T* ptr, int count)
{
mVal = ptr;
mCount = (.)count;
}
}
[AttributeUsage(.Struct)]
struct FlexibleArrayAttribute : Attribute, IOnTypeInit
{
String mName;
public this(String name)
{
mName = name;
}
[Comptime]
public void OnTypeInit(Type type, Self* prev)
{
if (type.IsGenericParam)
return;
var field = type.GetField(type.FieldCount - 1).GetValueOrDefault();
if ((field.FieldType == null) || (!field.FieldType.IsSizedArray) || (field.FieldType.Size != 0))
Runtime.FatalError("Type must end in a zero-sized array");
var elementType = (field.FieldType as SizedArrayType).UnderlyingType;
Compiler.EmitTypeBody(type, scope $"public {elementType}* {mName} mut => (({elementType}*)((uint8*)&this + Math.Align(typeof(Self).Size, typeof({elementType}).Align)));");
}
}
}
}