Added unique

This commit is contained in:
2026-05-15 21:43:28 +01:00
parent 102b4fb7bf
commit 5fba35a0a2
4 changed files with 102 additions and 1 deletions

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: d9b64dde6eaaefc498098e1b5641cb52
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,82 @@
using Unity.Collections.LowLevel.Unsafe;
using System.Runtime.CompilerServices;
using System.Diagnostics;
using Unity.Collections;
using System;
namespace PashaBibko.Pacore.MemUtil
{
public unsafe struct Unique<T> : IDisposable
where T : unmanaged
{
private T* mPtr;
public bool IsValid
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => mPtr != null;
}
public T Value
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
DebugOnly_CheckValid();
return *mPtr;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
DebugOnly_CheckValid();
*mPtr = value;
}
}
public ref T Ref
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
DebugOnly_CheckValid();
return ref *mPtr;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Unique<T> Create(T val = default)
{
T* ptr = (T*)UnsafeUtility.Malloc
(
sizeof(T),
UnsafeUtility.SizeOf<T>(),
Allocator.Persistent
);
*ptr = val;
return new Unique<T> { mPtr = ptr };
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose()
{
if (IsValid)
{
UnsafeUtility.Free(mPtr, Allocator.Persistent);
}
}
[Conditional("UNITY_EDITOR")]
private void DebugOnly_CheckValid()
{
if (!IsValid)
{
throw new InvalidOperationException
(
$"Owned<{nameof(T)}> was expected to be valid."
);
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f4ac012a56e2aa9429ff4f33c5554e7d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -4,7 +4,7 @@
"references": [], "references": [],
"includePlatforms": [], "includePlatforms": [],
"excludePlatforms": [], "excludePlatforms": [],
"allowUnsafeCode": false, "allowUnsafeCode": true,
"overrideReferences": false, "overrideReferences": false,
"precompiledReferences": [], "precompiledReferences": [],
"autoReferenced": true, "autoReferenced": true,