using System;
using System.Threading;


namespace Utility
{
    public class Singleton<T> : IDisposable where T : new()
    {
      
        static object m_lock = new object();
#if COMBAT_SERVER
       
        private static Map<int, T> _threadingInstance = new Map<int, T>();
#endif
#if !COMBAT_SERVER
        protected static T m_instance;
        public static T Instance
        {
            get
            {
                
                if (m_instance == null)
                {
                    lock (m_lock)
                    {
                        if (m_instance == null)
                        {
                            m_instance = new T();
                        }
                    }
                }

                return m_instance;
            }
            set { m_instance = value; }
        }
             public virtual void Dispose()
        {
           
            ProDispose();
            Instance = (T) (object) null;
        }
#else
        public static T Instance
        {
            get
            {
                int id = Thread.CurrentThread.ManagedThreadId;
                if (_threadingInstance.TryGetValue(id, out T VALUE))
                {
                    return VALUE;
                }
                lock (_threadingInstance)
                {
                    if (_threadingInstance.TryGetValue(id, out T VALUE2))
                    {
                        return VALUE2;
                    }
                    else
                    {
                        // LogTool.Log("新建示例"+id+"__"+typeof(T));
                        T iInstance = new T();
                        _threadingInstance.Add(id, iInstance);
                        return iInstance;
                    }
                }
            }
        }

        public virtual void Dispose()
        {
            ProDispose();
            lock (_threadingInstance)
            {
                int id = Thread.CurrentThread.ManagedThreadId;
                _threadingInstance.Remove(id);
            }
        }
#endif


        protected virtual void ProDispose()
        {
        }
    }
}