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 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;
|
||||
}
|
||||
|
|
|
@ -30,7 +30,7 @@ namespace PkmnLibSharpTests.Battling.BattleTests
|
|||
party.SwapInto(0,
|
||||
new PokemonBuilder(lib, "testSpecies", 50)
|
||||
.LearnMove("testMove", MoveLearnMethod.Unknown)
|
||||
.Build());
|
||||
.Build().TakeOwnership());
|
||||
return party;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue