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.
53 public struct Boolean
: IComparable
, IConvertible
, IComparable
<bool>, IEquatable
<bool>
56 /// The String representation of Boolean False
58 public static readonly string FalseString
= "False";
61 /// The String representation of Boolean True
63 public static readonly string TrueString
= "True";
65 internal bool m_value
;
68 /// Compares the current Boolean instance against another object.
71 /// Throws an ArgumentException if <c>obj</c> isn't null or
74 /// <param name="obj">
75 /// The object to compare against
78 /// An int reflecting the sort order of this instance as
79 /// compared to <c>obj</c>
80 /// -1 if this instance is false and <c>obj</c> is true
81 /// 0 if this instance is equal to <c>obj</c>
82 /// 1 if this instance is true and <c>obj</c> is false,
83 /// or <c>obj</c> is null
85 public int CompareTo (object obj
)
90 if (!(obj
is System
.Boolean
))
91 throw new ArgumentException (Locale
.GetText (
92 "Object is not a Boolean."));
94 bool value = (bool) obj
;
97 if (m_value
&& !value)
100 // for case #2, else it's #1
101 return (m_value
== value) ? 0 : -1;
105 /// Determines whether this instance and another object represent the
106 /// same type and value.
108 /// <param name="obj">
109 /// The object to check against
112 /// true if this instnace and <c>obj</c> are same value,
113 /// otherwise false if it is not or null
115 public override bool Equals (Object obj
)
117 if (obj
== null || !(obj
is System
.Boolean
))
120 bool value = (bool) obj
;
122 return m_value
? value : !value;
125 public int CompareTo (bool value)
127 if (m_value
== value)
129 return !m_value
? -1 : 1;
132 public bool Equals (bool obj
)
134 return m_value
== obj
;
138 /// Generates a hashcode for this object.
141 /// An Int32 value holding the hash code
143 public override int GetHashCode ()
145 // Guess there's not too many ways to hash a Boolean
146 return m_value
? 1 : 0;
150 /// Returns a given string as a boolean value. The string must be
151 /// equivalent to either TrueString or FalseString, with leading and/or
152 /// trailing spaces, and is parsed case-insensitively.
155 /// Throws an ArgumentNullException if <c>val</c> is null, or a
156 /// FormatException if <c>val</c> doesn't match <c>TrueString</c>
157 /// or <c>FalseString</c>
159 /// <param name="val">
160 /// The string value to parse
163 /// true if <c>val</c> is equivalent to TrueString,
166 public static bool Parse (string value)
169 throw new ArgumentNullException ("value");
171 value = value.Trim ();
173 if (String
.Compare (value, TrueString
, true, CultureInfo
.InvariantCulture
) == 0)
176 if (String
.Compare (value, FalseString
, true, CultureInfo
.InvariantCulture
) == 0)
179 throw new FormatException (Locale
.GetText (
180 "Value is not equivalent to either TrueString or FalseString."));
183 public static bool TryParse (string value, out bool result
)
189 value = value.Trim ();
191 if (String
.Compare (value, TrueString
, true, CultureInfo
.InvariantCulture
) == 0) {
196 if (String
.Compare (value, FalseString
, true, CultureInfo
.InvariantCulture
) == 0) {
197 // result = false; // already set at false by default
205 /// Returns a string representation of this Boolean object.
208 /// <c>FalseString</c> if the instance value is false, otherwise
209 /// <c>TrueString</c>
211 public override string ToString ()
213 return m_value
? TrueString
: FalseString
;
216 // =========== IConvertible Methods =========== //
217 public TypeCode
GetTypeCode ()
219 return TypeCode
.Boolean
;
222 object IConvertible
.ToType (Type targetType
, IFormatProvider provider
)
224 if (targetType
== null)
225 throw new ArgumentNullException ("targetType");
226 return System
.Convert
.ToType (m_value
, targetType
, provider
, false);
229 bool IConvertible
.ToBoolean (IFormatProvider provider
)
234 byte IConvertible
.ToByte (IFormatProvider provider
)
236 return System
.Convert
.ToByte (m_value
);
239 char IConvertible
.ToChar (IFormatProvider provider
)
241 throw new InvalidCastException ();
244 DateTime IConvertible
.ToDateTime (IFormatProvider provider
)
246 throw new InvalidCastException ();
249 decimal IConvertible
.ToDecimal (IFormatProvider provider
)
251 return System
.Convert
.ToDecimal (m_value
);
254 double IConvertible
.ToDouble (IFormatProvider provider
)
256 return System
.Convert
.ToDouble (m_value
);
259 short IConvertible
.ToInt16 (IFormatProvider provider
)
261 return System
.Convert
.ToInt16 (m_value
);
264 int IConvertible
.ToInt32 (IFormatProvider provider
)
266 return System
.Convert
.ToInt32 (m_value
);
269 long IConvertible
.ToInt64 (IFormatProvider provider
)
271 return System
.Convert
.ToInt64 (m_value
);
274 sbyte IConvertible
.ToSByte (IFormatProvider provider
)
276 return System
.Convert
.ToSByte (m_value
);
279 float IConvertible
.ToSingle (IFormatProvider provider
)
281 return System
.Convert
.ToSingle (m_value
);
284 public string ToString (IFormatProvider provider
)
289 ushort IConvertible
.ToUInt16 (IFormatProvider provider
)
291 return System
.Convert
.ToUInt16 (m_value
);
294 uint IConvertible
.ToUInt32 (IFormatProvider provider
)
296 return System
.Convert
.ToUInt32 (m_value
);
299 ulong IConvertible
.ToUInt64 (IFormatProvider provider
)
301 return System
.Convert
.ToUInt64 (m_value
);