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
60 private void *m_value;
62 public static readonly IntPtr Zero;
65 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
67 public IntPtr (int value)
69 m_value = (void *) value;
73 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
75 public IntPtr (long value)
77 /* FIXME: Needs to figure the exact check which works on all architectures */
79 if (((value >> 32 > 0) || (value < 0)) && (IntPtr.Size < 8)) {
80 throw new OverflowException (
81 Locale.GetText ("This isn't a 64bits machine."));
85 m_value = (void *) value;
88 [CLSCompliant (false)]
90 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
92 unsafe public IntPtr (void *value)
97 private IntPtr (SerializationInfo info, StreamingContext context)
99 long savedValue = info.GetInt64 ("value");
100 m_value = (void *) savedValue;
103 public static int Size {
105 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
108 return sizeof (void *);
112 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
115 throw new ArgumentNullException ("info");
117 info.AddValue ("value", ToInt64 ());
120 public override bool Equals (object obj)
122 if (!(obj is System.IntPtr))
125 return ((IntPtr) obj).m_value == m_value;
128 public override int GetHashCode ()
130 return (int) m_value;
134 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
136 public int ToInt32 ()
138 return (int) m_value;
142 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
144 public long ToInt64 ()
146 // pointer to long conversion is done using conv.u8 by the compiler
148 return (long)(int)m_value;
150 return (long)m_value;
153 [CLSCompliant (false)]
155 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
157 unsafe public void *ToPointer ()
162 override public string ToString ()
164 return ToString (null);
170 string ToString (string format)
173 return ((int) m_value).ToString (format);
175 return ((long) m_value).ToString (format);
179 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
181 public static bool operator == (IntPtr value1, IntPtr value2)
183 return (value1.m_value == value2.m_value);
187 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
189 public static bool operator != (IntPtr value1, IntPtr value2)
191 return (value1.m_value != value2.m_value);
195 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
197 public static explicit operator IntPtr (int value)
199 return new IntPtr (value);
203 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
205 public static explicit operator IntPtr (long value)
207 return new IntPtr (value);
211 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
213 [CLSCompliant (false)]
214 unsafe public static explicit operator IntPtr (void *value)
216 return new IntPtr (value);
219 public static explicit operator int (IntPtr value)
221 return (int) value.m_value;
224 public static explicit operator long (IntPtr value)
226 return value.ToInt64 ();
229 [CLSCompliant (false)]
230 unsafe public static explicit operator void * (IntPtr value)
232 return value.m_value;