Example #1
0
        /**
         * 对象入池
         **/
        public void ReturnObject <T>(T Instance) where T : IPoolable, new()
        {
            Type Prototype = typeof(T);

            if (ObjectDict.ContainsKey(Prototype))
            {
                Queue <Object> Objects;
                List <Object>  Used;
                bool           Result = ObjectDict.TryGetValue(Prototype, out Objects);
                if (Result)
                {
                    ObjectUsedRef.TryGetValue(Prototype, out Used);
                    int Capacity = 0;
                    CapacityDict.TryGetValue(Prototype, out Capacity);
                    if (Used.Contains(Instance))
                    {
                        //更新使用引用计数
                        int UsedCount = 0;
                        ObjectUsedDict.TryGetValue(Prototype, out UsedCount);
                        UsedCount--;
                        ObjectUsedDict[Prototype] = UsedCount;

                        //从使用队列删除,并且将对象加入未使用队列
                        Used.Remove(Instance);

                        if (Objects.Count >= Capacity)
                        {
                            //空闲对象到达阀值则将对象直接丢弃等待垃圾回收
                            ((IDisposable)Instance).Dispose();
                        }
                        else
                        {
                            ((IPoolable)Instance).Reset();
                            Objects.Enqueue(Instance);
                        }
                    }
                }
            }
        }