SteamVR_EnumEqualityComparer.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq.Expressions;
  5. namespace Valve.VR
  6. {
  7. struct SteamVREnumEqualityComparer<TEnum> : IEqualityComparer<TEnum> where TEnum : struct
  8. {
  9. static class BoxAvoidance
  10. {
  11. static readonly Func<TEnum, int> _wrapper;
  12. public static int ToInt(TEnum enu)
  13. {
  14. return _wrapper(enu);
  15. }
  16. static BoxAvoidance()
  17. {
  18. var p = Expression.Parameter(typeof(TEnum), null);
  19. var c = Expression.ConvertChecked(p, typeof(int));
  20. _wrapper = Expression.Lambda<Func<TEnum, int>>(c, p).Compile();
  21. }
  22. }
  23. public bool Equals(TEnum firstEnum, TEnum secondEnum)
  24. {
  25. return BoxAvoidance.ToInt(firstEnum) == BoxAvoidance.ToInt(secondEnum);
  26. }
  27. public int GetHashCode(TEnum firstEnum)
  28. {
  29. return BoxAvoidance.ToInt(firstEnum);
  30. }
  31. }
  32. public struct SteamVR_Input_Sources_Comparer : IEqualityComparer<SteamVR_Input_Sources>
  33. {
  34. public bool Equals(SteamVR_Input_Sources x, SteamVR_Input_Sources y)
  35. {
  36. return x == y;
  37. }
  38. public int GetHashCode(SteamVR_Input_Sources obj)
  39. {
  40. return (int)obj;
  41. }
  42. }
  43. }