From 65e51cadcd487e73956b97d8651166a3f1e1ee9a Mon Sep 17 00:00:00 2001 From: BeanCheeseBurrito Date: Wed, 31 Jul 2024 22:12:03 -0700 Subject: [PATCH] Move iterable iterators to Invoker class --- src/Flecs.NET/Core/IIterator.cs | 62 +-------------- src/Flecs.NET/Core/IQueryBuilder.cs | 4 + src/Flecs.NET/Core/Invoker.cs | 109 +++++++++++++++++++++++++++ src/Flecs.NET/Core/IterIterable.cs | 16 ++-- src/Flecs.NET/Core/PageIterable.cs | 16 ++-- src/Flecs.NET/Core/Query.cs | 19 +++-- src/Flecs.NET/Core/WorkerIterable.cs | 16 ++-- 7 files changed, 147 insertions(+), 95 deletions(-) diff --git a/src/Flecs.NET/Core/IIterator.cs b/src/Flecs.NET/Core/IIterator.cs index 7b19714c..fbce0da1 100644 --- a/src/Flecs.NET/Core/IIterator.cs +++ b/src/Flecs.NET/Core/IIterator.cs @@ -8,7 +8,7 @@ namespace Flecs.NET.Core /// Interface for iterator structs. /// [SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")] - public unsafe partial interface IIterator : IIterable + public unsafe interface IIterator : IIterable { /// /// Iterates the query using the provided callback. @@ -58,66 +58,6 @@ public unsafe partial interface IIterator : IIterable /// /// The callback. public void Run(delegate* callback); -#endif - } - - // Iterators - public unsafe partial interface IIterator - { - internal static void Iter(ref T iterable, Ecs.IterCallback callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - while (iterable.GetNext(&iter)) - Invoker.Iter(&iter, callback); - } - - internal static void Each(ref T iterable, Ecs.EachEntityCallback callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - while (iterable.GetNext(&iter)) - Invoker.Each(&iter, callback); - } - - internal static void Each(ref T iterable, Ecs.EachIterCallback callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - while (iterable.GetNext(&iter)) - Invoker.Each(&iter, callback); - } - - internal static void Run(ref T iterable, Ecs.RunCallback callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - Invoker.Run(&iter, callback); - } - -#if NET5_0_OR_GREATER - internal static void Iter(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - while (iterable.GetNext(&iter)) - Invoker.Iter(&iter, callback); - } - - internal static void Each(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - while (iterable.GetNext(&iter)) - Invoker.Each(&iter, callback); - } - - internal static void Each(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - while (iterable.GetNext(&iter)) - Invoker.Each(&iter, callback); - } - - internal static void Run(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase - { - ecs_iter_t iter = iterable.GetIter(); - Invoker.Run(&iter, callback); - } #endif } } diff --git a/src/Flecs.NET/Core/IQueryBuilder.cs b/src/Flecs.NET/Core/IQueryBuilder.cs index 68490eca..63e14924 100644 --- a/src/Flecs.NET/Core/IQueryBuilder.cs +++ b/src/Flecs.NET/Core/IQueryBuilder.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using static Flecs.NET.Bindings.flecs; namespace Flecs.NET.Core @@ -7,6 +8,7 @@ namespace Flecs.NET.Core /// Query builder interface. /// /// + [SuppressMessage("Naming", "CA1716:Identifiers should not match keywords")] public unsafe interface IQueryBuilder { /// @@ -434,8 +436,10 @@ public unsafe interface IQueryBuilder /// public ref TQueryBuilder GroupBy(Ecs.GroupByCallback callback); + /// public ref TQueryBuilder GroupByCtx(void* ctx, Ecs.ContextFree contextFree); + /// public ref TQueryBuilder GroupByCtx(void* ctx); /// diff --git a/src/Flecs.NET/Core/Invoker.cs b/src/Flecs.NET/Core/Invoker.cs index 65f08f24..aacd0413 100644 --- a/src/Flecs.NET/Core/Invoker.cs +++ b/src/Flecs.NET/Core/Invoker.cs @@ -7,6 +7,7 @@ namespace Flecs.NET.Core /// /// A static class for holding callback invokers. /// + // Iter Invokers public static unsafe partial class Invoker { /// @@ -219,6 +220,114 @@ public static void Run(ecs_iter_t* iter, delegate*, iter->flags &= ~EcsIterIsValid; callback(new Iter(iter), &Callback); } +#endif + } + + // Iterable Invokers + public static unsafe partial class Invoker + { + /// + /// Iterates over iterable with the provided Iter callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Iter(ref T iterable, Ecs.IterCallback callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + while (iterable.GetNext(&iter)) + Iter(&iter, callback); + } + + /// + /// Iterates over iterable with the provided Each callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Each(ref T iterable, Ecs.EachEntityCallback callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + while (iterable.GetNext(&iter)) + Each(&iter, callback); + } + + /// + /// Iterates over iterable with the provided Each callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Each(ref T iterable, Ecs.EachIterCallback callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + while (iterable.GetNext(&iter)) + Each(&iter, callback); + } + + /// + /// Iterates over iterable with the provided Run callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Run(ref T iterable, Ecs.RunCallback callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + Run(&iter, callback); + } + +#if NET5_0_OR_GREATER + /// + /// Iterates over iterable with the provided Iter callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Iter(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + while (iterable.GetNext(&iter)) + Iter(&iter, callback); + } + + /// + /// Iterates over iterable with the provided Each callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Each(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + while (iterable.GetNext(&iter)) + Each(&iter, callback); + } + + /// + /// Iterates over iterable with the provided Each callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Each(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + while (iterable.GetNext(&iter)) + Each(&iter, callback); + } + + /// + /// Iterates over iterable with the provided Run callback. + /// + /// The iterable object. + /// The callback. + /// The iterable type. + public static void Run(ref T iterable, delegate* callback) where T : unmanaged, IIterableBase + { + ecs_iter_t iter = iterable.GetIter(); + Run(&iter, callback); + } #endif } } diff --git a/src/Flecs.NET/Core/IterIterable.cs b/src/Flecs.NET/Core/IterIterable.cs index e1d6e1ba..5168aa1c 100644 --- a/src/Flecs.NET/Core/IterIterable.cs +++ b/src/Flecs.NET/Core/IterIterable.cs @@ -428,50 +428,50 @@ public unsafe partial struct IterIterable /// public void Iter(Ecs.IterCallback callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(Ecs.EachEntityCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(Ecs.EachIterCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(Ecs.RunCallback callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #if NET5_0_OR_GREATER /// public void Iter(delegate* callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(delegate* callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #endif } diff --git a/src/Flecs.NET/Core/PageIterable.cs b/src/Flecs.NET/Core/PageIterable.cs index c0627de3..d379a6ea 100644 --- a/src/Flecs.NET/Core/PageIterable.cs +++ b/src/Flecs.NET/Core/PageIterable.cs @@ -198,50 +198,50 @@ public unsafe partial struct PageIterable /// public void Iter(Ecs.IterCallback callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(Ecs.EachEntityCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(Ecs.EachIterCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(Ecs.RunCallback callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #if NET5_0_OR_GREATER /// public void Iter(delegate* callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(delegate* callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #endif } diff --git a/src/Flecs.NET/Core/Query.cs b/src/Flecs.NET/Core/Query.cs index fa6fcd9f..87b4728f 100644 --- a/src/Flecs.NET/Core/Query.cs +++ b/src/Flecs.NET/Core/Query.cs @@ -1,6 +1,5 @@ using System; using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; using Flecs.NET.Utilities; using static Flecs.NET.Bindings.flecs; @@ -9,7 +8,7 @@ namespace Flecs.NET.Core /// /// A wrapper around ecs_query_t. /// - public unsafe partial struct Query : IIterator, IEquatable, IDisposable + public unsafe partial struct Query : IEquatable, IDisposable, IIterator { private ecs_query_t* _handle; @@ -433,50 +432,50 @@ public unsafe partial struct Query /// public void Iter(Ecs.IterCallback callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(Ecs.EachEntityCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(Ecs.EachIterCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(Ecs.RunCallback callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #if NET5_0_OR_GREATER /// public void Iter(delegate* callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(delegate* callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #endif } diff --git a/src/Flecs.NET/Core/WorkerIterable.cs b/src/Flecs.NET/Core/WorkerIterable.cs index 2f52a6c8..f6daf84d 100644 --- a/src/Flecs.NET/Core/WorkerIterable.cs +++ b/src/Flecs.NET/Core/WorkerIterable.cs @@ -198,50 +198,50 @@ public unsafe partial struct WorkerIterable /// public void Iter(Ecs.IterCallback callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(Ecs.EachEntityCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(Ecs.EachIterCallback callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(Ecs.RunCallback callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #if NET5_0_OR_GREATER /// public void Iter(delegate* callback) { - IIterator.Iter(ref this, callback); + Invoker.Iter(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Each(delegate* callback) { - IIterator.Each(ref this, callback); + Invoker.Each(ref this, callback); } /// public void Run(delegate* callback) { - IIterator.Run(ref this, callback); + Invoker.Run(ref this, callback); } #endif }