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