A game about forced loneliness, made by TACStudios
1using System;
2using UnityEngine;
3
4namespace UnityEditor.TestTools.TestRunner.GUI.Controls
5{
6 /// <summary>
7 /// Provides methods for dealing with common enumerator operations.
8 /// </summary>
9 internal static class FlagEnumUtility
10 {
11 /// <summary>
12 /// Checks for the presence of a flag in a flag enum value.
13 /// </summary>
14 /// <param name="value">The value to check for the presence of the flag.</param>
15 /// <param name="flag">The flag whose presence is to be checked.</param>
16 /// <typeparam name="T">The flag enum type.</typeparam>
17 /// <returns></returns>
18 internal static bool HasFlag<T>(T value, T flag) where T : Enum
19 {
20 ValidateUnderlyingType<T>();
21
22 var intValue = (int)(object)value;
23 var intFlag = (int)(object)flag;
24 return (intValue & intFlag) == intFlag;
25 }
26
27 /// <summary>
28 /// Sets a flag in a flag enum value.
29 /// </summary>
30 /// <param name="value">The value where the flag should be set.</param>
31 /// <param name="flag">The flag to be set.</param>
32 /// <typeparam name="T">The flag enum type.</typeparam>
33 /// <returns>The input value with the flag set.</returns>
34 internal static T SetFlag<T>(T value, T flag) where T : Enum
35 {
36 ValidateUnderlyingType<T>();
37
38 var intValue = (int)(object)value;
39 var intFlag = (int)(object)flag;
40 var result = intValue | intFlag;
41 return (T)Enum.ToObject(typeof(T), result);
42 }
43
44 /// <summary>
45 /// Removes a flag in a flag enum value.
46 /// </summary>
47 /// <param name="value">The value where the flag should be removed.</param>
48 /// <param name="flag">The flag to be removed.</param>
49 /// <typeparam name="T">The flag enum type.</typeparam>
50 /// <returns>The input value with the flag removed.</returns>
51 internal static T RemoveFlag<T>(T value, T flag) where T : Enum
52 {
53 ValidateUnderlyingType<T>();
54
55 var intValue = (int)(object)value;
56 var intFlag = (int)(object)flag;
57 var result = intValue & ~intFlag;
58 return (T)Enum.ToObject(typeof(T), result);
59 }
60
61 /// <summary>
62 /// Validates that the underlying type of an enum is integer.
63 /// </summary>
64 /// <typeparam name="T">The enum type.</typeparam>
65 /// <exception cref="ArgumentException">Thrown if the underlying type of the enum type parameter is not integer.</exception>
66 private static void ValidateUnderlyingType<T>() where T : Enum
67 {
68 if (Enum.GetUnderlyingType(typeof(T)) != typeof(int))
69 {
70 throw new ArgumentException("Argument underlying type must be integer.");
71 }
72 }
73 }
74}