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