2009-02-27 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mcs / class / corlib / System / UInt16.cs
1 //
2 // System.UInt16.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // (C) Ximian, Inc.  http://www.ximian.com
8 // Copyright (C) 2004 Novell (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31
32 namespace System
33 {
34         [Serializable]
35         [CLSCompliant (false)]
36 #if NET_2_0
37         [System.Runtime.InteropServices.ComVisible (true)]
38 #endif
39         public struct UInt16 : IFormattable, IConvertible, IComparable
40 #if NET_2_0
41                 , IComparable<UInt16>, IEquatable <UInt16>
42 #endif
43         {
44                 public const ushort MaxValue = 0xffff;
45                 public const ushort MinValue = 0;
46
47                 internal ushort m_value;
48
49                 public int CompareTo (object value)
50                 {
51                         if (value == null)
52                                 return 1;
53
54                         if(!(value is System.UInt16))
55                                 throw new ArgumentException (Locale.GetText ("Value is not a System.UInt16."));
56
57                         return this.m_value - ((ushort) value);
58                 }
59
60                 public override bool Equals (object obj)
61                 {
62                         if (!(obj is System.UInt16))
63                                 return false;
64
65                         return ((ushort) obj) == m_value;
66                 }
67
68                 public override int GetHashCode ()
69                 {
70                         return m_value;
71                 }
72
73 #if NET_2_0
74                 public int CompareTo (ushort value)
75                 {
76                         return m_value - value;
77                 }
78
79                 public bool Equals (ushort obj)
80                 {
81                         return obj == m_value;
82                 }
83 #endif
84
85
86                 [CLSCompliant (false)]
87                 public static ushort Parse (string s, IFormatProvider provider)
88                 {
89                         return Parse (s, NumberStyles.Integer, provider);
90                 }
91
92                 [CLSCompliant (false)]
93                 public static ushort Parse (string s, NumberStyles style)
94                 {
95                         return Parse (s, style, null);
96                 }
97
98                 [CLSCompliant (false)]
99                 public static ushort Parse (string s, NumberStyles style, IFormatProvider provider)
100                 {
101                         uint tmpResult = UInt32.Parse (s, style, provider);
102                         if (tmpResult > UInt16.MaxValue)
103                                 throw new OverflowException (Locale.GetText ("Value too large."));
104
105                         return (ushort) tmpResult;
106                 }
107
108                 [CLSCompliant(false)]
109                 public static ushort Parse (string s) 
110                 {
111                         return Parse (s, NumberStyles.Number, null);
112                 }
113
114 #if NET_2_0
115                 [CLSCompliant(false)]
116                 public static bool TryParse (string s, out ushort result) 
117                 {
118                         return TryParse (s, NumberStyles.Integer, null, out result);
119                 }
120
121                 [CLSCompliant(false)]
122                 public static bool TryParse (string s, NumberStyles style, IFormatProvider provider, out ushort result) 
123                 {
124                         uint tmpResult;
125                         result = 0;
126                                 
127                         if (!UInt32.TryParse (s, style, provider, out tmpResult))
128                                 return false;
129                                 
130                         if (tmpResult > UInt16.MaxValue)
131                                 return false;
132                                 
133                         result = (ushort)tmpResult;
134                         return true;
135                 }
136 #endif
137
138                 public override string ToString ()
139                 {
140                         return NumberFormatter.NumberToString (m_value, null);
141                 }
142
143                 public string ToString (IFormatProvider provider)
144                 {
145                         return NumberFormatter.NumberToString (m_value, provider);
146                 }
147
148                 public string ToString (string format)
149                 {
150                         return ToString (format, null);
151                 }
152
153                 public string ToString (string format, IFormatProvider provider)
154                 {
155                         return NumberFormatter.NumberToString (format, m_value, provider);
156                 }
157
158                 // =========== IConvertible Methods =========== //
159                 public TypeCode GetTypeCode ()
160                 {
161                         return TypeCode.UInt16;
162                 }
163
164                 bool IConvertible.ToBoolean (IFormatProvider provider)
165                 {
166                         return System.Convert.ToBoolean (m_value);
167                 }
168
169                 byte IConvertible.ToByte (IFormatProvider provider)
170                 {
171                         return System.Convert.ToByte (m_value);
172                 }
173
174                 char IConvertible.ToChar (IFormatProvider provider)
175                 {
176                         return System.Convert.ToChar (m_value);
177                 }
178
179                 DateTime IConvertible.ToDateTime (IFormatProvider provider)
180                 {
181                         return System.Convert.ToDateTime (m_value);
182                 }
183
184                 decimal IConvertible.ToDecimal (IFormatProvider provider)
185                 {
186                         return System.Convert.ToDecimal (m_value);
187                 }
188
189                 double IConvertible.ToDouble (IFormatProvider provider)
190                 {
191                         return System.Convert.ToDouble (m_value);
192                 }
193
194                 short IConvertible.ToInt16 (IFormatProvider provider)
195                 {
196                         return System.Convert.ToInt16 (m_value);
197                 }
198
199                 int IConvertible.ToInt32 (IFormatProvider provider)
200                 {
201                         return System.Convert.ToInt32 (m_value);
202                 }
203
204                 long IConvertible.ToInt64 (IFormatProvider provider)
205                 {
206                         return System.Convert.ToInt64 (m_value);
207                 }
208
209                 sbyte IConvertible.ToSByte (IFormatProvider provider)
210                 {
211                         return System.Convert.ToSByte (m_value);
212                 }
213
214                 float IConvertible.ToSingle (IFormatProvider provider)
215                 {
216                         return System.Convert.ToSingle (m_value);
217                 }
218
219                 object IConvertible.ToType (Type type, IFormatProvider provider)
220                 {
221                         return System.Convert.ToType (m_value, type, provider);
222                 }
223
224 #if ONLY_1_1
225 #pragma warning disable 3019
226                 [CLSCompliant (false)]
227 #endif
228                 ushort IConvertible.ToUInt16 (IFormatProvider provider)
229                 {
230                         return m_value;
231                 }
232 #if ONLY_1_1
233 #pragma warning restore 3019
234 #endif
235
236 #if ONLY_1_1
237 #pragma warning disable 3019
238                 [CLSCompliant (false)]
239 #endif
240                 uint IConvertible.ToUInt32 (IFormatProvider provider)
241                 {
242                         return System.Convert.ToUInt32 (m_value);
243                 }
244 #if ONLY_1_1
245 #pragma warning restore 3019
246 #endif
247
248 #if ONLY_1_1
249 #pragma warning disable 3019
250                 [CLSCompliant (false)]
251 #endif
252                 ulong IConvertible.ToUInt64 (IFormatProvider provider)
253                 {
254                         return System.Convert.ToUInt64 (m_value);
255                 }
256         }
257 #if ONLY_1_1
258 #pragma warning restore 3019
259 #endif
260 }