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;
44 using System.Runtime.InteropServices;
49 /// Represents the boolean values of logical true and false.
55 public struct Boolean : IComparable, IConvertible
57 , IComparable <bool>, IEquatable <bool>
61 /// The String representation of Boolean False
63 public static readonly string FalseString = "False";
66 /// The String representation of Boolean True
68 public static readonly string TrueString = "True";
70 internal bool m_value;
73 /// Compares the current Boolean instance against another object.
76 /// Throws an ArgumentException if <c>obj</c> isn't null or
79 /// <param name="obj">
80 /// The object to compare against
83 /// An int reflecting the sort order of this instance as
84 /// compared to <c>obj</c>
85 /// -1 if this instance is false and <c>obj</c> is true
86 /// 0 if this instance is equal to <c>obj</c>
87 /// 1 if this instance is true and <c>obj</c> is false,
88 /// or <c>obj</c> is null
90 public int CompareTo (object obj)
95 if (!(obj is System.Boolean))
96 throw new ArgumentException (Locale.GetText (
97 "Object is not a Boolean."));
99 bool value = (bool) obj;
102 if (m_value && !value)
105 // for case #2, else it's #1
106 return (m_value == value) ? 0 : -1;
110 /// Determines whether this instance and another object represent the
111 /// same type and value.
113 /// <param name="obj">
114 /// The object to check against
117 /// true if this instnace and <c>obj</c> are same value,
118 /// otherwise false if it is not or null
120 public override bool Equals (Object obj)
122 if (obj == null || !(obj is System.Boolean))
125 bool value = (bool) obj;
127 return m_value ? value : !value;
131 public int CompareTo (bool value)
133 if (m_value == value)
135 return !m_value ? -1 : 1;
138 public bool Equals (bool obj)
140 return m_value == obj;
145 /// Generates a hashcode for this object.
148 /// An Int32 value holding the hash code
150 public override int GetHashCode ()
152 // Guess there's not too many ways to hash a Boolean
153 return m_value ? 1 : 0;
157 /// Returns a given string as a boolean value. The string must be
158 /// equivalent to either TrueString or FalseString, with leading and/or
159 /// trailing spaces, and is parsed case-insensitively.
162 /// Throws an ArgumentNullException if <c>val</c> is null, or a
163 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
164 /// or <c>FalseString</c>
166 /// <param name="val">
167 /// The string value to parse
170 /// true if <c>val</c> is equivalent to TrueString,
173 public static bool Parse (string value)
176 throw new ArgumentNullException ("value");
178 value = value.Trim ();
180 if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0)
183 if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0)
186 throw new FormatException (Locale.GetText (
187 "Value is not equivalent to either TrueString or FalseString."));
191 public static bool TryParse (string value, out bool result)
197 value = value.Trim ();
199 if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0) {
204 if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0) {
205 // result = false; // already set at false by default
214 /// Returns a string representation of this Boolean object.
217 /// <c>FalseString</c> if the instance value is false, otherwise
218 /// <c>TrueString</c>
220 public override string ToString ()
222 return m_value ? TrueString : FalseString;
225 // =========== IConvertible Methods =========== //
226 public TypeCode GetTypeCode ()
228 return TypeCode.Boolean;
231 object IConvertible.ToType (Type type, IFormatProvider provider)
233 return System.Convert.ToType (m_value, type, provider, false);
236 bool IConvertible.ToBoolean (IFormatProvider provider)
241 byte IConvertible.ToByte (IFormatProvider provider)
243 return System.Convert.ToByte (m_value);
246 char IConvertible.ToChar (IFormatProvider provider)
248 throw new InvalidCastException ();
251 DateTime IConvertible.ToDateTime (IFormatProvider provider)
253 throw new InvalidCastException ();
256 decimal IConvertible.ToDecimal (IFormatProvider provider)
258 return System.Convert.ToDecimal (m_value);
261 double IConvertible.ToDouble (IFormatProvider provider)
263 return System.Convert.ToDouble (m_value);
266 short IConvertible.ToInt16 (IFormatProvider provider)
268 return System.Convert.ToInt16 (m_value);
271 int IConvertible.ToInt32 (IFormatProvider provider)
273 return System.Convert.ToInt32 (m_value);
276 long IConvertible.ToInt64 (IFormatProvider provider)
278 return System.Convert.ToInt64 (m_value);
282 #pragma warning disable 3019
283 [CLSCompliant (false)]
285 sbyte IConvertible.ToSByte (IFormatProvider provider)
287 return System.Convert.ToSByte (m_value);
290 #pragma warning restore 3019
293 float IConvertible.ToSingle (IFormatProvider provider)
295 return System.Convert.ToSingle (m_value);
298 public string ToString (IFormatProvider provider)
304 #pragma warning disable 3019
305 [CLSCompliant (false)]
307 ushort IConvertible.ToUInt16 (IFormatProvider provider)
309 return System.Convert.ToUInt16 (m_value);
312 #pragma warning restore 3019
316 #pragma warning disable 3019
317 [CLSCompliant (false)]
319 uint IConvertible.ToUInt32 (IFormatProvider provider)
321 return System.Convert.ToUInt32 (m_value);
324 #pragma warning restore 3019
328 #pragma warning disable 3019
329 [CLSCompliant (false)]
331 ulong IConvertible.ToUInt64 (IFormatProvider provider)
333 return System.Convert.ToUInt64 (m_value);
336 #pragma warning restore 3019