From 3a1d6548b3727afcf17649933d7c3b7a518b58fd Mon Sep 17 00:00:00 2001 From: David Hall Date: Tue, 27 Aug 2019 09:05:48 -0600 Subject: [PATCH] Added BOOL and BOOLEAN types --- Core/InteropServices/BOOL.cs | 198 ++++++++++++++++++++++++++++++++++++++++ Core/InteropServices/BOOLEAN.cs | 188 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 386 insertions(+) create mode 100644 Core/InteropServices/BOOL.cs create mode 100644 Core/InteropServices/BOOLEAN.cs diff --git a/Core/InteropServices/BOOL.cs b/Core/InteropServices/BOOL.cs new file mode 100644 index 00000000..26244bb9 --- /dev/null +++ b/Core/InteropServices/BOOL.cs @@ -0,0 +1,198 @@ +using System; +using System.ComponentModel; +using System.Globalization; +using System.Runtime.InteropServices; + +namespace Vanara +{ + /// Managed instance of the SIZE_T type. + [StructLayout(LayoutKind.Sequential), Serializable] + [TypeConverter(typeof(BOOLTypeConverter))] + public struct BOOL : IEquatable, IComparable, IEquatable, IComparable, IConvertible, IComparable + { + private uint val; + + internal const uint True = 1U; + + internal const uint False = 0U; + + /// Initializes a new instance of the struct. + /// The value. + public BOOL(uint value) => val = value; + + /// Initializes a new instance of the struct. + /// The value. + public BOOL(bool value) => val = value ? True : False; + + /// Gets the value. + /// The value. + public bool Value { get => val != 0; private set => val = value ? True : False; } + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator BOOL(int value) => value >= 0 ? new BOOL((uint)value) : throw new ArgumentOutOfRangeException(); + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator BOOL(uint value) => new BOOL(value); + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator BOOL(bool value) => new BOOL(value); + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static explicit operator int(BOOL value) => (int)value.val; + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static explicit operator uint(BOOL value) => value.val; + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator bool(BOOL value) => value.Value; + + /// Indicates whether two instances are not equal. + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is not equal to the value of ; otherwise, . + /// + public static bool operator !=(BOOL s1, BOOL s2) => !s1.Equals(s2); + + /// Indicates whether a specified is less than another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is less than the value of ; otherwise, . + /// + public static bool operator <(BOOL s1, BOOL s2) => (s1.CompareTo(s2) < 0) ? true : false; + + /// Indicates whether a specified is less than or equal to another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is less than or equal to the value of ; + /// otherwise, . + /// + public static bool operator <=(BOOL s1, BOOL s2) => (s1.CompareTo(s2) <= 0) ? true : false; + + /// Indicates whether two instances are equal. + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is equal to the value of ; otherwise, . + /// + public static bool operator ==(BOOL s1, BOOL s2) => s1.Equals(s2); + + /// Indicates whether a specified is greater than another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is greater than the value of ; otherwise, . + /// + public static bool operator >(BOOL s1, BOOL s2) => (s1.CompareTo(s2) > 0) ? true : false; + + /// Indicates whether a specified is greater than or equal to another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is greater than or equal to the value of ; + /// otherwise, . + /// + public static bool operator >=(BOOL s1, BOOL s2) => (s1.CompareTo(s2) >= 0) ? true : false; + + /// + public int CompareTo(BOOL other) => Value.CompareTo(other.Value); + + /// + public int CompareTo(bool other) => Value.CompareTo(other); + + /// + public override bool Equals(object obj) => obj is BOOL s ? Equals(s) : (obj is bool b ? Value.Equals(b) : Value.Equals(obj)); + + /// + public bool Equals(BOOL other) => Value.Equals(other.Value); + + /// + public bool Equals(bool other) => Value.Equals(other); + + /// + public override int GetHashCode() => (int)val; + + /// + public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + public override string ToString() => Value.ToString(); + + /// + public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + int IComparable.CompareTo(object obj) => Value.CompareTo(Convert.ChangeType(obj, typeof(ulong))); + + /// + bool IConvertible.ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + char IConvertible.ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + DateTime IConvertible.ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)Value).ToDouble(provider); + + /// + short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + internal class BOOLTypeConverter : UInt32Converter + { + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (!(value is BOOL b)) throw new ArgumentException(); + return base.ConvertTo(context, culture, b.Value, destinationType); + } + + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) => + new BOOL((uint)base.ConvertFrom(context, culture, value)); + } + } +} \ No newline at end of file diff --git a/Core/InteropServices/BOOLEAN.cs b/Core/InteropServices/BOOLEAN.cs new file mode 100644 index 00000000..3e4635e6 --- /dev/null +++ b/Core/InteropServices/BOOLEAN.cs @@ -0,0 +1,188 @@ +using System; +using System.ComponentModel; +using System.Globalization; +using System.Runtime.InteropServices; + +namespace Vanara +{ + /// Managed instance of the SIZE_T type. + [StructLayout(LayoutKind.Sequential), Serializable] + [TypeConverter(typeof(BOOLEANTypeConverter))] + public struct BOOLEAN : IEquatable, IComparable, IEquatable, IComparable, IConvertible, IComparable + { + private byte val; + + internal const byte True = 1; + + internal const byte False = 0; + + /// Initializes a new instance of the struct. + /// The value. + public BOOLEAN(byte value) => val = value; + + /// Initializes a new instance of the struct. + /// The value. + public BOOLEAN(bool value) => val = value ? True : False; + + /// Gets the value. + /// The value. + public bool Value { get => val != False; private set => val = value ? True : False; } + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator BOOLEAN(byte value) => new BOOLEAN(value); + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator BOOLEAN(bool value) => new BOOLEAN(value); + + /// Performs an explicit conversion from to . + /// The value. + /// The result of the conversion. + public static explicit operator byte(BOOLEAN value) => value.val; + + /// Performs an implicit conversion from to . + /// The value. + /// The result of the conversion. + public static implicit operator bool(BOOLEAN value) => value.Value; + + /// Indicates whether two instances are not equal. + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is not equal to the value of ; otherwise, . + /// + public static bool operator !=(BOOLEAN s1, BOOLEAN s2) => !s1.Equals(s2); + + /// Indicates whether a specified is less than another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is less than the value of ; otherwise, . + /// + public static bool operator <(BOOLEAN s1, BOOLEAN s2) => (s1.CompareTo(s2) < 0) ? true : false; + + /// Indicates whether a specified is less than or equal to another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is less than or equal to the value of ; + /// otherwise, . + /// + public static bool operator <=(BOOLEAN s1, BOOLEAN s2) => (s1.CompareTo(s2) <= 0) ? true : false; + + /// Indicates whether two instances are equal. + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is equal to the value of ; otherwise, . + /// + public static bool operator ==(BOOLEAN s1, BOOLEAN s2) => s1.Equals(s2); + + /// Indicates whether a specified is greater than another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is greater than the value of ; otherwise, . + /// + public static bool operator >(BOOLEAN s1, BOOLEAN s2) => (s1.CompareTo(s2) > 0) ? true : false; + + /// Indicates whether a specified is greater than or equal to another specified . + /// The first integral size to compare. + /// The second integral size to compare. + /// + /// if the value of is greater than or equal to the value of ; + /// otherwise, . + /// + public static bool operator >=(BOOLEAN s1, BOOLEAN s2) => (s1.CompareTo(s2) >= 0) ? true : false; + + /// + public int CompareTo(BOOLEAN other) => Value.CompareTo(other.Value); + + /// + public int CompareTo(bool other) => Value.CompareTo(other); + + /// + public override bool Equals(object obj) => obj is BOOLEAN s ? Equals(s) : (obj is bool b ? Value.Equals(b) : Value.Equals(obj)); + + /// + public bool Equals(BOOLEAN other) => Value.Equals(other.Value); + + /// + public bool Equals(bool other) => Value.Equals(other); + + /// + public override int GetHashCode() => (int)val; + + /// + public TypeCode GetTypeCode() => Value.GetTypeCode(); + + /// + public override string ToString() => Value.ToString(); + + /// + public string ToString(IFormatProvider provider) => Value.ToString(provider); + + /// + int IComparable.CompareTo(object obj) => Value.CompareTo(Convert.ChangeType(obj, typeof(ulong))); + + /// + bool IConvertible.ToBoolean(IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + byte IConvertible.ToByte(IFormatProvider provider) => ((IConvertible)Value).ToByte(provider); + + /// + char IConvertible.ToChar(IFormatProvider provider) => ((IConvertible)Value).ToChar(provider); + + /// + DateTime IConvertible.ToDateTime(IFormatProvider provider) => ((IConvertible)Value).ToDateTime(provider); + + /// + decimal IConvertible.ToDecimal(IFormatProvider provider) => ((IConvertible)Value).ToDecimal(provider); + + /// + double IConvertible.ToDouble(IFormatProvider provider) => ((IConvertible)Value).ToDouble(provider); + + /// + short IConvertible.ToInt16(IFormatProvider provider) => ((IConvertible)Value).ToInt16(provider); + + /// + int IConvertible.ToInt32(IFormatProvider provider) => ((IConvertible)Value).ToInt32(provider); + + /// + long IConvertible.ToInt64(IFormatProvider provider) => ((IConvertible)Value).ToInt64(provider); + + /// + sbyte IConvertible.ToSByte(IFormatProvider provider) => ((IConvertible)Value).ToSByte(provider); + + /// + float IConvertible.ToSingle(IFormatProvider provider) => ((IConvertible)Value).ToSingle(provider); + + /// + object IConvertible.ToType(Type conversionType, IFormatProvider provider) => ((IConvertible)Value).ToBoolean(provider); + + /// + ushort IConvertible.ToUInt16(IFormatProvider provider) => ((IConvertible)Value).ToUInt16(provider); + + /// + uint IConvertible.ToUInt32(IFormatProvider provider) => ((IConvertible)Value).ToUInt32(provider); + + /// + ulong IConvertible.ToUInt64(IFormatProvider provider) => ((IConvertible)Value).ToUInt64(provider); + + internal class BOOLEANTypeConverter : ByteConverter + { + public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType) + { + if (!(value is BOOLEAN b)) throw new ArgumentException(); + return base.ConvertTo(context, culture, b.Value, destinationType); + } + + public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) => + new BOOLEAN((byte)base.ConvertFrom(context, culture, value)); + } + } +} \ No newline at end of file