//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// CommonHelpers satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;

#if !WASM
namespace CommonHelpers {
#else
namespace CommonHelpers.WASM {
#endif

//**************************************************************************************************
/// <summary>
/// </summary>
public static partial class HFlags {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="HGenericTypeException" />
public static bool Check<T>(T value,T testedFlags) where T: struct
{
   HGenericTypeChecking.VerifyFlagsEnum( typeof(T) );
   var baseType = Enum.GetUnderlyingType( typeof(T) );
   if ( baseType == typeof(int) )
      return Check( (int)(object)value, (int)(object)testedFlags );
   if ( baseType == typeof(long) )
      return Check( (long)(object)value, (long)(object)testedFlags );
   if ( baseType == typeof(short) )
      return Check( (short)(object)value, (short)(object)testedFlags );
   if ( baseType == typeof(byte) )
      return Check( (byte)(object)value, (byte)(object)testedFlags );
   if ( baseType == typeof(uint) )
      return Check( (uint)(object)value, (uint)(object)testedFlags );
   if ( baseType == typeof(ulong) )
      return Check( (ulong)(object)value, (ulong)(object)testedFlags );
   if ( baseType == typeof(ushort) )
      return Check( (ushort)(object)value, (ushort)(object)testedFlags );
   if ( baseType == typeof(sbyte) )
      return Check( (sbyte)(object)value, (sbyte)(object)testedFlags );
   throw new HGenericTypeException( typeof(T) );
}

//==================================================================================================

/// <exception cref="HGenericTypeException" />
/// <exception cref="ArgumentOutOfRangeException" />
public static bool CheckBit<T>(T value,int bitNo) where T: struct
{
   HGenericTypeChecking.VerifyFlagsEnum( typeof(T) );
   var baseType = Enum.GetUnderlyingType( typeof(T) );
   if ( baseType == typeof(int) )
      return CheckBit( (int)(object)value, bitNo );
   if ( baseType == typeof(long) )
      return CheckBit( (long)(object)value, bitNo );
   if ( baseType == typeof(short) )
      return CheckBit( (short)(object)value, bitNo );
   if ( baseType == typeof(byte) )
      return CheckBit( (byte)(object)value, bitNo );
   if ( baseType == typeof(uint) )
      return CheckBit( (uint)(object)value, bitNo );
   if ( baseType == typeof(ulong) )
      return CheckBit( (ulong)(object)value, bitNo );
   if ( baseType == typeof(ushort) )
      return CheckBit( (ushort)(object)value, bitNo );
   if ( baseType == typeof(sbyte) )
      return CheckBit( (sbyte)(object)value, bitNo );
   throw new HGenericTypeException( typeof(T) );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // HFlags
//**************************************************************************************************

} // CommonHelpers (.WASM)
