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