|
|
|
@ -10,6 +10,7 @@ namespace PkmnLibSharp.Utilities
|
|
|
|
|
public class ScopedOwner<T> : IDisposable where T : PointerWrapper
|
|
|
|
|
{
|
|
|
|
|
public T? Value { get; private set; }
|
|
|
|
|
private bool _isOwner = true;
|
|
|
|
|
|
|
|
|
|
public ScopedOwner(T value)
|
|
|
|
|
{
|
|
|
|
@ -18,19 +19,16 @@ namespace PkmnLibSharp.Utilities
|
|
|
|
|
|
|
|
|
|
public T? TakeOwnership()
|
|
|
|
|
{
|
|
|
|
|
var val = Value;
|
|
|
|
|
Value = null;
|
|
|
|
|
return val;
|
|
|
|
|
_isOwner = false;
|
|
|
|
|
return Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~ScopedOwner()
|
|
|
|
|
{
|
|
|
|
|
if (Value != null)
|
|
|
|
|
if (_isOwner && Value != null)
|
|
|
|
|
Value.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static implicit operator T?(ScopedOwner<T> val) => val.Value;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public override string ToString()
|
|
|
|
|
{
|
|
|
|
|
if (Value == null) return "null";
|
|
|
|
@ -39,7 +37,7 @@ namespace PkmnLibSharp.Utilities
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (Value != null)
|
|
|
|
|
if (_isOwner && Value != null)
|
|
|
|
|
Value.Dispose();
|
|
|
|
|
Value = null;
|
|
|
|
|
}
|
|
|
|
|