[Mono-list] IntPtr.cs contributions

Michael Lambert michaellambert@email.com
Thu, 23 Aug 2001 21:56:23 -0400


This is a multi-part message in MIME format.

------=_NextPart_000_0319_01C12C1E.72FDA0F0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

I had an IntPtr class that I had been using for some work.  Looking at the tree
file for IntPtr.cs, I have the following ideas.

1. Putting unsafe on the struct declaration saves us from having to add unsafe
to every member declaration.

2. Include CLS Compliant attribute on members that have void* in the declaration
avoid the compiler warnings.

3. Added implementation for GetObjectData

4. Mark the corlib assembly as CLSCompliant. (not included in diff)

The attached file.diff is the major differences from my version and the existing
one.  Also, I had converted my IntPtr class to UIntPtr since they were so close
and check it into the tree. It follows the same ideas.

I noticed the use of the member 'value'.  In this class there shouldn't be any
problems but a property set passes in the new type as 'value' which would be an
interesting conflict to resolve. I recommend changing 'value' to '_value'.
Example:

class foo {
int value;

public int bar {
	get { return value; } // ok
	set { value = value; } // value is the name the compiler uses for prop set
}
}

Kudos,
Michael Lambert

------=_NextPart_000_0319_01C12C1E.72FDA0F0
Content-Type: application/octet-stream;
	name="file.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="file.diff"

? file.diff
Index: IntPtr.cs
===================================================================
RCS file: /cvs/public/mcs/class/corlib/System/IntPtr.cs,v
retrieving revision 1.2
diff -u -r1.2 IntPtr.cs
--- IntPtr.cs	2001/08/07 00:11:48	1.2
+++ IntPtr.cs	2001/08/23 21:23:13
@@ -13,33 +13,36 @@
 
 namespace System {
 	
-	public struct IntPtr : ISerializable {
+	public unsafe struct IntPtr : ISerializable {
 
-		unsafe public void *value;
+		public void *value;
 
-		public static IntPtr Zero;
+		public static readonly IntPtr Zero;
 
-		unsafe static IntPtr ()
+		static IntPtr ()
 		{
 			Zero.value = (void *) 0;
 		}
 		
-		unsafe public IntPtr (int i32)
+		public IntPtr (int i32)
 		{
 			value = (void *) i32;
 		}
 
-		unsafe public IntPtr (long i64)
+		public IntPtr (long i64)
 		{
 			value = (void *) i64;
 		}
 
+        [
+            CLSCompliant(false)
+        ]
 		unsafe public IntPtr (void *ptr)
 		{
 			value = ptr;
 		}
 
-		unsafe public static int Size {
+		public static int Size {
 			get {
 				return sizeof (void *);
 			}
@@ -47,10 +50,13 @@
 
 		public void GetObjectData (SerializationInfo si, StreamingContext sc)
 		{
-			// FIXME: Implement me.
+            if( si == null )
+                throw new ArgumentNullException( "si" );
+        
+            si.AddValue("value", (long)value);
 		}
 
-		unsafe public override bool Equals (object o)
+		public override bool Equals (object o)
 		{
 			if (!(o is System.IntPtr))
 				return false;
@@ -58,27 +64,30 @@
 			return ((IntPtr) o).value == value;
 		}
 
-		unsafe public override int GetHashCode ()
+		public override int GetHashCode ()
 		{
 			return (int) value;
 		}
 
-		unsafe public int ToInt32 ()
+		public int ToInt32 ()
 		{
 			return (int) value;
 		}
 
-		unsafe public long ToInt64 ()
+		public long ToInt64 ()
 		{
 			return (long) value;
 		}
 
+        [
+            CLSCompliant(false)
+        ]
 		unsafe public void *ToPointer ()
 		{
 			return value;
 		}
 
-		unsafe override public string ToString ()
+		override public string ToString ()
 		{
 			if (Size == 4)
 				return ((int) value).ToString ();
@@ -86,41 +95,47 @@
 				return ((long) value).ToString ();
 		}
 
-		unsafe public static bool operator == (IntPtr a, IntPtr b)
+		public static bool operator == (IntPtr a, IntPtr b)
 		{
 			return (a.value == b.value);
 		}
 
-		unsafe public static bool operator != (IntPtr a, IntPtr b)
+		public static bool operator != (IntPtr a, IntPtr b)
 		{
 			return (a.value != b.value);
 		}
 
-		unsafe public static explicit operator IntPtr (int value)
+		public static explicit operator IntPtr (int value)
 		{
 			return new IntPtr (value);
 		}
 
-		unsafe public static explicit operator IntPtr (long value)
+		public static explicit operator IntPtr (long value)
 		{
 			return new IntPtr (value);
 		}
-		
+	
+        [
+            CLSCompliant(false)
+        ]
 		unsafe public static explicit operator IntPtr (void *value)
 		{
 			return new IntPtr (value);
 		}
 
-		unsafe public static explicit operator int (IntPtr value)
+		public static explicit operator int (IntPtr value)
 		{
 			return (int) value.value;
 		}
 
-		unsafe public static explicit operator long (IntPtr value)
+		public static explicit operator long (IntPtr value)
 		{
 			return (long) value.value;
 		}
 
+        [
+            CLSCompliant(false)
+        ]
 		unsafe public static explicit operator void * (IntPtr value)
 		{
 			return value.value;

------=_NextPart_000_0319_01C12C1E.72FDA0F0--