using System;
using UnityEngine;
namespace UnityEditor.TestTools.TestRunner.GUI.Controls
{
///
/// Provides methods for dealing with common enumerator operations.
///
internal static class FlagEnumUtility
{
///
/// Checks for the presence of a flag in a flag enum value.
///
/// The value to check for the presence of the flag.
/// The flag whose presence is to be checked.
/// The flag enum type.
///
internal static bool HasFlag(T value, T flag) where T : Enum
{
ValidateUnderlyingType();
var intValue = (int)(object)value;
var intFlag = (int)(object)flag;
return (intValue & intFlag) == intFlag;
}
///
/// Sets a flag in a flag enum value.
///
/// The value where the flag should be set.
/// The flag to be set.
/// The flag enum type.
/// The input value with the flag set.
internal static T SetFlag(T value, T flag) where T : Enum
{
ValidateUnderlyingType();
var intValue = (int)(object)value;
var intFlag = (int)(object)flag;
var result = intValue | intFlag;
return (T)Enum.ToObject(typeof(T), result);
}
///
/// Removes a flag in a flag enum value.
///
/// The value where the flag should be removed.
/// The flag to be removed.
/// The flag enum type.
/// The input value with the flag removed.
internal static T RemoveFlag(T value, T flag) where T : Enum
{
ValidateUnderlyingType();
var intValue = (int)(object)value;
var intFlag = (int)(object)flag;
var result = intValue & ~intFlag;
return (T)Enum.ToObject(typeof(T), result);
}
///
/// Validates that the underlying type of an enum is integer.
///
/// The enum type.
/// Thrown if the underlying type of the enum type parameter is not integer.
private static void ValidateUnderlyingType() where T : Enum
{
if (Enum.GetUnderlyingType(typeof(T)) != typeof(int))
{
throw new ArgumentException("Argument underlying type must be integer.");
}
}
}
}