/// <summary>
/// In Win32, The BOOL is a 32-bit signed integer. Represent the BOOL
/// </summary>
public struct BOOL
{
/// <summary>
/// The possible BOOL values: FALSE, 0
/// </summary>
public static readonly BOOL FALSE = new BOOL(0);
/// <summary>
/// The possible BOOL values: TRUE, 1
/// </summary>
public static readonly BOOL TRUE = new BOOL(1);
/// <summary>
/// Private field that stores 0x00, 0x01 for FALSE, TRUE:
/// </summary>
int value;
/// <summary>
/// Private constructor. The value parameter must be 0, or 1:
/// </summary>
/// <param name="value">The value parameter must be 0, or 1</param>
private BOOL(int value)
{
this.value = value;
}
/// <summary>
/// Implicit conversion from bool to BOOL.
/// Maps true to TRUE and false to FALSE:
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static implicit operator BOOL(bool x)
{
return x ? TRUE : FALSE;
}
/// <summary>
/// Explicit conversion from BOOL to bool.
/// Returns TRUE or FALSE:
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static explicit operator bool(BOOL x)
{
return x.value > 0;
}
/// <summary>
/// Equality operator.
/// Returns TRUE or FALSE:
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static BOOL operator ==(BOOL x, BOOL y)
{
return x.value == y.value ? TRUE : FALSE;
}
/// <summary>
/// Inequality operator.
/// Returns TRUE or FALSE:
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static BOOL operator !=(BOOL x, BOOL y)
{
return x.value != y.value ? TRUE : FALSE;
}
/// <summary>
/// Logical negation operator.
/// Returns TRUE if the operand is FALSE,
/// or FALSE if the operand is TRUE:
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static BOOL operator !(BOOL x)
{
return new BOOL((x.value + 1) % 2);
}
/// <summary>
/// Logical AND operator.
/// Returns FALSE if either operand is FALSE, otherwise TRUE:
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static BOOL operator &(BOOL x, BOOL y)
{
return new BOOL(x.value < y.value ? x.value : y.value);
}
/// <summary>
/// Logical OR operator.
/// Returns TRUE if either operand is TRUE, otherwise FALSE:
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static BOOL operator |(BOOL x, BOOL y)
{
return new BOOL(x.value > y.value ? x.value : y.value);
}
/// <summary>
/// Definitely true operator.
/// Returns true if the operand is TRUE, false otherwise:
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static bool operator true(BOOL x)
{
return x.value > 0;
}
/// <summary>
/// Definitely false operator.
/// Returns true if the operand is FALSE, false otherwise:
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static bool operator false(BOOL x)
{
return x.value < 0;
}
/// <summary>
/// Overload the conversion from BOOL to string:
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
public static implicit operator string(BOOL x)
{
return x.value > 0 ? "TRUE" : "FALSE";
}
/// <summary>
/// Override the Object.Equals(object o) method:
/// </summary>
/// <param name="o"></param>
/// <returns></returns>
public override bool Equals(object o)
{
try
{
return (bool)(this == (BOOL)o);
}
catch
{
return false;
}
}
/// <summary>
/// Override the Object.GetHashCode() method:
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return value;
}
/// <summary>
/// Override the ToString method to convert BOOL to a string:
/// </summary>
/// <returns></returns>
public override string ToString()
{
switch (value)
{
case 0:
return "FALSE";
case 1:
return "TRUE";
default:
throw new InvalidOperationException();
}
}
}