| | 1 | | using System; |
| | 2 | | using System.Runtime.CompilerServices; |
| | 3 | |
|
| | 4 | | namespace MainScripts.DCL.Helpers.Utils |
| | 5 | | { |
| | 6 | | /// <summary> |
| | 7 | | /// Contains non-allocating generic versions of enum utility functions |
| | 8 | | /// </summary> |
| | 9 | | public static class EnumUtils |
| | 10 | | { |
| | 11 | | public static unsafe bool HasFlag<T>(T x, T y) where T : unmanaged, Enum |
| | 12 | | { |
| 900 | 13 | | switch (sizeof(T)) |
| | 14 | | { |
| | 15 | | case sizeof(byte): |
| 0 | 16 | | return (*(byte*) &x & *(byte*) &y) != 0; |
| | 17 | |
|
| | 18 | | case sizeof(short): |
| 0 | 19 | | return (*(short*) &x & *(short*) &y) != 0; |
| | 20 | |
|
| | 21 | | case sizeof(int): |
| 900 | 22 | | return (*(int*) &x & *(int*) &y) != 0; |
| | 23 | |
|
| | 24 | | case sizeof(long): |
| 0 | 25 | | return (*(long*) &x & *(long*) &y) != 0L; |
| | 26 | |
|
| | 27 | | default: |
| 0 | 28 | | return false; |
| | 29 | | } |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public static unsafe T Max<T>(T x, T y) where T : unmanaged, Enum |
| | 33 | | { |
| 0 | 34 | | switch (sizeof(T)) |
| | 35 | | { |
| | 36 | | case sizeof(byte): |
| 0 | 37 | | var b = Math.Max(*(byte*) &x, *(byte*) &y); |
| 0 | 38 | | return Unsafe.As<byte, T>(ref b); |
| | 39 | | case sizeof(short): |
| 0 | 40 | | var s = Math.Max(*(short*) &x, *(short*) &y); |
| 0 | 41 | | return Unsafe.As<short, T>(ref s); |
| | 42 | | case sizeof(int): |
| 0 | 43 | | var i = Math.Max(*(int*) &x, *(int*) &y); |
| 0 | 44 | | return Unsafe.As<int, T>(ref i); |
| | 45 | | case sizeof(long): |
| 0 | 46 | | var l = Math.Max(*(long*) &x, *(long*) &y); |
| 0 | 47 | | return Unsafe.As<long, T>(ref l); |
| | 48 | | default: |
| 0 | 49 | | throw new NotImplementedException(sizeof(T).ToString()); |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public static T[] Values<T>() => |
| 2 | 54 | | (T[]) Enum.GetValues(typeof(T)); |
| | 55 | | } |
| | 56 | | } |