// Copyright (c) 2022 bradson // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. using System.Collections.Concurrent; using JetBrains.Annotations; namespace FisheryLib; [PublicAPI] public abstract class SingletonFactory where T_base : notnull { /// /// Get a Singleton instance of a type derived from T_base /// /// The type of the instance /// The Singleton instance public static T Get() where T : T_base => (T)GetInternal(typeof(T)); /// /// Get a Singleton instance of a type derived from T_base /// /// The type of the instance /// The Singleton instance public static T_base Get(Type type) { if (!typeof(T_base).IsAssignableFrom(type)) { ThrowHelper.ThrowArgumentException( $"Parameters for {typeof(T_base).Name}.Get must be assignable from Type { typeof(T_base).Name}. Got {type.FullDescription()} instead."); } return GetInternal(type); } /// /// protected to allow declaring of default values on derived types. Do not directly invoke this constructor or /// expose it as public. /// protected SingletonFactory() { if (_instances.ContainsKey(GetType())) { ThrowHelper.ThrowInvalidOperationException( $"Tried creating a second instance of {GetType().Name}. Only the { typeof(T_base).Name}.Get method should be used for instantiation."); } } private static T_base GetInternal(Type type) => _instances.GetOrAdd(type, static type => (T_base)Activator.CreateInstance(type, true)); private static readonly ConcurrentDictionary _instances = new(); }