diff --git a/src/EcsPool.cs b/src/EcsPool.cs index e268a4c..c45c98e 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -88,7 +88,7 @@ namespace DCFApixels.DragonECS _items = new T[capacity]; _itemsCount = 0; - _componentResetHandler = ComponentResetHandler.New(); + _componentResetHandler = IEcsComponentReset.Handler; _poolRunnres = poolRunnres; } #endregion diff --git a/src/Interfaces/IEcsComponentReset.cs b/src/Interfaces/IEcsComponentReset.cs index acc301d..161ad83 100644 --- a/src/Interfaces/IEcsComponentReset.cs +++ b/src/Interfaces/IEcsComponentReset.cs @@ -1,7 +1,43 @@ -namespace DCFApixels.DragonECS +using System.Runtime.CompilerServices; +using System; +using System.Linq; + +namespace DCFApixels.DragonECS { public interface IEcsComponentReset { public void Reset(ref T component); + + + private static IEcsComponentReset _handler; + public static IEcsComponentReset Handler + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get + { + if(_handler == null) + { + Type targetType = typeof(T); + if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType))) + _handler = (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); + else + _handler = new ComponentResetDummy(); + } + return _handler; + } + } + } + + internal sealed class ComponentResetDummy : IEcsComponentReset + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Reset(ref T component) => component = default; + } + internal sealed class ComponentResetHandler : IEcsComponentReset + where T : IEcsComponentReset + { + private T _fakeInstnace = default; + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public void Reset(ref T component) => _fakeInstnace.Reset(ref component); } } diff --git a/src/Utils/ComponentResetHandler.cs b/src/Utils/ComponentResetHandler.cs deleted file mode 100644 index 20a0210..0000000 --- a/src/Utils/ComponentResetHandler.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Linq; -using System.Runtime.CompilerServices; - -namespace DCFApixels.DragonECS -{ - internal static class ComponentResetHandler - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static IEcsComponentReset New() - { - Type targetType = typeof(T); - if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType))) - { - return (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); - } - return (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetDummy<>).MakeGenericType(targetType)); - } - } - internal sealed class ComponentResetDummy : IEcsComponentReset - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Reset(ref T component) => component = default; - } - internal sealed class ComponentResetHandler : IEcsComponentReset - where T : IEcsComponentReset - { - private T _fakeInstnace = default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Reset(ref T component) => _fakeInstnace.Reset(ref component); - } -}