5 // Derek Holden (dholden@draper.com)
7 // (C) Ximian, Inc. http://www.ximian.com
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34 // I guess this is the Boolean class. This was written word for word
35 // off the Library specification for System.Boolean in the ECMA
36 // TC39 TG2 and TG3 working documents.
38 // The XML style documentation isn't that elegant, but it seems to
39 // be the standard way according to the poorly documented C#
40 // Programmer's Reference section on XML Documentation.
43 using System.Globalization;
48 /// Represents the boolean values of logical true and false.
51 public struct Boolean : IComparable, IConvertible
57 /// The String representation of Boolean False
59 public static readonly string FalseString = "False";
62 /// The String representation of Boolean True
64 public static readonly string TrueString = "True";
66 internal bool m_value;
69 /// Compares the current Boolean instance against another object.
72 /// Throws an ArgumentException if <c>obj</c> isn't null or
75 /// <param name="obj">
76 /// The object to compare against
79 /// An int reflecting the sort order of this instance as
80 /// compared to <c>obj</c>
81 /// -1 if this instance is false and <c>obj</c> is true
82 /// 0 if this instance is equal to <c>obj</c>
83 /// 1 if this instance is true and <c>obj</c> is false,
84 /// or <c>obj</c> is null
86 public int CompareTo (object obj)
91 if (!(obj is System.Boolean))
92 throw new ArgumentException (Locale.GetText (
93 "Object is not a Boolean."));
96 if (obj == null || (m_value == true && (bool) obj == false))
99 // for case #2, else it's #1
100 return (m_value == (bool) obj) ? 0 : -1;
104 /// Determines whether this instance and another object represent the
105 /// same type and value.
107 /// <param name="obj">
108 /// The object to check against
111 /// true if this instnace and <c>obj</c> are same value,
112 /// otherwise false if it is not or null
114 public override bool Equals (Object obj)
116 if (obj == null || !(obj is System.Boolean))
119 return ((m_value) ? ((bool) obj) : !((bool) obj));
123 public int CompareTo (bool value)
125 return (m_value == value) ? 0 : -1;
128 public bool Equals (bool value)
130 return m_value == value;
135 /// Generates a hashcode for this object.
138 /// An Int32 value holding the hash code
140 public override int GetHashCode ()
142 // Guess there's not too many ways to hash a Boolean
143 return m_value ? 1 : 0;
147 /// Returns a given string as a boolean value. The string must be
148 /// equivalent to either TrueString or FalseString, with leading and/or
149 /// trailing spaces, and is parsed case-insensitively.
152 /// Throws an ArgumentNullException if <c>val</c> is null, or a
153 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
154 /// or <c>FalseString</c>
156 /// <param name="val">
157 /// The string value to parse
160 /// true if <c>val</c> is equivalent to TrueString,
163 public static bool Parse (string value)
166 throw new ArgumentNullException ("value");
168 value = value.Trim ();
170 if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0)
173 if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0)
176 throw new FormatException (Locale.GetText (
177 "Value is not equivalent to either TrueString or FalseString."));
181 public static bool TryParse (string value, out bool result)
187 value = value.Trim ();
189 if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0) {
194 if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0) {
195 // result = false; // already set at false by default
204 /// Returns a string representation of this Boolean object.
207 /// <c>FalseString</c> if the instance value is false, otherwise
208 /// <c>TrueString</c>
210 public override string ToString ()
212 return m_value ? TrueString : FalseString;
215 // =========== IConvertible Methods =========== //
216 public TypeCode GetTypeCode ()
218 return TypeCode.Boolean;
221 object IConvertible.ToType (Type conversionType, IFormatProvider provider)
223 return System.Convert.ToType (m_value, conversionType, provider);
226 bool IConvertible.ToBoolean (IFormatProvider provider)
231 byte IConvertible.ToByte (IFormatProvider provider)
233 return System.Convert.ToByte (m_value);
236 char IConvertible.ToChar (IFormatProvider provider)
238 throw new InvalidCastException ();
241 DateTime IConvertible.ToDateTime (IFormatProvider provider)
243 throw new InvalidCastException ();
246 decimal IConvertible.ToDecimal (IFormatProvider provider)
248 return System.Convert.ToDecimal (m_value);
251 double IConvertible.ToDouble (IFormatProvider provider)
253 return System.Convert.ToDouble (m_value);
256 short IConvertible.ToInt16 (IFormatProvider provider)
258 return System.Convert.ToInt16 (m_value);
261 int IConvertible.ToInt32 (IFormatProvider provider)
263 return System.Convert.ToInt32 (m_value);
266 long IConvertible.ToInt64 (IFormatProvider provider)
268 return System.Convert.ToInt64 (m_value);
271 sbyte IConvertible.ToSByte (IFormatProvider provider)
273 return System.Convert.ToSByte (m_value);
276 float IConvertible.ToSingle (IFormatProvider provider)
278 return System.Convert.ToSingle (m_value);
281 public string ToString (IFormatProvider provider)
286 ushort IConvertible.ToUInt16 (IFormatProvider provider)
288 return System.Convert.ToUInt16 (m_value);
291 uint IConvertible.ToUInt32 (IFormatProvider provider)
293 return System.Convert.ToUInt32 (m_value);
296 ulong IConvertible.ToUInt64 (IFormatProvider provider)
298 return System.Convert.ToUInt64 (m_value);