| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- //
- // System.IntPtr.cs
- //
- // Author:
- // Miguel de Icaza ([email protected])
- //
- // Maintainer:
- // Michael Lambert, [email protected]
- //
- // (C) Ximian, Inc. http://www.ximian.com
- //
- // Remarks: Requires '/unsafe' compiler option. This class uses void*,
- // in overloaded constructors, conversion, and cast members in
- // the public interface. Using pointers is not valid CLS and
- // the methods in question have been marked with the
- // CLSCompliant attribute that avoid compiler warnings.
- //
- // FIXME: How do you specify a native int in C#? I am going to have to do some figuring out
- //
- using System;
- using System.Runtime.Serialization;
- [
- assembly: System.CLSCompliant(true)
- ]
- namespace System {
- [
- CLSCompliant(true)
- ]
- public unsafe struct IntPtr : ISerializable {
- private void *value;
- public static IntPtr Zero;
- static IntPtr ()
- {
- Zero.value = (void *) 0;
- }
-
- public IntPtr (int i32)
- {
- value = (void *) i32;
- }
- public IntPtr (long i64)
- {
- value = (void *) i64;
- }
- [
- CLSCompliant(false)
- ]
- unsafe public IntPtr (void *ptr)
- {
- value = ptr;
- }
- public static int Size {
- get {
- return sizeof (void *);
- }
- }
- public void GetObjectData (SerializationInfo si, StreamingContext sc)
- {
- if( si == null )
- throw new ArgumentNullException( "si" );
-
- si.AddValue("value", (long) value);
- }
- public override bool Equals (object o)
- {
- if (!(o is System.IntPtr))
- return false;
- return ((IntPtr) o).value == value;
- }
- public override int GetHashCode ()
- {
- return (int) value;
- }
- public int ToInt32 ()
- {
- return (int) value;
- }
- public long ToInt64 ()
- {
- return (long) value;
- }
- [
- CLSCompliant(false)
- ]
- unsafe public void *ToPointer ()
- {
- return value;
- }
- override public string ToString ()
- {
- if (Size == 4)
- return ((int) value).ToString ();
- else
- return ((long) value).ToString ();
- }
- public static bool operator == (IntPtr a, IntPtr b)
- {
- return (a.value == b.value);
- }
- public static bool operator != (IntPtr a, IntPtr b)
- {
- return (a.value != b.value);
- }
- public static explicit operator IntPtr (int value)
- {
- return new IntPtr (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);
- }
- public static explicit operator int (IntPtr value)
- {
- return (int) value.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;
- }
- }
- }
|