Merge pull request #350 from robwilkens/bug1089
[mono.git] / mcs / class / corlib / System / Nullable.cs
index f1c95601dceb6856edd2373be5081590c1174dcf..0d9d5928690bdb5ea239e6d901a134733ade3730 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 {
+namespace System
+{
+       [ComVisible (true)]
        public static class Nullable {
-               public static int Compare<T> (Nullable<T> left, Nullable<T> right)
+
+#if NET_2_1
+               [ComVisible (false)]
+#endif
+               public static int Compare<T> (T? n1, T? n2) where T: struct
                {
-                       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;
+                       if (n1.has_value) {
+                               if (!n2.has_value)
+                                       return 1;
 
-                       return icomparable.CompareTo (right.value);
+                               return Comparer<T>.Default.Compare (n1.value, n2.value);
+                       }
+                       
+                       return n2.has_value ? -1 : 0;
                }
 
-               public static bool Equals<T> (Nullable <T> value1, Nullable<T> value2)
+#if NET_2_1
+               [ComVisible (false)]
+#endif
+               public static bool Equals<T> (T? n1, T? n2) where T: struct
                {
-                       return value1.Equals (value2);
+                       if (n1.has_value != n2.has_value)
+                               return false;
+
+                       if (!n1.has_value)
+                               return true;
+
+                       return EqualityComparer<T>.Default.Equals (n1.value, n2.value);
                }
 
                public static Type GetUnderlyingType (Type nullableType)
                {
                        if (nullableType == null)
                                throw new ArgumentNullException ("nullableType");
-                       if (nullableType.IsGenericInstance && nullableType.GetGenericTypeDefinition () == typeof (Nullable<>))
+                       if (nullableType.IsGenericType && nullableType.GetGenericTypeDefinition () == typeof (Nullable<>))
                                return nullableType.GetGenericArguments ()[0];
                        else
                                return null;
                }
        }
 
-       public struct Nullable<T> : IComparable, INullableValue
+       [Serializable]
+       [DebuggerDisplay ("{DebuggerDisplay}")]
+       [DebuggerStepThrough]
+       public struct Nullable<T> where T: struct
        {
                #region Sync with runtime code
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                internal T value;
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                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;
                }
 
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
+               string DebuggerDisplay {
+                       get {
+                               return has_value ? value.ToString () : null;
+                       }
+               }
+
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                public bool HasValue {
                        get { return has_value; }
                }
 
+               [DebuggerBrowsable (DebuggerBrowsableState.Never)]
                public T Value {
                        get { 
                                if (!has_value)
@@ -99,36 +121,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;
@@ -149,15 +152,12 @@ namespace System {
 
                public T GetValueOrDefault ()
                {
-                       return GetValueOrDefault (default (T));
+                       return has_value ? value : default (T);
                }
 
-               public T GetValueOrDefault (T def_value)
+               public T GetValueOrDefault (T defaultValue)
                {
-                       if (!has_value)
-                               return def_value;
-                       else
-                               return value;
+                       return has_value ? value : defaultValue;
                }
 
                public override string ToString ()
@@ -165,7 +165,7 @@ namespace System {
                        if (has_value)
                                return value.ToString ();
                        else
-                               return "";
+                               return String.Empty;
                }
 
                public static implicit operator Nullable<T> (T value)
@@ -178,25 +178,19 @@ namespace System {
                        return value.Value;
                }
 
-               public static bool operator == (Nullable<T> left, Nullable<T> right)
-               {
-                       return left.Equals (right);
-               }
-
-               public static bool operator != (Nullable<T> left, Nullable<T> right)
-               {
-                       return !left.Equals (right);
-               }
-
-
+               //
                // These are called by the JIT
-               // Ironicly, the C#  code is the same for these two,
-               // however, on the inside they do somewhat different things
+               //
+#pragma warning disable 169
+               //
+               // JIT implementation of box valuetype System.Nullable`1<T>
+               //
                static object Box (T? o)
                {
-                       if (o == null)
+                       if (!o.has_value)
                                return null;
-                       return (T) o;
+                               
+                       return o.value;
                }
                
                static T? Unbox (object o)
@@ -205,6 +199,6 @@ namespace System {
                                return null;
                        return (T) o;
                }
+#pragma warning restore 169
        }
 }
-#endif