Fixes for scopedowner.
This commit is contained in:
parent
7807ee9676
commit
d5bbc18b1a
|
@ -10,6 +10,7 @@ namespace PkmnLibSharp.Utilities
|
||||||
public class ScopedOwner<T> : IDisposable where T : PointerWrapper
|
public class ScopedOwner<T> : IDisposable where T : PointerWrapper
|
||||||
{
|
{
|
||||||
public T? Value { get; private set; }
|
public T? Value { get; private set; }
|
||||||
|
private bool _isOwner = true;
|
||||||
|
|
||||||
public ScopedOwner(T value)
|
public ScopedOwner(T value)
|
||||||
{
|
{
|
||||||
|
@ -18,19 +19,16 @@ namespace PkmnLibSharp.Utilities
|
||||||
|
|
||||||
public T? TakeOwnership()
|
public T? TakeOwnership()
|
||||||
{
|
{
|
||||||
var val = Value;
|
_isOwner = false;
|
||||||
Value = null;
|
return Value;
|
||||||
return val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~ScopedOwner()
|
~ScopedOwner()
|
||||||
{
|
{
|
||||||
if (Value != null)
|
if (_isOwner && Value != null)
|
||||||
Value.Dispose();
|
Value.Dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator T?(ScopedOwner<T> val) => val.Value;
|
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
if (Value == null) return "null";
|
if (Value == null) return "null";
|
||||||
|
@ -39,7 +37,7 @@ namespace PkmnLibSharp.Utilities
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
if (Value != null)
|
if (_isOwner && Value != null)
|
||||||
Value.Dispose();
|
Value.Dispose();
|
||||||
Value = null;
|
Value = null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,7 +30,7 @@ namespace PkmnLibSharpTests.Battling.BattleTests
|
||||||
party.SwapInto(0,
|
party.SwapInto(0,
|
||||||
new PokemonBuilder(lib, "testSpecies", 50)
|
new PokemonBuilder(lib, "testSpecies", 50)
|
||||||
.LearnMove("testMove", MoveLearnMethod.Unknown)
|
.LearnMove("testMove", MoveLearnMethod.Unknown)
|
||||||
.Build());
|
.Build().TakeOwnership());
|
||||||
return party;
|
return party;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue