refactoring

This commit is contained in:
Mikhail 2023-04-01 22:46:42 +08:00
parent 815b1c2bb0
commit 8f39070e22
3 changed files with 38 additions and 34 deletions

View File

@ -88,7 +88,7 @@ namespace DCFApixels.DragonECS
_items = new T[capacity];
_itemsCount = 0;
_componentResetHandler = ComponentResetHandler.New<T>();
_componentResetHandler = IEcsComponentReset<T>.Handler;
_poolRunnres = poolRunnres;
}
#endregion

View File

@ -1,7 +1,43 @@
namespace DCFApixels.DragonECS
using System.Runtime.CompilerServices;
using System;
using System.Linq;
namespace DCFApixels.DragonECS
{
public interface IEcsComponentReset<T>
{
public void Reset(ref T component);
private static IEcsComponentReset<T> _handler;
public static IEcsComponentReset<T> Handler
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if(_handler == null)
{
Type targetType = typeof(T);
if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType)))
_handler = (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType));
else
_handler = new ComponentResetDummy<T>();
}
return _handler;
}
}
}
internal sealed class ComponentResetDummy<T> : IEcsComponentReset<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => component = default;
}
internal sealed class ComponentResetHandler<T> : IEcsComponentReset<T>
where T : IEcsComponentReset<T>
{
private T _fakeInstnace = default;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => _fakeInstnace.Reset(ref component);
}
}

View File

@ -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<T> New<T>()
{
Type targetType = typeof(T);
if (targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType)))
{
return (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType));
}
return (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetDummy<>).MakeGenericType(targetType));
}
}
internal sealed class ComponentResetDummy<T> : IEcsComponentReset<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => component = default;
}
internal sealed class ComponentResetHandler<T> : IEcsComponentReset<T>
where T : IEcsComponentReset<T>
{
private T _fakeInstnace = default;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => _fakeInstnace.Reset(ref component);
}
}