complete World.Dispose

pull/6/head
cosmonaut 2023-11-20 15:11:17 -08:00
parent 6136450617
commit b27875b9a8
3 changed files with 85 additions and 6 deletions

View File

@ -1,8 +1,8 @@
using MoonTools.ECS.Collections; using System;
using MoonTools.ECS.Collections;
namespace MoonTools.ECS; namespace MoonTools.ECS;
// TODO: do we want to get fancy with queries beyond Include and Exclude?
public class Filter public class Filter
{ {
private World World; private World World;
@ -10,6 +10,8 @@ public class Filter
internal IndexableSet<Entity> EntitySet = new IndexableSet<Entity>(); internal IndexableSet<Entity> EntitySet = new IndexableSet<Entity>();
private bool IsDisposed;
public ReverseSpanEnumerator<Entity> Entities => EntitySet.GetEnumerator(); public ReverseSpanEnumerator<Entity> Entities => EntitySet.GetEnumerator();
public bool Empty => EntitySet.Count == 0; public bool Empty => EntitySet.Count == 0;
@ -91,4 +93,31 @@ public class Filter
public bool MoveNext() => LinearCongruentialEnumerator.MoveNext(); public bool MoveNext() => LinearCongruentialEnumerator.MoveNext();
public Entity Current => Filter.NthEntity(LinearCongruentialEnumerator.Current); public Entity Current => Filter.NthEntity(LinearCongruentialEnumerator.Current);
} }
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
EntitySet.Dispose();
}
IsDisposed = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~Filter()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
} }

View File

@ -1,12 +1,15 @@
using System;
using MoonTools.ECS.Collections; using MoonTools.ECS.Collections;
namespace MoonTools.ECS; namespace MoonTools.ECS;
internal class IdAssigner internal class IdAssigner : IDisposable
{ {
uint Next; uint Next;
NativeArray<uint> AvailableIds = new NativeArray<uint>(); NativeArray<uint> AvailableIds = new NativeArray<uint>();
private bool IsDisposed;
public uint Assign() public uint Assign()
{ {
if (AvailableIds.TryPop(out var id)) if (AvailableIds.TryPop(out var id))
@ -29,4 +32,31 @@ internal class IdAssigner
AvailableIds.CopyTo(other.AvailableIds); AvailableIds.CopyTo(other.AvailableIds);
other.Next = Next; other.Next = Next;
} }
protected virtual void Dispose(bool disposing)
{
if (!IsDisposed)
{
if (disposing)
{
AvailableIds.Dispose();
}
IsDisposed = true;
}
}
// // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
// ~IdAssigner()
// {
// // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
// Dispose(disposing: false);
// }
public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
} }

View File

@ -466,7 +466,6 @@ namespace MoonTools.ECS
{ {
if (disposing) if (disposing)
{ {
// TODO: dispose managed state (managed objects)
foreach (var componentStorage in ComponentIndex.Values) foreach (var componentStorage in ComponentIndex.Values)
{ {
componentStorage.Dispose(); componentStorage.Dispose();
@ -476,10 +475,31 @@ namespace MoonTools.ECS
{ {
relationStorage.Dispose(); relationStorage.Dispose();
} }
foreach (var messageStorage in MessageIndex.Values)
{
messageStorage.Dispose();
}
foreach (var typeSet in EntityComponentIndex.Values)
{
typeSet.Dispose();
}
foreach (var typeSet in EntityRelationIndex.Values)
{
typeSet.Dispose();
}
foreach (var filter in FilterIndex.Values)
{
filter.Dispose();
}
EntityIdAssigner.Dispose();
TypeIdAssigner.Dispose();
} }
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
IsDisposed = true; IsDisposed = true;
} }
} }