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

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

static partial class bitwise {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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

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

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

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // bitwise

} // user_rtx (.wasm)
