using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Syroot.IOExtension
{
///
/// Represents a 4-byte value which can hold differently typed data.
///
[DebuggerDisplay("{" + nameof(Int32) + "} / {" + nameof(Single) + "}")]
[StructLayout(LayoutKind.Explicit)]
public struct DWord : IConvertible
{
// ---- FIELDS -------------------------------------------------------------------------------------------------
///
/// The data as an .
///
[FieldOffset(0)]
public Int32 Int32;
///
/// The data as a .
///
[FieldOffset(0)]
public Single Single;
///
/// The data as an .
///
[FieldOffset(0)]
public UInt32 UInt32;
// ---- OPERATORS ----------------------------------------------------------------------------------------------
///
/// Converts the given value to a instance.
///
/// The value to represent in the new instance.
///
public static implicit operator DWord(int value)
{
return new DWord() { Int32 = value };
}
///
/// Converts the given value to a instance.
///
/// The value to represent in the new instance.
///
public static implicit operator DWord(float value)
{
return new DWord() { Single = value };
}
///
/// Converts the given value to a instance.
///
/// The value to represent in the new instance.
///
public static implicit operator DWord(uint value)
{
return new DWord() { UInt32 = value };
}
///
/// Converts the given value to an instance.
///
/// The value to represent in the new instance.
///
public static implicit operator int(DWord value)
{
return value.Int32;
}
///
/// Converts the given value to a instance.
///
/// The value to represent in the new instance.
///
public static implicit operator float(DWord value)
{
return value.Single;
}
///
/// Converts the given value to an instance.
///
/// The value to represent in the new instance.
///
public static implicit operator uint(DWord value)
{
return value.UInt32;
}
// ---- METHODS (PUBLIC) ---------------------------------------------------------------------------------------
///
/// Returns the for this instance.
///
/// The enumerated constant that is the of the class or value type that
/// implements this interface.
public TypeCode GetTypeCode()
{
// These are sadly not flags, so just return the value for Int32 at least.
return TypeCode.Int32;
}
///
/// This operation is not supported.
///
public bool ToBoolean(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {typeof(bool)}.");
}
///
/// This operation is not supported.
///
public byte ToByte(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {typeof(byte)}.");
}
///
/// This operation is not supported.
///
public char ToChar(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {typeof(char)}.");
}
///
/// This operation is not supported.
///
public DateTime ToDateTime(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {typeof(DateTime)}.");
}
///
/// This operation is not supported.
///
public decimal ToDecimal(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {typeof(decimal)}.");
}
///
/// Converts the value of this instance to an equivalent double-precision floating-point number using the
/// specified culture-specific formatting information.
///
/// An interface implementation that supplies
/// culture-specific formatting information.
/// A double-precision floating-point number equivalent to the value of this instance.
public double ToDouble(IFormatProvider provider)
{
return Single;
}
///
/// This operation is not supported.
///
public short ToInt16(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {typeof(short)}.");
}
///
/// Converts the value of this instance to an equivalent 32-bit signed integer using the specified
/// culture-specific formatting information.
///
/// An interface implementation that supplies
/// culture-specific formatting information.
/// An 32-bit signed integer equivalent to the value of this instance.
public int ToInt32(IFormatProvider provider)
{
return Int32;
}
///
/// Converts the value of this instance to an equivalent 64-bit signed integer using the specified
/// culture-specific formatting information.
///
/// An interface implementation that supplies
/// culture-specific formatting information.
/// An 64-bit signed integer equivalent to the value of this instance.
public long ToInt64(IFormatProvider provider)
{
return Int32;
}
///
/// This operation is not supported.
///
public sbyte ToSByte(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {nameof(String)}.");
}
///
/// Converts the value of this instance to an equivalent single-precision floating-point number using the
/// specified culture-specific formatting information.
///
/// An interface implementation that supplies
/// culture-specific formatting information.
/// A single-precision floating-point number equivalent to the value of this instance.
public float ToSingle(IFormatProvider provider)
{
return Single;
}
///
/// This operation is not supported.
///
public string ToString(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {nameof(String)}.");
}
///
/// Converts the value of this instance to an of the specified that has
/// an equivalent value, using the specified culture-specific formatting information.
///
/// The to which the value of this instance is converted.
///
/// An interface implementation that supplies
/// culture-specific formatting information.
/// An instance of type conversionType whose value is equivalent to the value of
/// this instance.
public object ToType(Type conversionType, IFormatProvider provider)
{
if (conversionType == typeof(Int32) || conversionType == typeof(Int64))
{
return Int32;
}
else if (conversionType == typeof(Single) || conversionType == typeof(Double))
{
return Single;
}
else
{
throw new ArgumentException($"Cannot convert {nameof(DWord)} to type {conversionType}.");
}
}
///
/// This operation is not supported.
///
public ushort ToUInt16(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {nameof(UInt16)}.");
}
///
/// This operation is not supported.
///
public uint ToUInt32(IFormatProvider provider)
{
return UInt32;
}
///
/// This operation is not supported.
///
public ulong ToUInt64(IFormatProvider provider)
{
throw new InvalidOperationException($"Cannot convert {nameof(DWord)} to type {nameof(UInt64)}.");
}
}
}