Exemple #1
0
 public static void CallOnSpawn <T>(T instance, PoolInternalBaseThread pool)
 {
     if (instance is IPoolableSpawn poolable)
     {
         poolable.OnSpawn();
     }
 }
Exemple #2
0
        public T Spawn <T, TState>(TState state, System.Func <TState, T> constructor, System.Action <T> destructor = null) where T : class
        {
            if (Pools.isActive == false)
            {
                var instance = constructor.Invoke(state);
                PoolInternalBaseThread.CallOnSpawn(instance, null);
                return(instance);
            }

            var type = typeof(T);

            if (this.pool.TryGetValue(type, out var pool) == true)
            {
                return((T)pool.Spawn());
            }

            pool = new PoolInternalThread <T, TState>(type, state, constructor, destructor);
            if (this.pool.TryAdd(type, pool) == true)
            {
                return((T)pool.Spawn());
            }

            if (this.pool.TryGetValue(type, out pool) == true)
            {
                return((T)pool.Spawn());
            }

            throw new System.Exception("Spawn failed");
        }
Exemple #3
0
 public static void CallOnDespawn <T>(T instance, PoolInternalBaseThread pool)
 {
     if (instance is IPoolableRecycle poolable)
     {
         poolable.OnRecycle();
     }
 }
Exemple #4
0
 void IPoolImplementation.PoolRecycle <T>(ref T obj)
 {
     if (this.isNull == true || this.pools.Recycle(ref obj) == false)
     {
         PoolInternalBaseThread.CallOnDespawn(obj, null);
         obj = null;
     }
 }
Exemple #5
0
        public static T Create <T>(PoolInternalBaseThread pool) where T : new()
        {
            var instance = new T();

            PoolInternalBaseThread.CallOnSpawn(instance, pool);

            return(instance);
        }
Exemple #6
0
 T IPoolImplementation.PoolSpawn <T>(System.Action <T> destructor)
 {
     if (this.isNull == true)
     {
         var res = new T();
         PoolInternalBaseThread.CallOnSpawn(res, null);
         return(res);
     }
     return(this.pools.Spawn <T, byte>(0, (state) => new T(), destructor));
 }
Exemple #7
0
        T IPoolImplementation.PoolSpawn <T, TState>(TState state, System.Func <TState, T> constructor, System.Action <T> destructor)
        {
            if (this.isNull == true)
            {
                var res = constructor.Invoke(state);
                PoolInternalBaseThread.CallOnSpawn(res, null);
                //ME.WeakRef.Reg(res);
                return(res);
            }
            var instance = this.pools.Spawn <T, TState>(state, constructor, destructor);

            //ME.WeakRef.Reg(instance);
            return(instance);
        }
Exemple #8
0
        public bool Recycle <T>(ref T obj) where T : class
        {
            if (obj == null)
            {
                return(false);
            }

            if (Pools.isActive == false)
            {
                PoolInternalBaseThread.CallOnDespawn(obj, null);
                obj = default;
                return(false);
            }

            {
                var type = typeof(T);
                if (this.pool.TryGetValue(type, out var pool) == true)
                {
                    pool.Recycle(obj);
                    obj = default;
                    return(true);
                }
            }

            {
                var type = obj.GetType();
                if (this.pool.TryGetValue(type, out var pool) == true)
                {
                    pool.Recycle(obj);
                    obj = default;
                    return(true);
                }
            }

            return(false);
        }