Skip to content

Commit

Permalink
Add IIterator interface and refactor iterable apis
Browse files Browse the repository at this point in the history
  • Loading branch information
BeanCheeseBurrito committed Jul 31, 2024
1 parent e80eed6 commit 4404cff
Show file tree
Hide file tree
Showing 5 changed files with 339 additions and 356 deletions.
119 changes: 119 additions & 0 deletions src/Flecs.NET/Core/IIterator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using System.Diagnostics.CodeAnalysis;

using static Flecs.NET.Bindings.flecs;

namespace Flecs.NET.Core
{
/// <summary>
/// Interface for iterator structs.
/// </summary>
[SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")]
public unsafe interface IIterator : IIterable
{
/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Iter(Ecs.IterCallback callback);

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(Ecs.EachEntityCallback callback);

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(Ecs.EachIterCallback callback);

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Run(Ecs.RunCallback callback);

#if NET5_0_OR_GREATER
/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Iter(delegate*<Iter, void> callback);

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(delegate*<Entity, void> callback);

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(delegate*<Iter, int, void> callback);

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Run(delegate*<Iter, void> callback);
#endif

internal static void Iter<T>(ref T iterable, Ecs.IterCallback callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
while (iterable.GetNext(&iter))
Invoker.Iter(&iter, callback);
}

internal static void Each<T>(ref T iterable, Ecs.EachEntityCallback callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
while (iterable.GetNext(&iter))
Invoker.Each(&iter, callback);
}

internal static void Each<T>(ref T iterable, Ecs.EachIterCallback callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
while (iterable.GetNext(&iter))
Invoker.Each(&iter, callback);
}

internal static void Run<T>(ref T iterable, Ecs.RunCallback callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
Invoker.Run(&iter, callback);
}

#if NET5_0_OR_GREATER
internal static void Iter<T>(ref T iterable, delegate*<Iter, void> callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
while (iterable.GetNext(&iter))
Invoker.Iter(&iter, callback);
}

internal static void Each<T>(ref T iterable, delegate*<Entity, void> callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
while (iterable.GetNext(&iter))
Invoker.Each(&iter, callback);
}

internal static void Each<T>(ref T iterable, delegate*<Iter, int, void> callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
while (iterable.GetNext(&iter))
Invoker.Each(&iter, callback);
}

internal static void Run<T>(ref T iterable, delegate*<Iter, void> callback) where T : unmanaged, IIterable
{
ecs_iter_t iter = iterable.GetIter();
Invoker.Run(&iter, callback);
}
#endif
}
}
144 changes: 55 additions & 89 deletions src/Flecs.NET/Core/IterIterable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Flecs.NET.Core
/// <summary>
/// An iterator object that can be modified before iterating.
/// </summary>
public unsafe partial struct IterIterable : IIterable, IEquatable<IterIterable>
public unsafe partial struct IterIterable : IIterator, IEquatable<IterIterable>
{
private ecs_iter_t _iter;
private IterableType _iterableType;
Expand Down Expand Up @@ -247,94 +247,6 @@ public ref IterIterable SetGroup<T>()
}
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Iter(Ecs.IterCallback callback)
{
ecs_iter_t iter = GetIter();
while (GetNext(&iter))
Invoker.Iter(&iter, callback);
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(Ecs.EachEntityCallback callback)
{
ecs_iter_t iter = GetIter();
while (GetNext(&iter))
Invoker.Each(&iter, callback);
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(Ecs.EachIterCallback callback)
{
ecs_iter_t iter = GetIter();
while (GetNext(&iter))
Invoker.Each(&iter, callback);
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Run(Ecs.RunCallback callback)
{
ecs_iter_t iter = GetIter();
Invoker.Run(&iter, callback);
}

#if NET5_0_OR_GREATER
/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Iter(delegate*<Iter, void> callback)
{
ecs_iter_t iter = GetIter();
while (GetNext(&iter))
Invoker.Iter(&iter, callback);
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(delegate*<Entity, void> callback)
{
ecs_iter_t iter = GetIter();
while (GetNext(&iter))
Invoker.Each(&iter, callback);
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Each(delegate*<Iter, int, void> callback)
{
ecs_iter_t iter = GetIter();
while (GetNext(&iter))
Invoker.Each(&iter, callback);
}

/// <summary>
/// Iterates the query using the provided callback.
/// </summary>
/// <param name="callback">The callback.</param>
public void Run(delegate*<Iter, void> callback)
{
ecs_iter_t iter = GetIter();
Invoker.Run(&iter, callback);
}
#endif

/// <summary>
/// Checks if two <see cref="IterIterable"/> instances are equal.
/// </summary>
Expand Down Expand Up @@ -505,4 +417,58 @@ IterIterable IIterable.SetGroup<T>()
return Iter().SetGroup<T>();
}
}

// IIterator Interface
public unsafe partial struct IterIterable
{
/// <inheritdoc cref="IIterator.Iter(Ecs.IterCallback)"/>
public void Iter(Ecs.IterCallback callback)
{
IIterator.Iter(ref this, callback);
}

/// <inheritdoc cref="IIterator.Each(Ecs.EachEntityCallback)"/>
public void Each(Ecs.EachEntityCallback callback)
{
IIterator.Each(ref this, callback);
}

/// <inheritdoc cref="IIterator.Each(Ecs.EachIterCallback)"/>
public void Each(Ecs.EachIterCallback callback)
{
IIterator.Each(ref this, callback);
}

/// <inheritdoc cref="IIterator.Run(Ecs.RunCallback)"/>
public void Run(Ecs.RunCallback callback)
{
IIterator.Run(ref this, callback);
}

#if NET5_0_OR_GREATER
/// <inheritdoc cref="IIterator.Iter(Ecs.IterCallback)"/>
public void Iter(delegate*<Iter, void> callback)
{
IIterator.Iter(ref this, callback);
}

/// <inheritdoc cref="IIterator.Each(Ecs.EachEntityCallback)"/>
public void Each(delegate*<Entity, void> callback)
{
IIterator.Each(ref this, callback);
}

/// <inheritdoc cref="IIterator.Each(Ecs.EachIterCallback)"/>
public void Each(delegate*<Iter, int, void> callback)
{
IIterator.Each(ref this, callback);
}

/// <inheritdoc cref="IIterator.Run(Ecs.RunCallback)"/>
public void Run(delegate*<Iter, void> callback)
{
IIterator.Run(ref this, callback);
}
#endif
}
}
Loading

0 comments on commit 4404cff

Please sign in to comment.