Bump corefx
[mono.git] / mcs / class / referencesource / mscorlib / system / int16.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 /*============================================================
7 **
8 ** Class:  Int16.cs
9 **
10 **
11 ** Purpose: This class will encapsulate a short and provide an
12 **          Object representation of it.
13 **
14 ** 
15 ===========================================================*/
16
17 namespace System {
18     
19     using System;
20     using System.Globalization;
21 ///#if GENERICS_WORK
22 ///    using System.Numerics;
23 ///#endif
24     using System.Runtime.InteropServices;
25     using System.Diagnostics.Contracts;
26
27 [System.Runtime.InteropServices.ComVisible(true)]
28 [Serializable]
29 [System.Runtime.InteropServices.StructLayout(LayoutKind.Sequential)]
30 #if GENERICS_WORK
31     public struct Int16 : IComparable, IFormattable, IConvertible
32         , IComparable<Int16>, IEquatable<Int16>
33 ///     , IArithmetic<Int16>
34 #else
35     public struct Int16 : IComparable, IFormattable, IConvertible
36 #endif
37     {
38         internal short m_value;
39     
40         public const short MaxValue = (short)0x7FFF;
41         public const short MinValue = unchecked((short)0x8000);
42         
43         // Compares this object to another object, returning an integer that
44         // indicates the relationship. 
45         // Returns a value less than zero if this  object
46         // null is considered to be less than any instance.
47         // If object is not of type Int16, this method throws an ArgumentException.
48         // 
49         public int CompareTo(Object value) {
50             if (value == null) {
51                 return 1;
52             }
53     
54             if (value is Int16) {
55                 return m_value - ((Int16)value).m_value;
56             }
57     
58             throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt16"));
59         }
60
61         public int CompareTo(Int16 value) {
62             return m_value - value;
63         }
64     
65         public override bool Equals(Object obj) {
66             if (!(obj is Int16)) {
67                 return false;
68             }
69             return m_value == ((Int16)obj).m_value;
70         }
71
72         [System.Runtime.Versioning.NonVersionable]
73         public bool Equals(Int16 obj)
74         {
75             return m_value == obj;
76         }
77
78         // Returns a HashCode for the Int16
79         public override int GetHashCode() {
80             return ((int)((ushort)m_value) | (((int)m_value) << 16));
81         }
82     
83
84         [System.Security.SecuritySafeCritical]  // auto-generated
85         public override String ToString() {
86             Contract.Ensures(Contract.Result<String>() != null);
87             return Number.FormatInt32(m_value, null, NumberFormatInfo.CurrentInfo);
88         }
89         
90         [System.Security.SecuritySafeCritical]  // auto-generated
91         public String ToString(IFormatProvider provider) {
92             Contract.Ensures(Contract.Result<String>() != null);
93             return Number.FormatInt32(m_value, null, NumberFormatInfo.GetInstance(provider));
94         }        
95
96         public String ToString(String format) {
97             Contract.Ensures(Contract.Result<String>() != null);
98             return ToString(format, NumberFormatInfo.CurrentInfo);
99         }        
100     
101         public String ToString(String format, IFormatProvider provider) {
102             Contract.Ensures(Contract.Result<String>() != null);
103             return ToString(format, NumberFormatInfo.GetInstance(provider));
104         }
105
106         [System.Security.SecuritySafeCritical]  // auto-generated
107         private String ToString(String format, NumberFormatInfo info) {
108             Contract.Ensures(Contract.Result<String>() != null);
109
110             if (m_value<0 && format!=null && format.Length>0 && (format[0]=='X' || format[0]=='x')) {
111                 uint temp = (uint)(m_value & 0x0000FFFF);
112                 return Number.FormatUInt32(temp,format, info);
113             }
114             return Number.FormatInt32(m_value, format, info);
115         }
116     
117         public static short Parse(String s) {
118             return Parse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
119         }
120     
121         public static short Parse(String s, NumberStyles style) {
122             NumberFormatInfo.ValidateParseStyleInteger(style);
123             return Parse(s, style, NumberFormatInfo.CurrentInfo);
124         }
125
126         public static short Parse(String s, IFormatProvider provider) {
127             return Parse(s, NumberStyles.Integer, NumberFormatInfo.GetInstance(provider));
128         }
129
130         public static short Parse(String s, NumberStyles style, IFormatProvider provider) {
131             NumberFormatInfo.ValidateParseStyleInteger(style);
132             return Parse(s, style, NumberFormatInfo.GetInstance(provider));
133         }
134         
135         private static short Parse(String s, NumberStyles style, NumberFormatInfo info) {        
136
137             int i = 0;
138             try {
139                 i = Number.ParseInt32(s, style, info);
140             }
141             catch(OverflowException e) {
142                 throw new OverflowException(Environment.GetResourceString("Overflow_Int16"), e);
143             }
144
145             // We need this check here since we don't allow signs to specified in hex numbers. So we fixup the result
146             // for negative numbers
147             if ((style & NumberStyles.AllowHexSpecifier) != 0) { // We are parsing a hexadecimal number
148                 if ((i < 0) || (i > UInt16.MaxValue)) {
149                     throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
150                 }
151                 return (short)i;
152             }
153                 
154             if (i < MinValue || i > MaxValue) throw new OverflowException(Environment.GetResourceString("Overflow_Int16"));
155             return (short)i;
156         }
157
158         public static bool TryParse(String s, out Int16 result) {
159             return TryParse(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
160         }
161
162         public static bool TryParse(String s, NumberStyles style, IFormatProvider provider, out Int16 result) {
163             NumberFormatInfo.ValidateParseStyleInteger(style);
164             return TryParse(s, style, NumberFormatInfo.GetInstance(provider), out result);
165         }
166         
167         private static bool TryParse(String s, NumberStyles style, NumberFormatInfo info, out Int16 result) {
168
169             result = 0;
170             int i;
171             if (!Number.TryParseInt32(s, style, info, out i)) {
172                 return false;
173             }
174
175             // We need this check here since we don't allow signs to specified in hex numbers. So we fixup the result
176             // for negative numbers
177             if ((style & NumberStyles.AllowHexSpecifier) != 0) { // We are parsing a hexadecimal number
178                 if ((i < 0) || i > UInt16.MaxValue) {
179                     return false;
180                 }
181                 result = (Int16) i;
182                 return true;
183             }
184                 
185             if (i < MinValue || i > MaxValue) {
186                 return false;
187             }
188             result = (Int16) i;
189             return true;
190         }
191
192         //
193         // IConvertible implementation
194         // 
195         
196         public TypeCode GetTypeCode() {
197             return TypeCode.Int16;
198         }
199
200
201         /// <internalonly/>
202         bool IConvertible.ToBoolean(IFormatProvider provider) {
203             return Convert.ToBoolean(m_value);
204         }
205
206         /// <internalonly/>
207         char IConvertible.ToChar(IFormatProvider provider) {
208             return Convert.ToChar(m_value);
209         }
210
211         /// <internalonly/>
212         sbyte IConvertible.ToSByte(IFormatProvider provider) {
213             return Convert.ToSByte(m_value);
214         }
215
216         /// <internalonly/>
217         byte IConvertible.ToByte(IFormatProvider provider) {
218             return Convert.ToByte(m_value);
219         }
220
221         /// <internalonly/>
222         short IConvertible.ToInt16(IFormatProvider provider) {
223             return m_value;
224         }
225
226         /// <internalonly/>
227         ushort IConvertible.ToUInt16(IFormatProvider provider) {
228             return Convert.ToUInt16(m_value);
229         }
230
231         /// <internalonly/>
232         int IConvertible.ToInt32(IFormatProvider provider) {
233             return Convert.ToInt32(m_value);
234         }
235
236         /// <internalonly/>
237         uint IConvertible.ToUInt32(IFormatProvider provider) {
238             return Convert.ToUInt32(m_value);
239         }
240
241         /// <internalonly/>
242         long IConvertible.ToInt64(IFormatProvider provider) {
243             return Convert.ToInt64(m_value);
244         }
245
246         /// <internalonly/>
247         ulong IConvertible.ToUInt64(IFormatProvider provider) {
248             return Convert.ToUInt64(m_value);
249         }
250
251         /// <internalonly/>
252         float IConvertible.ToSingle(IFormatProvider provider) {
253             return Convert.ToSingle(m_value);
254         }
255
256         /// <internalonly/>
257         double IConvertible.ToDouble(IFormatProvider provider) {
258             return Convert.ToDouble(m_value);
259         }
260
261         /// <internalonly/>
262         Decimal IConvertible.ToDecimal(IFormatProvider provider) {
263             return Convert.ToDecimal(m_value);
264         }
265
266         /// <internalonly/>
267         DateTime IConvertible.ToDateTime(IFormatProvider provider) {
268             throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Int16", "DateTime"));
269         }
270
271         /// <internalonly/>
272         Object IConvertible.ToType(Type type, IFormatProvider provider) {
273             return Convert.DefaultToType((IConvertible)this, type, provider);
274         }
275
276 ///#if GENERICS_WORK
277 ///        //
278 ///        // IArithmetic<Int16> implementation
279 ///        //
280 ///
281 ///        /// <internalonly/>
282 ///        Int16 IArithmetic<Int16>.AbsoluteValue(out bool overflowed) {
283 ///            overflowed = (m_value == MinValue);  // -m_value overflows
284 ///            return (Int16) (m_value < 0 ? -m_value : m_value);
285 ///        }
286 ///
287 ///        /// <internalonly/>
288 ///        Int16 IArithmetic<Int16>.Negate(out bool overflowed) {
289 ///            overflowed = (m_value == MinValue); // Negate(MinValue) overflows
290 ///            return (Int16) (-m_value);
291 ///        }
292 ///
293 ///        /// <internalonly/>
294 ///        Int16 IArithmetic<Int16>.Sign(out bool overflowed) {
295 ///            overflowed = false;
296 ///            return (Int16) (m_value >= 0 ? (m_value == 0 ? 0 : 1) : -1);
297 ///        }
298 ///
299 ///        /// <internalonly/>
300 ///        Int16 IArithmetic<Int16>.Add(Int16 addend, out bool overflowed) {
301 ///            int i = ((int)m_value) + addend;
302 ///            overflowed = (i > MaxValue || i < MinValue);
303 ///            return (Int16) i;
304 ///        }
305 ///
306 ///        /// <internalonly/>
307 ///        Int16 IArithmetic<Int16>.Subtract(Int16 subtrahend, out bool overflowed) {
308 ///            int i = ((int)m_value)- subtrahend;
309 ///            overflowed = (i > MaxValue || i < MinValue);
310 ///            return (Int16) i;
311 ///        }
312 ///
313 ///        /// <internalonly/>
314 ///        Int16 IArithmetic<Int16>.Multiply(Int16 multiplier, out bool overflowed) {
315 ///            long l = ((long)m_value) * multiplier;
316 ///            overflowed = (l > MaxValue || l < MinValue);
317 ///            return (Int16) l;
318 ///        }
319 ///
320 ///
321 ///        /// <internalonly/>
322 ///        Int16 IArithmetic<Int16>.Divide(Int16 divisor, out bool overflowed) {
323 ///            // signed integer division can overflow.  Consider the following
324 ///            // 8-bit case: -128/-1 = 128.
325 ///            // 128 won't fit into a signed 8-bit integer, instead you will end up
326 ///            // with -128.
327 ///            //
328 ///            // Because of this corner case, we must check if the numerator
329 ///            // is MinValue and if the denominator is -1.
330 ///
331 ///            overflowed = (divisor == -1 && m_value == MinValue);
332 ///            return (Int16) unchecked(m_value / divisor);
333 ///        }
334 ///
335 ///        /// <internalonly/>
336 ///        Int16 IArithmetic<Int16>.DivideRemainder(Int16 divisor, out Int16 remainder, out bool overflowed) {
337 ///            remainder = (Int16) (m_value % divisor);
338 ///            overflowed = (divisor == -1 && m_value == MinValue);
339 ///            return (Int16) unchecked(m_value / divisor);
340 ///        }
341 ///
342 ///        /// <internalonly/>
343 ///        Int16 IArithmetic<Int16>.Remainder(Int16 divisor, out bool overflowed) {
344 ///            overflowed = false;
345 ///            return (Int16) (m_value % divisor);
346 ///        }
347 ///
348 ///        /// <internalonly/>
349 ///        ArithmeticDescriptor<Int16> IArithmetic<Int16>.GetDescriptor() {
350 ///            if (s_descriptor == null) {
351 ///                s_descriptor = new Int16ArithmeticDescriptor( ArithmeticCapabilities.One
352 ///                                                             | ArithmeticCapabilities.Zero
353 ///                                                             | ArithmeticCapabilities.MaxValue
354 ///                                                             | ArithmeticCapabilities.MinValue);
355 ///            }
356 ///            return s_descriptor;
357 ///        }
358 ///
359 ///        private static Int16ArithmeticDescriptor s_descriptor;
360 /// 
361 ///        class Int16ArithmeticDescriptor : ArithmeticDescriptor<Int16> {
362 ///            public Int16ArithmeticDescriptor(ArithmeticCapabilities capabilities) : base(capabilities) {}
363 ///
364 ///            public override Int16 One {
365 ///                get {
366 ///                    return (Int16) 1;
367 ///                }
368 ///            }
369 ///
370 ///            public override Int16 Zero {
371 ///                get {
372 ///                    return (Int16) 0;
373 ///                }
374 ///            }
375 ///
376 ///            public override Int16 MinValue {
377 ///                get {
378 ///                    return Int16.MinValue;
379 ///                }
380 ///            }
381 ///
382 ///            public override Int16 MaxValue {
383 ///                get {
384 ///                    return Int16.MaxValue;
385 ///                }
386 ///            }
387 ///        }
388 ///#endif // #if GENERICS_WORK
389
390
391     }
392 }