2009-03-14 Miguel de Icaza <miguel@novell.com>
[mono.git] / mcs / class / corlib / System / Boolean.cs
1 //
2 // System.Boolean.cs
3 //
4 // Author:
5 //   Derek Holden (dholden@draper.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
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:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
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.
31 //
32 // Testing SVN 
33 //
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.
37 //
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.
41 //
42
43 using System.Globalization;
44 using System.Runtime.InteropServices;
45
46 namespace System
47 {
48         /// <summary>
49         /// Represents the boolean values of logical true and false.
50         /// </summary>
51         [Serializable]
52 #if NET_2_0
53         [ComVisible (true)]
54 #endif
55         public struct Boolean : IComparable, IConvertible
56 #if NET_2_0
57                 , IComparable <bool>, IEquatable <bool>
58 #endif
59         {
60                 /// <value>
61                 /// The String representation of Boolean False
62                 /// </value>    
63                 public static readonly string FalseString = "False";
64
65                 /// <value>
66                 /// The String representation of Boolean True
67                 /// </value>    
68                 public static readonly string TrueString = "True";
69
70                 internal bool m_value;
71
72                 /// <summary>
73                 /// Compares the current Boolean instance against another object.
74                 /// </summary>
75                 /// <remarks>
76                 /// Throws an ArgumentException if <c>obj</c> isn't null or 
77                 /// a Boolean.
78                 /// </remarks>
79                 /// <param name="obj">
80                 /// The object to compare against
81                 /// </param>
82                 /// <returns>
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
89                 /// </returns>
90                 public int CompareTo (object obj)
91                 {
92                         if (obj == null)
93                                 return 1;
94
95                         if (!(obj is System.Boolean))
96                                 throw new ArgumentException (Locale.GetText (
97                                         "Object is not a Boolean."));
98
99                         bool value = (bool) obj;
100
101                         // for case #3
102                         if (m_value && !value)
103                                 return 1;
104
105                         // for case #2, else it's #1
106                         return (m_value == value) ? 0 : -1;
107                 }
108
109                 /// <summary>
110                 /// Determines whether this instance and another object represent the
111                 /// same type and value.
112                 /// </summary>
113                 /// <param name="obj">
114                 /// The object to check against
115                 /// </param>
116                 /// <returns>
117                 /// true if this instnace and <c>obj</c> are same value, 
118                 /// otherwise false if it is not or null
119                 /// </returns>
120                 public override bool Equals (Object obj)
121                 {
122                         if (obj == null || !(obj is System.Boolean))
123                                 return false;
124
125                         bool value = (bool) obj;
126
127                         return m_value ? value : !value;
128                 }
129
130 #if NET_2_0
131                 public int CompareTo (bool value)
132                 {
133                         if (m_value == value)
134                                 return 0;
135                         return !m_value ? -1 : 1;
136                 }
137
138                 public bool Equals (bool obj)
139                 {
140                         return m_value == obj;
141                 }
142 #endif
143
144                 /// <summary>
145                 /// Generates a hashcode for this object.
146                 /// </summary>
147                 /// <returns>
148                 /// An Int32 value holding the hash code
149                 /// </returns>
150                 public override int GetHashCode ()
151                 {
152                         // Guess there's not too many ways to hash a Boolean
153                         return m_value ? 1 : 0;
154                 }
155
156                 /// <summary>
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.
160                 /// </summary>
161                 /// <remarks>
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>
165                 /// </remarks>
166                 /// <param name="val">
167                 /// The string value to parse
168                 /// </param>
169                 /// <returns>
170                 /// true if <c>val</c> is equivalent to TrueString, 
171                 /// otherwise false
172                 /// </returns>
173                 public static bool Parse (string value)
174                 {
175                         if (value == null)
176                                 throw new ArgumentNullException ("value");
177
178                         value = value.Trim ();
179
180                         if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0)
181                                 return true;
182
183                         if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0)
184                                 return false;
185
186                         throw new FormatException (Locale.GetText (
187                                 "Value is not equivalent to either TrueString or FalseString."));
188                 }
189
190 #if NET_2_0
191                 public static bool TryParse (string value, out bool result)
192                 {
193                         result = false;
194                         if (value == null)
195                                 return false;
196
197                         value = value.Trim ();
198
199                         if (String.Compare (value, TrueString, true, CultureInfo.InvariantCulture) == 0) {
200                                 result = true;
201                                 return true;
202                         }
203
204                         if (String.Compare (value, FalseString, true, CultureInfo.InvariantCulture) == 0) {
205                                 // result = false; // already set at false by default
206                                 return true;
207                         }
208
209                         return false;
210                 }
211 #endif
212
213                 /// <summary>
214                 /// Returns a string representation of this Boolean object.
215                 /// </summary>
216                 /// <returns>
217                 /// <c>FalseString</c> if the instance value is false, otherwise 
218                 /// <c>TrueString</c>
219                 /// </returns>
220                 public override string ToString ()
221                 {
222                         return m_value ? TrueString : FalseString;
223                 }
224
225                 // =========== IConvertible Methods =========== //
226                 public TypeCode GetTypeCode ()
227                 {
228                         return TypeCode.Boolean;
229                 }
230
231                 object IConvertible.ToType (Type type, IFormatProvider provider)
232                 {
233                         return System.Convert.ToType (m_value, type, provider, false);
234                 }
235
236                 bool IConvertible.ToBoolean (IFormatProvider provider)
237                 {
238                         return m_value;
239                 }
240
241                 byte IConvertible.ToByte (IFormatProvider provider)
242                 {
243                         return System.Convert.ToByte (m_value);
244                 }
245
246                 char IConvertible.ToChar (IFormatProvider provider)
247                 {
248                         throw new InvalidCastException ();
249                 }
250
251                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
252                 {
253                         throw new InvalidCastException ();
254                 }
255
256                 decimal IConvertible.ToDecimal (IFormatProvider provider)
257                 {
258                         return System.Convert.ToDecimal (m_value);
259                 }
260
261                 double IConvertible.ToDouble (IFormatProvider provider)
262                 {
263                         return System.Convert.ToDouble (m_value);
264                 }
265
266                 short IConvertible.ToInt16 (IFormatProvider provider)
267                 {
268                         return System.Convert.ToInt16 (m_value);
269                 }
270
271                 int IConvertible.ToInt32 (IFormatProvider provider)
272                 {
273                         return System.Convert.ToInt32 (m_value);
274                 }
275
276                 long IConvertible.ToInt64 (IFormatProvider provider)
277                 {
278                         return System.Convert.ToInt64 (m_value);
279                 }
280
281 #if ONLY_1_1
282 #pragma warning disable 3019
283                 [CLSCompliant (false)]
284 #endif
285                 sbyte IConvertible.ToSByte (IFormatProvider provider)
286                 {
287                         return System.Convert.ToSByte (m_value);
288                 }
289 #if ONLY_1_1
290 #pragma warning restore 3019
291 #endif
292
293                 float IConvertible.ToSingle (IFormatProvider provider)
294                 {
295                         return System.Convert.ToSingle (m_value);
296                 }
297
298                 public string ToString (IFormatProvider provider)
299                 {
300                         return ToString ();
301                 }
302
303 #if ONLY_1_1
304 #pragma warning disable 3019
305                 [CLSCompliant (false)]
306 #endif
307                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
308                 {
309                         return System.Convert.ToUInt16 (m_value);
310                 }
311 #if ONLY_1_1
312 #pragma warning restore 3019
313 #endif
314
315 #if ONLY_1_1
316 #pragma warning disable 3019
317                 [CLSCompliant (false)]
318 #endif
319                 uint IConvertible.ToUInt32 (IFormatProvider provider)
320                 {
321                         return System.Convert.ToUInt32 (m_value);
322                 }
323 #if ONLY_1_1
324 #pragma warning restore 3019
325 #endif
326
327 #if ONLY_1_1
328 #pragma warning disable 3019
329                 [CLSCompliant (false)]
330 #endif
331                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
332                 {
333                         return System.Convert.ToUInt64 (m_value);
334                 }
335 #if ONLY_1_1
336 #pragma warning restore 3019
337 #endif
338         }
339 }