/**
 * 封装List，用于重用
 */

using System.Collections.Generic;

namespace Fort23.Core
{
    public class ListComponent<T>: CObject
    {
        private bool _isDispose;
        
        public static ListComponent<T> Create()
        {
            ListComponent<T> listComponent = CObjectPool.Instance.Fetch<ListComponent<T>>();
            listComponent._isDispose = false;
            return listComponent;
        }
        
        public List<T> List { get; } = new List<T>();

        public override void Dispose()
        {
            if (this._isDispose)
            {
                return;
            }

            this._isDispose = true;
            
            base.Dispose();
            
            this.List.Clear();
            CObjectPool.Instance.Recycle(this);
        }
    }

    public class ListComponentDisposeChildren<T>: CObject where T : CObject
    {
        private bool _isDispose;
        
        public static ListComponentDisposeChildren<T> Create()
        {
            ListComponentDisposeChildren<T> listComponent = CObjectPool.Instance.Fetch<ListComponentDisposeChildren<T>>();
            listComponent._isDispose = false;
            return listComponent;
        }
        
        public List<T> List = new List<T>();

        public override void Dispose()
        {
            if (this._isDispose)
            {
                return;
            }
            
            this._isDispose = true;

            base.Dispose();

            foreach (T entity in this.List)
            {
                entity.Dispose();
            }

            this.List.Clear();
            
            CObjectPool.Instance.Recycle(this);
        }
    }
}