5 // Miguel de Icaza (miguel@ximian.com)
8 // Michael Lambert, michaellambert@email.com
10 // (C) Ximian, Inc. http://www.ximian.com
13 // Requires '/unsafe' compiler option. This class uses void*,
14 // in overloaded constructors, conversion, and cast members in
15 // the public interface. Using pointers is not valid CLS and
16 // the methods in question have been marked with the
17 // CLSCompliant attribute that avoid compiler warnings.
19 // FIXME: How do you specify a native int in C#? I am going to have to do some figuring out
23 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
25 // Permission is hereby granted, free of charge, to any person obtaining
26 // a copy of this software and associated documentation files (the
27 // "Software"), to deal in the Software without restriction, including
28 // without limitation the rights to use, copy, modify, merge, publish,
29 // distribute, sublicense, and/or sell copies of the Software, and to
30 // permit persons to whom the Software is furnished to do so, subject to
31 // the following conditions:
33 // The above copyright notice and this permission notice shall be
34 // included in all copies or substantial portions of the Software.
36 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
37 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
38 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
39 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
40 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
41 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
42 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45 using System.Globalization;
46 using System.Runtime.Serialization;
49 using System.Runtime.ConstrainedExecution;
56 [System.Runtime.InteropServices.ComVisible (true)]
58 public unsafe struct IntPtr : ISerializable
62 public static readonly IntPtr Zero;
65 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
67 public IntPtr (int i32)
73 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
75 public IntPtr (long i64)
77 if (((i64 > Int32.MaxValue) || (i64 < Int32.MinValue)) && (IntPtr.Size < 8)) {
78 throw new OverflowException (
79 Locale.GetText ("This isn't a 64bits machine."));
85 [CLSCompliant (false)]
87 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
89 unsafe public IntPtr (void *ptr)
94 private IntPtr (SerializationInfo info, StreamingContext context)
96 long savedValue = info.GetInt64 ("value");
97 value = (void *) savedValue;
100 public static int Size {
102 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
105 return sizeof (void *);
109 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
112 throw new ArgumentNullException ("info");
114 info.AddValue ("value", ToInt64 ());
117 public override bool Equals (object o)
119 if (!(o is System.IntPtr))
122 return ((IntPtr) o).value == value;
125 public override int GetHashCode ()
131 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
133 public int ToInt32 ()
139 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
141 public long ToInt64 ()
143 // pointer to long conversion is done using conv.u8 by the compiler
145 return (long)(int)value;
150 [CLSCompliant (false)]
152 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
154 unsafe public void *ToPointer ()
159 override public string ToString ()
161 return ToString (null);
167 string ToString (string format)
170 return ((int) value).ToString (format);
172 return ((long) value).ToString (format);
176 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
178 public static bool operator == (IntPtr a, IntPtr b)
180 return (a.value == b.value);
184 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
186 public static bool operator != (IntPtr a, IntPtr b)
188 return (a.value != b.value);
192 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
194 public static explicit operator IntPtr (int value)
196 return new IntPtr (value);
200 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
202 public static explicit operator IntPtr (long value)
204 return new IntPtr (value);
208 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
210 [CLSCompliant (false)]
211 unsafe public static explicit operator IntPtr (void *value)
213 return new IntPtr (value);
216 public static explicit operator int (IntPtr value)
218 return (int) value.value;
221 public static explicit operator long (IntPtr value)
223 return value.ToInt64 ();
226 [CLSCompliant (false)]
227 unsafe public static explicit operator void * (IntPtr value)