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
;
47 using System
.Runtime
.ConstrainedExecution
;
52 [System
.Runtime
.InteropServices
.ComVisible (true)]
53 public unsafe struct IntPtr
: ISerializable
55 private void *m_value
;
57 public static readonly IntPtr Zero
;
59 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
60 public IntPtr (int value)
62 m_value
= (void *) value;
65 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
66 public IntPtr (long value)
68 /* FIXME: Needs to figure the exact check which works on all architectures */
70 if (((value >> 32 > 0) || (value < 0)) && (IntPtr.Size < 8)) {
71 throw new OverflowException (
72 Locale.GetText ("This isn't a 64bits machine."));
76 m_value
= (void *) value;
79 [CLSCompliant (false)]
80 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
81 unsafe public IntPtr (void *value)
86 private IntPtr (SerializationInfo info
, StreamingContext context
)
88 long savedValue
= info
.GetInt64 ("value");
89 m_value
= (void *) savedValue
;
92 public static int Size
{
93 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
95 return sizeof (void *);
99 void ISerializable
.GetObjectData (SerializationInfo info
, StreamingContext context
)
102 throw new ArgumentNullException ("info");
104 info
.AddValue ("value", ToInt64 ());
107 public override bool Equals (object obj
)
109 if (!(obj
is System
.IntPtr
))
112 return ((IntPtr
) obj
).m_value
== m_value
;
115 public override int GetHashCode ()
117 return (int) m_value
;
120 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
121 public int ToInt32 ()
123 return (int) m_value
;
126 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
127 public long ToInt64 ()
129 // pointer to long conversion is done using conv.u8 by the compiler
131 return (long)(int)m_value
;
133 return (long)m_value
;
136 [CLSCompliant (false)]
137 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
138 unsafe public void *ToPointer ()
143 override public string ToString ()
145 return ToString (null);
148 public string ToString (string format
)
151 return ((int) m_value
).ToString (format
);
153 return ((long) m_value
).ToString (format
);
156 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
157 public static bool operator == (IntPtr value1
, IntPtr value2
)
159 return (value1
.m_value
== value2
.m_value
);
162 [ReliabilityContractAttribute (Consistency
.WillNotCorruptState
, Cer
.Success
)]
163 public static bool operator != (IntPtr value1
, IntPtr value2
)
165 return (value1
.m_value
!= value2
.m_value
);
168 [ReliabilityContractAttribute (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
169 public static explicit operator IntPtr (int value)
171 return new IntPtr (value);
174 [ReliabilityContractAttribute (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
175 public static explicit operator IntPtr (long value)
177 return new IntPtr (value);
180 [ReliabilityContractAttribute (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
181 [CLSCompliant (false)]
182 unsafe public static explicit operator IntPtr (void *value)
184 return new IntPtr (value);
187 public static explicit operator int (IntPtr
value)
189 return (int) value.m_value
;
192 public static explicit operator long (IntPtr
value)
194 return value.ToInt64 ();
197 [CLSCompliant (false)]
198 unsafe public static explicit operator void * (IntPtr
value)
200 return value.m_value
;
204 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
205 public static IntPtr
Add (IntPtr pointer
, int offset
)
207 return (IntPtr
) (unchecked (((byte *) pointer
) + offset
));
210 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
211 public static IntPtr
Subtract (IntPtr pointer
, int offset
)
213 return (IntPtr
) (unchecked (((byte *) pointer
) - offset
));
216 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
217 public static IntPtr
operator + (IntPtr pointer
, int offset
)
219 return (IntPtr
) (unchecked (((byte *) pointer
) + offset
));
222 [ReliabilityContract (Consistency
.MayCorruptInstance
, Cer
.MayFail
)]
223 public static IntPtr
operator - (IntPtr pointer
, int offset
)
225 return (IntPtr
) (unchecked (((byte *) pointer
) - offset
));