Improved Iterator cleanup?

wip/source-generators
copygirl 2 years ago
parent 66724e4100
commit 129f75649c
  1. 20
      src/gaemstone/ECS/Iterator.cs

@ -7,13 +7,15 @@ using static flecs_hub.flecs;
namespace gaemstone.ECS;
public unsafe partial class Iterator
public unsafe class Iterator
: IEnumerable<Iterator>
, IDisposable
{
public Universe Universe { get; }
public IteratorType? Type { get; }
public ecs_iter_t Value;
public bool Completed { get; private set; }
public int Count => Value.count;
public TimeSpan DeltaTime => TimeSpan.FromSeconds(Value.delta_time);
public TimeSpan DeltaSystemTime => TimeSpan.FromSeconds(Value.delta_system_time);
@ -29,6 +31,15 @@ public unsafe partial class Iterator
return new(universe, IteratorType.Term, flecsIter);
}
public void Dispose()
{
GC.SuppressFinalize(this);
// NOTE: When an iterator is iterated until completion, resources are automatically freed.
if (!Completed)
fixed (ecs_iter_t* ptr = &Value)
ecs_iter_fini(ptr);
}
public void SetThis(Entity entity)
{
fixed (ecs_iter_t* ptr = &Value)
@ -37,14 +48,17 @@ public unsafe partial class Iterator
public bool Next()
{
fixed (ecs_iter_t* ptr = &Value)
return Type switch {
fixed (ecs_iter_t* ptr = &Value) {
var result = Type switch {
IteratorType.Term => ecs_term_next(ptr),
IteratorType.Filter => ecs_filter_next(ptr),
IteratorType.Query => ecs_query_next(ptr),
IteratorType.Rule => ecs_rule_next(ptr),
_ => ecs_iter_next(ptr),
};
Completed = !result;
return result;
}
}
public EntityRef Entity(int index)

Loading…
Cancel
Save