2009-07-11 Michael Barker <mike@middlesoft.co.uk>
[mono.git] / mcs / class / corlib / System / IntPtr.cs
1 //
2 // System.IntPtr.cs
3 //
4 // Author:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //
7 // Maintainer:
8 //       Michael Lambert, michaellambert@email.com
9 //
10 // (C) Ximian, Inc.  http://www.ximian.com
11 //
12 // Remarks:
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.
18 //
19 // FIXME: How do you specify a native int in C#?  I am going to have to do some figuring out
20 //
21
22 //
23 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
24 //
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:
32 // 
33 // The above copyright notice and this permission notice shall be
34 // included in all copies or substantial portions of the Software.
35 // 
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.
43 //
44
45 using System.Globalization;
46 using System.Runtime.Serialization;
47
48 #if NET_2_0
49 using System.Runtime.ConstrainedExecution;
50 #endif
51
52 namespace System
53 {
54         [Serializable]
55 #if NET_2_0
56         [System.Runtime.InteropServices.ComVisible (true)]
57 #endif
58         public unsafe struct IntPtr : ISerializable
59         {
60                 private void *m_value;
61
62                 public static readonly IntPtr Zero;
63
64 #if NET_2_0
65                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
66 #endif
67                 public IntPtr (int value)
68                 {
69                         m_value = (void *) value;
70                 }
71
72 #if NET_2_0
73                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
74 #endif
75                 public IntPtr (long value)
76                 {
77                         /* FIXME: Needs to figure the exact check which works on all architectures */
78                         /*
79                         if (((value >> 32 > 0) || (value < 0)) && (IntPtr.Size < 8)) {
80                                 throw new OverflowException (
81                                         Locale.GetText ("This isn't a 64bits machine."));
82                         }
83                         */
84
85                         m_value = (void *) value;
86                 }
87
88                 [CLSCompliant (false)]
89 #if NET_2_0
90                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
91 #endif
92                 unsafe public IntPtr (void *value)
93                 {
94                         m_value = value;
95                 }
96
97                 private IntPtr (SerializationInfo info, StreamingContext context)
98                 {
99                         long savedValue = info.GetInt64 ("value");
100                         m_value = (void *) savedValue;
101                 }
102
103                 public static int Size {
104 #if NET_2_0
105                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
106 #endif
107                         get {
108                                 return sizeof (void *);
109                         }
110                 }
111
112                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
113                 {
114                         if (info == null)
115                                 throw new ArgumentNullException ("info");
116
117                         info.AddValue ("value", ToInt64 ());
118                 }
119
120                 public override bool Equals (object obj)
121                 {
122                         if (!(obj is System.IntPtr))
123                                 return false;
124
125                         return ((IntPtr) obj).m_value == m_value;
126                 }
127
128                 public override int GetHashCode ()
129                 {
130                         return (int) m_value;
131                 }
132
133 #if NET_2_0
134                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
135 #endif
136                 public int ToInt32 ()
137                 {
138                         return (int) m_value;
139                 }
140
141 #if NET_2_0
142                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
143 #endif
144                 public long ToInt64 ()
145                 {
146                         // pointer to long conversion is done using conv.u8 by the compiler
147                         if (Size == 4)
148                                 return (long)(int)m_value;
149                         else
150                                 return (long)m_value;
151                 }
152
153                 [CLSCompliant (false)]
154 #if NET_2_0
155                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
156 #endif
157                 unsafe public void *ToPointer ()
158                 {
159                         return m_value;
160                 }
161
162                 override public string ToString ()
163                 {
164                         return ToString (null);
165                 }
166
167 #if NET_2_0
168                 public
169 #endif
170                 string ToString (string format)
171                 {
172                         if (Size == 4)
173                                 return ((int) m_value).ToString (format);
174                         else
175                                 return ((long) m_value).ToString (format);
176                 }
177
178 #if NET_2_0
179                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
180 #endif
181                 public static bool operator == (IntPtr value1, IntPtr value2)
182                 {
183                         return (value1.m_value == value2.m_value);
184                 }
185
186 #if NET_2_0
187                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
188 #endif
189                 public static bool operator != (IntPtr value1, IntPtr value2)
190                 {
191                         return (value1.m_value != value2.m_value);
192                 }
193
194 #if NET_2_0
195                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
196 #endif
197                 public static explicit operator IntPtr (int value)
198                 {
199                         return new IntPtr (value);
200                 }
201
202 #if NET_2_0
203                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
204 #endif
205                 public static explicit operator IntPtr (long value)
206                 {
207                         return new IntPtr (value);
208                 }
209
210 #if NET_2_0
211                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
212 #endif          
213                 [CLSCompliant (false)]
214                 unsafe public static explicit operator IntPtr (void *value)
215                 {
216                         return new IntPtr (value);
217                 }
218
219                 public static explicit operator int (IntPtr value)
220                 {
221                         return (int) value.m_value;
222                 }
223
224                 public static explicit operator long (IntPtr value)
225                 {
226                         return value.ToInt64 ();
227                 }
228
229                 [CLSCompliant (false)]
230                 unsafe public static explicit operator void * (IntPtr value)
231                 {
232                         return value.m_value;
233                 }
234         }
235 }