Advertisement
Guest User

ProofOfConcept

a guest
Nov 19th, 2019
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. using UnityEngine;
  2. using Multiton.Containers;
  3.  
  4. public static class Multiton<T> where T : SingletonOfType
  5. {
  6.  
  7. /// <summary>
  8. /// Checks within the static dictionary if it's possible to add a Singleton that exists of a certain type
  9. /// </summary>
  10. /// <param name="_singletonType"></param>
  11. /// <returns></returns>
  12. public static bool TryAddSingleton(T _singletonType)
  13. {
  14. if(!MultitonContainer.SingletonCollection.ContainsKey(_singletonType))
  15. {
  16. MultitonContainer.SingletonCollection.Add(_singletonType, _singletonType);
  17. Debug.LogFormat($"Succesfully added: { _singletonType.GetType() } to the static Multiton dictionary");
  18. return true;
  19. }
  20.  
  21. return false;
  22. }
  23.  
  24. /// <summary>
  25. /// Atttempts to return a co-responding type of singleton
  26. /// </summary>
  27. /// <returns></returns>
  28. public static T TryGetSingletonOfType()
  29. {
  30. foreach (object type in MultitonContainer.SingletonCollection.Values)
  31. {
  32. if (type.GetType().Equals(typeof(T)))
  33. {
  34. return (T)type;
  35. }
  36. }
  37.  
  38. return default;
  39. }
  40.  
  41. /// <summary>
  42. /// Attempts to remove a possibly existing Singleton based on the given parameter
  43. /// </summary>
  44. /// <param name="_singletonType"></param>
  45. /// <returns></returns>
  46. public static bool TryRemoveSingletonOfType(T _singletonType)
  47. {
  48. if(MultitonContainer.SingletonCollection.ContainsKey(_singletonType))
  49. {
  50. MultitonContainer.SingletonCollection.Remove(_singletonType);
  51. return true;
  52. }
  53.  
  54. return false;
  55. }
  56.  
  57. /// <summary>
  58. /// Clears the Multiton out of all the contained Singletons
  59. /// </summary>
  60. public static void ClearAllSingletons()
  61. {
  62. MultitonContainer.SingletonCollection.Clear();
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement