Merge pull request #820 from brendanzagaeski/master
[mono.git] / mcs / class / corlib / System / Nullable.cs
1 //
2 // System.Nullable.cs
3 //
4 // Martin Baulig (martin@ximian.com)
5 // Marek Safar   (marek.safar@gmail.com)
6 //
7 // (C) 2004 Novell, Inc.
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
33 using System.Reflection;
34 using System.Collections.Generic;
35 using System.Runtime.CompilerServices;
36 using System.Runtime.InteropServices;
37 using System.Diagnostics;
38
39 namespace System
40 {
41         [ComVisible (true)]
42         public static class Nullable {
43
44 #if NET_2_1
45                 [ComVisible (false)]
46 #endif
47                 public static int Compare<T> (T? n1, T? n2) where T: struct
48                 {
49                         if (n1.has_value) {
50                                 if (!n2.has_value)
51                                         return 1;
52
53                                 return Comparer<T>.Default.Compare (n1.value, n2.value);
54                         }
55                         
56                         return n2.has_value ? -1 : 0;
57                 }
58
59 #if NET_2_1
60                 [ComVisible (false)]
61 #endif
62                 public static bool Equals<T> (T? n1, T? n2) where T: struct
63                 {
64                         if (n1.has_value != n2.has_value)
65                                 return false;
66
67                         if (!n1.has_value)
68                                 return true;
69
70                         return EqualityComparer<T>.Default.Equals (n1.value, n2.value);
71                 }
72
73                 public static Type GetUnderlyingType (Type nullableType)
74                 {
75                         if (nullableType == null)
76                                 throw new ArgumentNullException ("nullableType");
77
78                         return nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition && nullableType.GetGenericTypeDefinition () == typeof(Nullable<>) ?
79                                 nullableType.GetGenericArguments () [0] : null;
80                 }
81         }
82
83         [Serializable]
84         [DebuggerStepThrough]
85         public struct Nullable<T> where T: struct
86         {
87                 #region Sync with runtime code
88                 internal T value;
89                 internal bool has_value;
90                 #endregion
91
92                 public Nullable (T value)
93                 {
94                         this.has_value = true;
95                         this.value = value;
96                 }
97
98                 public bool HasValue {
99                         get { return has_value; }
100                 }
101
102                 public T Value {
103                         get { 
104                                 if (!has_value)
105                                         throw new InvalidOperationException ("Nullable object must have a value.");
106                                 
107                                 return value; 
108                         }
109                 }
110
111                 public override bool Equals (object other)
112                 {
113                         if (other == null)
114                                 return has_value == false;
115                         if (!(other is Nullable<T>))
116                                 return false;
117
118                         return Equals ((Nullable <T>) other);
119                 }
120
121                 bool Equals (Nullable<T> other)
122                 {
123                         if (other.has_value != has_value)
124                                 return false;
125
126                         if (has_value == false)
127                                 return true;
128
129                         return other.value.Equals (value);
130                 }
131
132                 public override int GetHashCode ()
133                 {
134                         if (!has_value)
135                                 return 0;
136
137                         return value.GetHashCode ();
138                 }
139
140                 public T GetValueOrDefault ()
141                 {
142                         return value;
143                 }
144
145                 public T GetValueOrDefault (T defaultValue)
146                 {
147                         return has_value ? value : defaultValue;
148                 }
149
150                 public override string ToString ()
151                 {
152                         if (has_value)
153                                 return value.ToString ();
154                         else
155                                 return String.Empty;
156                 }
157
158                 public static implicit operator Nullable<T> (T value)
159                 {
160                         return new Nullable<T> (value);
161                 }
162
163                 public static explicit operator T (Nullable<T> value)
164                 {
165                         return value.Value;
166                 }
167
168                 //
169                 // These are called by the JIT
170                 //
171 #pragma warning disable 169
172                 //
173                 // JIT implementation of box valuetype System.Nullable`1<T>
174                 //
175                 static object Box (T? o)
176                 {
177                         if (!o.has_value)
178                                 return null;
179                                 
180                         return o.value;
181                 }
182                 
183                 static T? Unbox (object o)
184                 {
185                         if (o == null)
186                                 return null;
187                         return (T) o;
188                 }
189 #pragma warning restore 169
190         }
191 }