using System; using System.Collections.Generic; namespace Zenject.Internal { public static class ZenPools { #if ZEN_INTERNAL_NO_POOLS public static InjectContext SpawnInjectContext(DiContainer container, Type memberType) { return new InjectContext(container, memberType); } public static void DespawnInjectContext(InjectContext context) { } public static List SpawnList() { return new List(); } public static void DespawnList(List list) { } public static void DespawnArray(T[] arr) { } public static T[] SpawnArray(int length) { return new T[length]; } public static HashSet SpawnHashSet() { return new HashSet(); } public static Dictionary SpawnDictionary() { return new Dictionary(); } public static void DespawnDictionary(Dictionary dictionary) { } public static void DespawnHashSet(HashSet set) { } public static LookupId SpawnLookupId(IProvider provider, BindingId bindingId) { return new LookupId(provider, bindingId); } public static void DespawnLookupId(LookupId lookupId) { } public static BindInfo SpawnBindInfo() { return new BindInfo(); } public static void DespawnBindInfo(BindInfo bindInfo) { } public static BindStatement SpawnStatement() { return new BindStatement(); } public static void DespawnStatement(BindStatement statement) { } #else static readonly StaticMemoryPool _contextPool = new StaticMemoryPool(); static readonly StaticMemoryPool _lookupIdPool = new StaticMemoryPool(); static readonly StaticMemoryPool _bindInfoPool = new StaticMemoryPool(); static readonly StaticMemoryPool _bindStatementPool = new StaticMemoryPool(); public static HashSet SpawnHashSet() { return HashSetPool.Instance.Spawn(); } public static Dictionary SpawnDictionary() { return DictionaryPool.Instance.Spawn(); } public static BindStatement SpawnStatement() { return _bindStatementPool.Spawn(); } public static void DespawnStatement(BindStatement statement) { statement.Reset(); _bindStatementPool.Despawn(statement); } public static BindInfo SpawnBindInfo() { return _bindInfoPool.Spawn(); } public static void DespawnBindInfo(BindInfo bindInfo) { bindInfo.Reset(); _bindInfoPool.Despawn(bindInfo); } public static void DespawnDictionary(Dictionary dictionary) { DictionaryPool.Instance.Despawn(dictionary); } public static void DespawnHashSet(HashSet set) { HashSetPool.Instance.Despawn(set); } public static LookupId SpawnLookupId(IProvider provider, BindingId bindingId) { var lookupId = _lookupIdPool.Spawn(); lookupId.Provider = provider; lookupId.BindingId = bindingId; return lookupId; } public static void DespawnLookupId(LookupId lookupId) { lookupId.Reset(); _lookupIdPool.Despawn(lookupId); } public static List SpawnList() { return ListPool.Instance.Spawn(); } public static void DespawnList(List list) { ListPool.Instance.Despawn(list); } public static void DespawnArray(T[] arr) { ArrayPool.GetPool(arr.Length).Despawn(arr); } public static T[] SpawnArray(int length) { return ArrayPool.GetPool(length).Spawn(); } public static InjectContext SpawnInjectContext(DiContainer container, Type memberType) { var context = _contextPool.Spawn(); context.Container = container; context.MemberType = memberType; return context; } public static void DespawnInjectContext(InjectContext context) { context.Reset(); _contextPool.Despawn(context); } #endif public static InjectContext SpawnInjectContext( DiContainer container, InjectableInfo injectableInfo, InjectContext currentContext, object targetInstance, Type targetType, object concreteIdentifier) { var context = SpawnInjectContext(container, injectableInfo.MemberType); context.ObjectType = targetType; context.ParentContext = currentContext; context.ObjectInstance = targetInstance; context.Identifier = injectableInfo.Identifier; context.MemberName = injectableInfo.MemberName; context.Optional = injectableInfo.Optional; context.SourceType = injectableInfo.SourceType; context.FallBackValue = injectableInfo.DefaultValue; context.ConcreteIdentifier = concreteIdentifier; return context; } } }