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", (long) value);
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 ()
146 [CLSCompliant (false)]
148 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
150 unsafe public void *ToPointer ()
155 override public string ToString ()
157 return ToString (null);
163 string ToString (string format)
166 return ((int) value).ToString ();
168 return ((long) value).ToString ();
172 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
174 public static bool operator == (IntPtr a, IntPtr b)
176 return (a.value == b.value);
180 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
182 public static bool operator != (IntPtr a, IntPtr b)
184 return (a.value != b.value);
188 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
190 public static explicit operator IntPtr (int value)
192 return new IntPtr (value);
196 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
198 public static explicit operator IntPtr (long value)
200 return new IntPtr (value);
204 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
206 [CLSCompliant (false)]
207 unsafe public static explicit operator IntPtr (void *value)
209 return new IntPtr (value);
212 public static explicit operator int (IntPtr value)
214 return (int) value.value;
217 public static explicit operator long (IntPtr value)
219 return (long) value.value;
222 [CLSCompliant (false)]
223 unsafe public static explicit operator void * (IntPtr value)