Merge pull request #3565 from vargaz/no-free-imt-thunks
[mono.git] / mcs / class / referencesource / mscorlib / system / boolean.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  Boolean
9 **
10 **
11 ** Purpose: The boolean class serves as a wrapper for the primitive
12 ** type boolean.
13 **
14 ** 
15 ===========================================================*/
16 namespace System {
17     
18     using System;
19     using System.Globalization;
20     using System.Diagnostics.Contracts;
21     // The Boolean class provides the
22     // object representation of the boolean primitive type.
23     [Serializable]
24 [System.Runtime.InteropServices.ComVisible(true)]
25     public struct Boolean : IComparable, IConvertible
26 #if GENERICS_WORK
27         , IComparable<Boolean>,  IEquatable<Boolean>
28 #endif
29     {
30 #pragma warning disable 649    
31       //
32       // Member Variables
33       //
34       private bool m_value;
35 #pragma warning disable
36
37       // The true value. 
38       // 
39       internal const int True = 1; 
40       
41       // The false value.
42       // 
43       internal const int False = 0; 
44       
45       
46       //
47       // Internal Constants are real consts for performance.
48       //
49
50       // The internal string representation of true.
51       // 
52       internal const String TrueLiteral  = "True";
53       
54       // The internal string representation of false.
55       // 
56       internal const String FalseLiteral = "False";
57       
58
59       //
60       // Public Constants
61       //
62                  
63       // The public string representation of true.
64       // 
65       public static readonly String TrueString  = TrueLiteral;
66       
67       // The public string representation of false.
68       // 
69       public static readonly String FalseString = FalseLiteral;
70
71       //
72       // Overriden Instance Methods
73       //
74       /*=================================GetHashCode==================================
75       **Args:  None
76       **Returns: 1 or 0 depending on whether this instance represents true or false.
77       **Exceptions: None
78       **Overriden From: Value
79       ==============================================================================*/
80       // Provides a hash code for this instance.
81       public override int GetHashCode() {
82           return (m_value)?True:False;
83       }
84     
85       /*===================================ToString===================================
86       **Args: None
87       **Returns:  "True" or "False" depending on the state of the boolean.
88       **Exceptions: None.
89       ==============================================================================*/
90       // Converts the boolean value of this instance to a String.
91       public override String ToString() {
92         if (false == m_value) {
93           return FalseLiteral;
94         }
95         return TrueLiteral;
96       }
97
98       public String ToString(IFormatProvider provider) {
99         if (false == m_value) {
100           return FalseLiteral;
101         }
102         return TrueLiteral;
103       }
104     
105       // Determines whether two Boolean objects are equal.
106       public override bool Equals (Object obj) {
107         //If it's not a boolean, we're definitely not equal
108         if (!(obj is Boolean)) {
109           return false;
110         }
111     
112         return (m_value==((Boolean)obj).m_value);
113       }
114
115       [System.Runtime.Versioning.NonVersionable]
116       public bool Equals(Boolean obj)
117       {
118           return m_value == obj;
119       }
120
121         // Compares this object to another object, returning an integer that
122         // indicates the relationship. For booleans, false sorts before true.
123         // null is considered to be less than any instance.
124         // If object is not of type boolean, this method throws an ArgumentException.
125         // 
126         // Returns a value less than zero if this  object
127         // 
128         public int CompareTo(Object obj) {
129             if (obj==null) {
130                 return 1;
131             }
132             if (!(obj is Boolean)) {
133                 throw new ArgumentException (Environment.GetResourceString("Arg_MustBeBoolean"));
134             }
135              
136             if (m_value==((Boolean)obj).m_value) {
137                 return 0;
138             } else if (m_value==false) {
139                 return -1;
140             }
141             return 1;
142         }
143
144         public int CompareTo(Boolean value) {
145             if (m_value==value) {
146                 return 0;
147             } else if (m_value==false) {
148                 return -1;
149             }
150             return 1;    
151         }
152     
153         //
154         // Static Methods
155         // 
156     
157         // Determines whether a String represents true or false.
158         // 
159         public static Boolean Parse (String value) {
160             if (value==null) throw new ArgumentNullException("value");
161             Contract.EndContractBlock();
162             Boolean result = false;
163             if (!TryParse(value, out result)) {
164                 throw new FormatException(Environment.GetResourceString("Format_BadBoolean"));            
165             }
166             else {
167                 return result;
168             }
169         }
170
171         // Determines whether a String represents true or false.
172         // 
173         public static Boolean TryParse (String value, out Boolean result) {
174             result = false;
175             if (value==null) {
176                 return false;
177             }
178             // For perf reasons, let's first see if they're equal, then do the
179             // trim to get rid of white space, and check again.
180             if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
181                 result = true;
182                 return true;
183             }
184             if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
185                 result = false;
186                 return true;
187             }
188
189             // Special case: Trim whitespace as well as null characters.
190             value = TrimWhiteSpaceAndNull(value);
191
192             if (TrueLiteral.Equals(value, StringComparison.OrdinalIgnoreCase)) {
193                 result = true;
194                 return true;
195             }
196             
197             if (FalseLiteral.Equals(value,StringComparison.OrdinalIgnoreCase)) {
198                 result = false;
199                 return true;
200             }
201             
202             return false;
203         }
204
205         private static String TrimWhiteSpaceAndNull(String value) {
206             int start = 0;
207             int end   = value.Length-1;
208             char nullChar = (char) 0x0000;
209
210             while (start < value.Length) {
211                 if (!Char.IsWhiteSpace(value[start]) && value[start] != nullChar) {
212                     break;
213                 }
214                 start++;
215             }
216
217             while (end >= start) {
218                 if (!Char.IsWhiteSpace(value[end]) && value[end] != nullChar) {
219                     break;
220                 }
221                 end--;            
222             }
223
224             return value.Substring(start, end - start + 1);
225         }       
226
227         //
228         // IConvertible implementation
229         // 
230         
231         public TypeCode GetTypeCode() {
232             return TypeCode.Boolean;
233         }
234
235
236         /// <internalonly/>
237         bool IConvertible.ToBoolean(IFormatProvider provider) {
238             return m_value;
239         }
240
241         /// <internalonly/>
242         char IConvertible.ToChar(IFormatProvider provider) {
243             throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Boolean", "Char"));
244         }
245
246         /// <internalonly/>
247         sbyte IConvertible.ToSByte(IFormatProvider provider) {
248             return Convert.ToSByte(m_value);
249         }
250
251         /// <internalonly/>
252         byte IConvertible.ToByte(IFormatProvider provider) {
253             return Convert.ToByte(m_value);
254         }
255
256         /// <internalonly/>
257         short IConvertible.ToInt16(IFormatProvider provider) {
258             return Convert.ToInt16(m_value);
259         }
260
261         /// <internalonly/>
262         ushort IConvertible.ToUInt16(IFormatProvider provider) {
263             return Convert.ToUInt16(m_value);
264         }
265
266         /// <internalonly/>
267         int IConvertible.ToInt32(IFormatProvider provider) {
268             return Convert.ToInt32(m_value);
269         }
270
271         /// <internalonly/>
272         uint IConvertible.ToUInt32(IFormatProvider provider) {
273             return Convert.ToUInt32(m_value);
274         }
275
276         /// <internalonly/>
277         long IConvertible.ToInt64(IFormatProvider provider) {
278             return Convert.ToInt64(m_value);
279         }
280
281         /// <internalonly/>
282         ulong IConvertible.ToUInt64(IFormatProvider provider) {
283             return Convert.ToUInt64(m_value);
284         }
285
286         /// <internalonly/>
287         float IConvertible.ToSingle(IFormatProvider provider) {
288             return Convert.ToSingle(m_value);
289         }
290
291         /// <internalonly/>
292         double IConvertible.ToDouble(IFormatProvider provider) {
293             return Convert.ToDouble(m_value);
294         }
295
296         /// <internalonly/>
297         Decimal IConvertible.ToDecimal(IFormatProvider provider) {
298             return Convert.ToDecimal(m_value);
299         }
300
301         /// <internalonly/>
302         DateTime IConvertible.ToDateTime(IFormatProvider provider) {
303             throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Boolean", "DateTime"));
304         }
305
306         /// <internalonly/>
307         Object IConvertible.ToType(Type type, IFormatProvider provider) {
308             return Convert.DefaultToType((IConvertible)this, type, provider);
309         }
310     }
311 }