KerepWrapper project
This commit is contained in:
99
KerepWrapper/Autoarr/Autoarr.cs
Normal file
99
KerepWrapper/Autoarr/Autoarr.cs
Normal file
@@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
|
||||
public class Autoarr<T> : IEnumerable<T>, IDisposable where T : struct
|
||||
{
|
||||
public readonly IntPtr UnmanagedPtr;
|
||||
//if true, destructor frees allocated unmanaged memory
|
||||
public bool AutoDispose;
|
||||
private readonly AutoarrFunctions<T> Funcs;
|
||||
public readonly uint MaxLength;
|
||||
public uint Length { get; private set; }
|
||||
|
||||
public Autoarr(IntPtr ptr, bool autoDispose)
|
||||
{
|
||||
AutoDispose = autoDispose;
|
||||
Funcs = AutoarrFunctions<T>.GetFunctions();
|
||||
UnmanagedPtr = ptr;
|
||||
MaxLength = Funcs.MaxLength(UnmanagedPtr);
|
||||
Length = Funcs.Length(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public Autoarr(ushort blockCount, ushort blockLength, bool autoDispose=true)
|
||||
{
|
||||
AutoDispose = autoDispose;
|
||||
Funcs = AutoarrFunctions<T>.GetFunctions();
|
||||
UnmanagedPtr = Funcs.Create(blockCount, blockLength);
|
||||
MaxLength = Funcs.MaxLength(UnmanagedPtr);
|
||||
Length = Funcs.Length(UnmanagedPtr);
|
||||
}
|
||||
|
||||
|
||||
public T this[uint i]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (i < Length) return Funcs.Get(UnmanagedPtr, i);
|
||||
throw new IndexOutOfRangeException($"index {i} >= Autoarr.Length {Length}");
|
||||
}
|
||||
set
|
||||
{
|
||||
if (i < Length) Funcs.Set(UnmanagedPtr, i, value);
|
||||
else throw new IndexOutOfRangeException($"index {i} >= Autoarr.Length {Length}");
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if(AutoDispose)
|
||||
Funcs.Free(UnmanagedPtr);
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new AutoarrEnumerator(this);
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public void Add(T value)
|
||||
{
|
||||
if (Length < MaxLength)
|
||||
{
|
||||
Funcs.Add(UnmanagedPtr, value);
|
||||
Length++;
|
||||
}
|
||||
else throw new IndexOutOfRangeException($"Autoarr.Length == MaxLength ({MaxLength})");
|
||||
}
|
||||
|
||||
~Autoarr()
|
||||
{
|
||||
if (AutoDispose) Dispose();
|
||||
}
|
||||
|
||||
private class AutoarrEnumerator : IEnumerator<T>
|
||||
{
|
||||
private readonly Autoarr<T> arr;
|
||||
private uint index;
|
||||
|
||||
public AutoarrEnumerator(Autoarr<T> ar) => arr = ar;
|
||||
|
||||
public T Current { get; private set; }
|
||||
object IEnumerator.Current => Current;
|
||||
|
||||
public void Dispose() { }
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (index >= arr.Length) return false;
|
||||
Current = arr[index];
|
||||
index++;
|
||||
return true;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
38
KerepWrapper/Autoarr/AutoarrFunctions.cs
Normal file
38
KerepWrapper/Autoarr/AutoarrFunctions.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using DTLib;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
using AutoarrPtr=DtsodPtr;
|
||||
|
||||
public abstract class AutoarrFunctions<T>
|
||||
{
|
||||
static AutoarrFunctions()
|
||||
{
|
||||
DependencyResolver.CopyLibs();
|
||||
}
|
||||
|
||||
internal abstract AutoarrPtr Create(ushort maxBlocksCount, ushort maxBlockLength);
|
||||
internal abstract void Free(AutoarrPtr ar);
|
||||
internal abstract T Get(AutoarrPtr ar, uint index);
|
||||
internal abstract void Add(AutoarrPtr ar, T element);
|
||||
internal abstract void Set(AutoarrPtr ar, uint index, T element);
|
||||
internal abstract uint Length(AutoarrPtr ar);
|
||||
internal abstract uint MaxLength(AutoarrPtr ar);
|
||||
|
||||
private static AutoarrFunctions<Unitype> f_uni = new AutoarrUnitypeFunctions();
|
||||
private static AutoarrFunctions<KVPair> f_kvp = new AutoarrKVPairFunctions();
|
||||
static internal AutoarrFunctions<T> GetFunctions()
|
||||
{
|
||||
if (f_kvp is AutoarrFunctions<T> f)
|
||||
return f;
|
||||
else if (f_uni is AutoarrFunctions<T> ff)
|
||||
return ff;
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");
|
||||
/*if (typeof(T) == typeof(Unitype))
|
||||
return (AutoarrFunctions<T>)Convert.ChangeType(f_uni, typeof(AutoarrFunctions<T>));
|
||||
else if (typeof(T) == typeof(KVPair))
|
||||
return (AutoarrFunctions<T>) Convert.ChangeType(f_kvp, typeof(AutoarrFunctions<T>));
|
||||
else throw new Exception($"unsupported type: {typeof(T)}");*/
|
||||
}
|
||||
}
|
||||
52
KerepWrapper/Autoarr/AutoarrKVPairFunctions.cs
Normal file
52
KerepWrapper/Autoarr/AutoarrKVPairFunctions.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
|
||||
internal class AutoarrKVPairFunctions : AutoarrFunctions<KVPair>
|
||||
{
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_create(ushort max_blocks_count, ushort max_block_length, out AutoarrKVPairPtr output);
|
||||
internal override AutoarrKVPairPtr Create(ushort maxBlocksCount, ushort maxBlockLength)
|
||||
{
|
||||
kerep_Autoarr_KVPair_create(maxBlocksCount, maxBlockLength, out var ar);
|
||||
return ar;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_free(AutoarrKVPairPtr ar);
|
||||
internal override void Free(AutoarrKVPairPtr ar) => kerep_Autoarr_KVPair_free(ar);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_get(AutoarrKVPairPtr ar, uint index, out KVPair output);
|
||||
internal override KVPair Get(AutoarrKVPairPtr ar, uint index)
|
||||
{
|
||||
kerep_Autoarr_KVPair_get(ar, index, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep",CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_add(AutoarrKVPairPtr ar, KVPair element);
|
||||
internal override void Add(AutoarrKVPairPtr ar, KVPair element) => kerep_Autoarr_KVPair_add(ar, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_KVPair_set(AutoarrKVPairPtr ar, uint index, KVPair element);
|
||||
internal override void Set(AutoarrKVPairPtr ar, uint index, KVPair element) =>
|
||||
kerep_Autoarr_KVPair_set(ar, index, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_length(AutoarrKVPairPtr ar, out uint output);
|
||||
internal override uint Length(AutoarrKVPairPtr ar)
|
||||
{
|
||||
kerep_Autoarr_KVPair_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_KVPair_max_length(AutoarrKVPairPtr ar, out uint output);
|
||||
internal override uint MaxLength(AutoarrKVPairPtr ar)
|
||||
{
|
||||
kerep_Autoarr_KVPair_max_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
52
KerepWrapper/Autoarr/AutoarrUnitypeFunctions.cs
Normal file
52
KerepWrapper/Autoarr/AutoarrUnitypeFunctions.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using KerepWrapper.KerepTypes;
|
||||
|
||||
namespace KerepWrapper.Autoarr;
|
||||
|
||||
internal class AutoarrUnitypeFunctions : AutoarrFunctions<Unitype>
|
||||
{
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_create(ushort max_blocks_count, ushort max_block_length, out AutoarrUnitypePtr output);
|
||||
internal override AutoarrUnitypePtr Create(ushort maxBlocksCount, ushort maxBlockLength)
|
||||
{
|
||||
kerep_Autoarr_Unitype_create(maxBlocksCount, maxBlockLength, out var ar);
|
||||
return ar;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_free(AutoarrUnitypePtr ar);
|
||||
internal override void Free(AutoarrUnitypePtr ar) => kerep_Autoarr_Unitype_free(ar);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_get(AutoarrUnitypePtr ar, uint index, out Unitype output);
|
||||
internal override Unitype Get(AutoarrUnitypePtr ar, uint index)
|
||||
{
|
||||
kerep_Autoarr_Unitype_get(ar, index, out var output);
|
||||
return output;
|
||||
}
|
||||
|
||||
[DllImport("kerep",CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_add(AutoarrUnitypePtr ar, Unitype element);
|
||||
internal override void Add(AutoarrUnitypePtr ar, Unitype element) => kerep_Autoarr_Unitype_add(ar, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
internal static extern void kerep_Autoarr_Unitype_set(AutoarrUnitypePtr ar, uint index, Unitype element);
|
||||
internal override void Set(AutoarrUnitypePtr ar, uint index, Unitype element) =>
|
||||
kerep_Autoarr_Unitype_set(ar, index, element);
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_length(AutoarrUnitypePtr ar, out uint output);
|
||||
internal override uint Length(AutoarrUnitypePtr ar)
|
||||
{
|
||||
kerep_Autoarr_Unitype_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
|
||||
[DllImport("kerep", CallingConvention = CallingConvention.Cdecl)]
|
||||
static extern void kerep_Autoarr_Unitype_max_length(AutoarrUnitypePtr ar, out uint output);
|
||||
internal override uint MaxLength(AutoarrUnitypePtr ar)
|
||||
{
|
||||
kerep_Autoarr_Unitype_max_length(ar, out var l);
|
||||
return l;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user