Fixes for scopedowner.

This commit is contained in:
Deukhoofd 2021-01-16 14:48:07 +01:00
parent 7807ee9676
commit d5bbc18b1a
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
2 changed files with 7 additions and 9 deletions

View File

@ -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;
}

View File

@ -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;
}