2007-08-08 Atsushi Enomoto <atsushi@ximian.com>
[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 *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 i32)
68                 {
69                         value = (void *) i32;
70                 }
71
72 #if NET_2_0
73                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
74 #endif
75                 public IntPtr (long i64)
76                 {
77                         if (((i64 > Int32.MaxValue) || (i64 < Int32.MinValue)) && (IntPtr.Size < 8)) {
78                                 throw new OverflowException (
79                                         Locale.GetText ("This isn't a 64bits machine."));
80                         }
81
82                         value = (void *) i64;
83                 }
84
85                 [CLSCompliant (false)]
86 #if NET_2_0
87                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
88 #endif
89                 unsafe public IntPtr (void *ptr)
90                 {
91                         value = ptr;
92                 }
93
94                 private IntPtr (SerializationInfo info, StreamingContext context)
95                 {
96                         long savedValue = info.GetInt64 ("value");
97                         value = (void *) savedValue;
98                 }
99
100                 public static int Size {
101 #if NET_2_0
102                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
103 #endif
104                         get {
105                                 return sizeof (void *);
106                         }
107                 }
108
109                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
110                 {
111                         if (info == null)
112                                 throw new ArgumentNullException ("info");
113
114                         info.AddValue ("value", (long) value);
115                 }
116
117                 public override bool Equals (object o)
118                 {
119                         if (!(o is System.IntPtr))
120                                 return false;
121
122                         return ((IntPtr) o).value == value;
123                 }
124
125                 public override int GetHashCode ()
126                 {
127                         return (int) value;
128                 }
129
130 #if NET_2_0
131                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
132 #endif
133                 public int ToInt32 ()
134                 {
135                         return (int) value;
136                 }
137
138 #if NET_2_0
139                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
140 #endif
141                 public long ToInt64 ()
142                 {
143                         return (long) value;
144                 }
145
146                 [CLSCompliant (false)]
147 #if NET_2_0
148                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
149 #endif
150                 unsafe public void *ToPointer ()
151                 {
152                         return value;
153                 }
154
155                 override public string ToString ()
156                 {
157                         return ToString (null);
158                 }
159
160 #if NET_2_0
161                 public
162 #endif
163                 string ToString (string format)
164                 {
165                         if (Size == 4)
166                                 return ((int) value).ToString ();
167                         else
168                                 return ((long) value).ToString ();
169                 }
170
171 #if NET_2_0
172                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
173 #endif
174                 public static bool operator == (IntPtr a, IntPtr b)
175                 {
176                         return (a.value == b.value);
177                 }
178
179 #if NET_2_0
180                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
181 #endif
182                 public static bool operator != (IntPtr a, IntPtr b)
183                 {
184                         return (a.value != b.value);
185                 }
186
187 #if NET_2_0
188                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
189 #endif
190                 public static explicit operator IntPtr (int value)
191                 {
192                         return new IntPtr (value);
193                 }
194
195 #if NET_2_0
196                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
197 #endif
198                 public static explicit operator IntPtr (long value)
199                 {
200                         return new IntPtr (value);
201                 }
202
203 #if NET_2_0
204                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
205 #endif          
206                 [CLSCompliant (false)]
207                 unsafe public static explicit operator IntPtr (void *value)
208                 {
209                         return new IntPtr (value);
210                 }
211
212                 public static explicit operator int (IntPtr value)
213                 {
214                         return (int) value.value;
215                 }
216
217                 public static explicit operator long (IntPtr value)
218                 {
219                         return (long) value.value;
220                 }
221
222                 [CLSCompliant (false)]
223                 unsafe public static explicit operator void * (IntPtr value)
224                 {
225                         return value.value;
226                 }
227         }
228 }