Esempio n. 1
0
        /// <summary>
        /// Register a singleton.
        /// </summary>
        /// <param name="singleton">The instance.</param>
        /// <param name="allowReplace">If true, allow replacement without warning.</param>
        public static void AddSingleton(T singleton, bool allowReplace)
        {
            if (singleton == null)
            {
                Release();
                return;
            }

            Assert.IsFalse(!allowReplace && Exists, "Replacing a singleton that already exists. Check the stack trace for more info. If you would like to allow replacement, call AddSingleton and set allowReplace to true.");

            _singleton = singleton;
            if (_onAdd != null)
            {
                _onAdd(singleton);
                _onAdd = null;
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Get a singleton whenever it is available.
        /// </summary>
        /// <param name="onAdd">The callback to run when singleton Exists</param>
        public static void GetSingletonDeferred(SingletonReady onAdd)
        {
            if (onAdd == null)
            {
                return;
            }

            if (Exists)
            {
                onAdd(GetSingleton());
            }
            else if (_onAdd == null)
            {
                _onAdd = onAdd;
            }
            else
            {
                _onAdd += onAdd;
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Release the singleton so others can use it.
 /// </summary>
 public static void Release()
 {
     _singleton = null;
     _onAdd     = null;
 }