Skip to content

Instantly share code, notes, and snippets.

@copygirl
Created September 4, 2022 20:51
Show Gist options
  • Save copygirl/3f1ec14ab70d3774cc80fc10c753804d to your computer and use it in GitHub Desktop.
Save copygirl/3f1ec14ab70d3774cc80fc10c753804d to your computer and use it in GitHub Desktop.
public readonly unsafe struct Query : IEnumerable<Iterator>
{
private readonly World _world = null!;
private readonly ecs_query_t* _handle = null;
public Query(World world, string expr)
{
ecs_query_desc_t desc = default;
desc.filter.expr = expr;
_world = world;
_handle = ecs_query_init(world.Handle, &desc);
}
public void Fini()
=> ecs_query_fini(_handle);
public IEnumerator<Iterator> GetEnumerator() => new QueryIterator(this);
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
private struct QueryIterator : IEnumerator<Iterator>
{
private readonly ecs_iter_t _iter;
public QueryIterator(Query query)
=> _iter = ecs_query_iter(query._world.Handle, query._handle);
public bool MoveNext()
{
fixed (QueryIterator* @this = &this)
return ecs_query_next(&@this->_iter);
}
object IEnumerator.Current => Current;
public Iterator Current { get {
fixed (QueryIterator* @this = &this)
return new(World.Pointers[(IntPtr)@this->_iter.world], &@this->_iter);
} }
public void Reset() => throw new NotSupportedException();
public void Dispose() { }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment