Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / corlib / System / Nullable.cs
index 8fa79e4453fd838d2022ae74e3cfe73d7ec7ab6b..11f6c67dbd4e4d73c69b0a3b4a007025069ea7bd 100644 (file)
@@ -1,7 +1,8 @@
 //
-// System.Nullable
+// System.Nullable.cs
 //
 // Martin Baulig (martin@ximian.com)
+// Marek Safar  (marek.safar@gmail.com)
 //
 // (C) 2004 Novell, Inc.
 //
 //
 
 using System.Reflection;
-#if NET_2_0
 using System.Collections.Generic;
-#endif
 using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+using System.Diagnostics;
 
-#if NET_2_0
 namespace System
 {
+       [ComVisible (true)]
+       public static class Nullable {
 
-       [CLSCompliant(false)]
-       public static class Nullable
-       {
-               public static int Compare<T> (Nullable<T> left, Nullable<T> right)
-               {
-                       IComparable icomparable = left.value as IComparable;
-                       if (icomparable == null)
-                               throw new ArgumentException ("At least one object must implement IComparable.");
-                       if (left.has_value == false && right.has_value == false)
-                               return 0;
-                       if (!left.has_value)
-                               return -1;
-                       if (!right.has_value)
-                               return 1;
-
-                       return icomparable.CompareTo (right.value);
-               }
-
-               public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2)
-               {
-                       return value1.Equals (value2);
-               }
-
-               public static Nullable<T> FromObject<T> (object value)
+#if NET_2_1
+               [ComVisible (false)]
+#endif
+               public static int Compare<T> (T? n1, T? n2) where T: struct
                {
-                       if (!(value is T))
-                               throw new ArgumentException ("Object type can not be converted to target type.");
-
-                       return new Nullable<T> ((T) value);
-               }
+                       if (n1.has_value) {
+                               if (!n2.has_value)
+                                       return 1;
 
-               public static T GetValueOrDefault<T>(Nullable<T> value)
-               {
-                       return GetValueOrDefault<T> (value, default (T));
+                               return Comparer<T>.Default.Compare (n1.value, n2.value);
+                       }
+                       
+                       return n2.has_value ? -1 : 0;
                }
 
-               public static T GetValueOrDefault<T> (Nullable<T> value, T defaultValue)
+#if NET_2_1
+               [ComVisible (false)]
+#endif
+               public static bool Equals<T> (T? n1, T? n2) where T: struct
                {
-                       if (!value.has_value)
-                               return defaultValue;
+                       if (n1.has_value != n2.has_value)
+                               return false;
 
-                       return value.value;
-               }
+                       if (!n1.has_value)
+                               return true;
 
-               public static bool HasValue<T> (Nullable <T> value)
-               {
-                       return value.has_value;
+                       return EqualityComparer<T>.Default.Equals (n1.value, n2.value);
                }
 
-               public static object ToObject<T> (Nullable<T> value)
+               public static Type GetUnderlyingType (Type nullableType)
                {
-                       if (!value.has_value)
-                               return null;
+                       if (nullableType == null)
+                               throw new ArgumentNullException ("nullableType");
 
-                       return (object)value.value;
+                       return nullableType.IsGenericType && !nullableType.IsGenericTypeDefinition && nullableType.GetGenericTypeDefinition () == typeof(Nullable<>) ?
+                               nullableType.GetGenericArguments () [0] : null;
                }
        }
-       
-       [CLSCompliant(false)]
-       public struct Nullable<T> : IComparable, INullableValue
+
+       [Serializable]
+       [DebuggerStepThrough]
+       public struct Nullable<T> where T: struct
        {
+               #region Sync with runtime code
                internal T value;
                internal bool has_value;
+               #endregion
 
                public Nullable (T value)
                {
-                       if (value == null)
-                               this.has_value = false;
-                       else
-                               this.has_value = true;
-                       
+                       this.has_value = true;
                        this.value = value;
                }
 
@@ -126,36 +108,17 @@ namespace System
                        }
                }
 
-               object INullableValue.Value {
-                       get {
-                               return (object)Value;
-                       }
-               }
-               
-               [Obsolete]
-               public int CompareTo (Nullable<T> other)
-               {
-                       return Nullable.Compare<T> (this, other);
-               }
-
-               [Obsolete]
-               public int CompareTo (object other)
-               {
-                       if (!(other is Nullable<T>))
-                               throw new ArgumentException ("Object type can not be converted to target type.");
-
-                       return Nullable.Compare<T> (this, (Nullable<T>) other);
-               }
-
                public override bool Equals (object other)
                {
+                       if (other == null)
+                               return has_value == false;
                        if (!(other is Nullable<T>))
                                return false;
 
                        return Equals ((Nullable <T>) other);
                }
 
-               public bool Equals (Nullable<T> other)
+               bool Equals (Nullable<T> other)
                {
                        if (other.has_value != has_value)
                                return false;
@@ -166,12 +129,6 @@ namespace System
                        return other.value.Equals (value);
                }
 
-               [Obsolete]
-               public static Nullable<T> FromObject (object value)
-               {
-                       return Nullable.FromObject<T> (value);
-               }
-
                public override int GetHashCode ()
                {
                        if (!has_value)
@@ -180,24 +137,14 @@ namespace System
                        return value.GetHashCode ();
                }
 
-               [Obsolete]
                public T GetValueOrDefault ()
                {
-                       return Nullable.GetValueOrDefault<T> (this, default (T));
+                       return value;
                }
 
-               [Obsolete]
-               public T GetValueOrDefault (T def_value)
+               public T GetValueOrDefault (T defaultValue)
                {
-                       return Nullable.GetValueOrDefault<T> (this, def_value);
-               }
-
-               public object ToObject ()
-               {
-                       if (!has_value)
-                               return null;
-
-                       return (object)value;
+                       return has_value ? value : defaultValue;
                }
 
                public override string ToString ()
@@ -205,7 +152,7 @@ namespace System
                        if (has_value)
                                return value.ToString ();
                        else
-                               return "";
+                               return String.Empty;
                }
 
                public static implicit operator Nullable<T> (T value)
@@ -218,15 +165,27 @@ namespace System
                        return value.Value;
                }
 
-               public static bool operator == (Nullable<T> left, Nullable<T> right)
+               //
+               // These are called by the JIT
+               //
+#pragma warning disable 169
+               //
+               // JIT implementation of box valuetype System.Nullable`1<T>
+               //
+               static object Box (T? o)
                {
-                       return left.Equals (right);
+                       if (!o.has_value)
+                               return null;
+                               
+                       return o.value;
                }
-
-               public static bool operator != (Nullable<T> left, Nullable<T> right)
+               
+               static T? Unbox (object o)
                {
-                       return !left.Equals (right);
+                       if (o == null)
+                               return null;
+                       return (T) o;
                }
+#pragma warning restore 169
        }
 }
-#endif