Enums.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using System;
  2. namespace FirstGearGames.Utilities.Maths
  3. {
  4. public static class Enums
  5. {
  6. /// <summary>
  7. /// Determine an enum value from a given string. This can be an expensive function.
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. /// <param name="text">Text of string.</param>
  11. /// <param name="defaultValue">Default value if enum couldn't be found.</param>
  12. /// <returns>Enum found or default value if no enum is found.</returns>
  13. public static T FromString<T>(string text, T defaultValue)
  14. {
  15. //If string is empty or null return default value.
  16. if (string.IsNullOrEmpty(text))
  17. return defaultValue;
  18. //If enum isn't defined return default value.
  19. if (!Enum.IsDefined(typeof(T), (string)text))
  20. return defaultValue;
  21. //Return parsed value.
  22. return (T)Enum.Parse(typeof(T), text, true);
  23. }
  24. /// <summary>
  25. /// Returns if whole(extended enum) has any of the part values.
  26. /// </summary>
  27. /// <param name="whole"></param>
  28. /// <param name="part">Values to check for within whole.</param>
  29. /// <returns>Returns true part is within whole.</returns>
  30. public static bool Contains(this Enum whole, Enum part)
  31. {
  32. //If not the same type of Enum return false.
  33. /* Commented out for performance. Designer
  34. * should know better than to compare two different
  35. * enums. */
  36. //if (!SameType(value, target))
  37. // return false;
  38. /* Convert enum values to ulong. With so few
  39. * values a uint would be safe, but should
  40. * the options expand ulong is safer. */
  41. ulong wholeNum = Convert.ToUInt64(whole);
  42. ulong partNum = Convert.ToUInt64(part);
  43. return ((wholeNum & partNum) != 0);
  44. }
  45. /// <summary>
  46. /// Returns if part values contains any of whole(extended enum).
  47. /// </summary>
  48. /// <param name="whole"></param>
  49. /// <param name="part"></param>
  50. /// <returns>Returns true whole is within part.</returns>
  51. public static bool ReverseContains(this Enum whole, Enum part)
  52. {
  53. //If not the same type of Enum return false.
  54. /* Commented out for performance. Designer
  55. * should know better than to compare two different
  56. * enums. */
  57. //if (!SameType(value, target))
  58. // return false;
  59. /* Convert enum values to ulong. With so few
  60. * values a uint would be safe, but should
  61. * the options expand ulong is safer. */
  62. ulong wholeNum = Convert.ToUInt64(whole);
  63. ulong partNum = Convert.ToUInt64(part);
  64. return ((partNum & wholeNum) != 0);
  65. }
  66. /// <summary>
  67. /// Returns if an enum equals a specified value.
  68. /// </summary>
  69. /// <param name="value"></param>
  70. /// <param name="target"></param>
  71. /// <returns></returns>
  72. public static bool Equals(this Enum value, Enum target)
  73. {
  74. //If not the same type of Enum return false.
  75. /* Commented out for performance. Designer
  76. * should know better than to compare two different
  77. * enums. */
  78. //if (!SameType(value, target))
  79. // return false;
  80. ulong valueNum = Convert.ToUInt64(value);
  81. ulong wholeNum = Convert.ToUInt64(target);
  82. return (valueNum == wholeNum);
  83. }
  84. /// <summary>
  85. /// Returns if a is the same Enum as b.
  86. /// </summary>
  87. /// <param name="a"></param>
  88. /// <param name="target"></param>
  89. /// <returns></returns>
  90. public static bool SameType(Enum a, Enum b)
  91. {
  92. return (a.GetType() == b.GetType());
  93. }
  94. }
  95. }