Merge pull request #1631 from alexanderkyte/stringbuilder-referencesource
[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 using System.Runtime.ConstrainedExecution;
48 using System.Diagnostics.Contracts;
49
50 namespace System
51 {
52         [Serializable]
53         [System.Runtime.InteropServices.ComVisible (true)]
54         public unsafe struct IntPtr : ISerializable
55         {
56                 private void *m_value;
57
58                 public static readonly IntPtr Zero;
59
60                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
61                 public IntPtr (int value)
62                 {
63                         m_value = (void *) value;
64                 }
65
66                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
67                 public IntPtr (long value)
68                 {
69                         /* FIXME: Needs to figure the exact check which works on all architectures */
70                         /*
71                         if (((value >> 32 > 0) || (value < 0)) && (IntPtr.Size < 8)) {
72                                 throw new OverflowException (
73                                         Locale.GetText ("This isn't a 64bits machine."));
74                         }
75                         */
76
77                         m_value = (void *) value;
78                 }
79
80                 [CLSCompliant (false)]
81                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
82                 unsafe public IntPtr (void *value)
83                 {
84                         m_value = value;
85                 }
86
87                 private IntPtr (SerializationInfo info, StreamingContext context)
88                 {
89                         long savedValue = info.GetInt64 ("value");
90                         m_value = (void *) savedValue;
91                 }
92
93                 public static int Size {
94                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
95                         get {
96                                 return sizeof (void *);
97                         }
98                 }
99
100                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
101                 {
102                         if (info == null)
103                                 throw new ArgumentNullException ("info");
104
105                         info.AddValue ("value", ToInt64 ());
106                 }
107
108                 public override bool Equals (object obj)
109                 {
110                         if (!(obj is System.IntPtr))
111                                 return false;
112
113                         return ((IntPtr) obj).m_value == m_value;
114                 }
115
116                 public override int GetHashCode ()
117                 {
118                         return (int) m_value;
119                 }
120
121                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
122                 public int ToInt32 ()
123                 {
124                         return (int) m_value;
125                 }
126
127                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
128                 public long ToInt64 ()
129                 {
130                         // pointer to long conversion is done using conv.u8 by the compiler
131                         if (Size == 4)
132                                 return (long)(int)m_value;
133                         else
134                                 return (long)m_value;
135                 }
136
137                 [CLSCompliant (false)]
138                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
139                 unsafe public void *ToPointer ()
140                 {
141                         return m_value;
142                 }
143
144                 override public string ToString ()
145                 {
146                         return ToString (null);
147                 }
148
149                 public string ToString (string format)
150                 {
151                         if (Size == 4)
152                                 return ((int) m_value).ToString (format, null);
153                         else
154                                 return ((long) m_value).ToString (format, null);
155                 }
156
157                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
158                 public static bool operator == (IntPtr value1, IntPtr value2)
159                 {
160                         return (value1.m_value == value2.m_value);
161                 }
162
163                 [ReliabilityContractAttribute (Consistency.WillNotCorruptState, Cer.Success)]
164                 public static bool operator != (IntPtr value1, IntPtr value2)
165                 {
166                         return (value1.m_value != value2.m_value);
167                 }
168
169                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
170                 public static explicit operator IntPtr (int value)
171                 {
172                         return new IntPtr (value);
173                 }
174
175                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
176                 public static explicit operator IntPtr (long value)
177                 {
178                         return new IntPtr (value);
179                 }
180
181                 [ReliabilityContractAttribute (Consistency.MayCorruptInstance, Cer.MayFail)]
182                 [CLSCompliant (false)]
183                 unsafe public static explicit operator IntPtr (void *value)
184                 {
185                         return new IntPtr (value);
186                 }
187
188                 public static explicit operator int (IntPtr value)
189                 {
190                         return (int) value.m_value;
191                 }
192
193                 public static explicit operator long (IntPtr value)
194                 {
195                         return value.ToInt64 ();
196                 }
197
198                 [CLSCompliant (false)]
199                 unsafe public static explicit operator void * (IntPtr value)
200                 {
201                         return value.m_value;
202                 }
203
204                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
205                 public static IntPtr Add (IntPtr pointer, int offset)
206                 {
207                         return (IntPtr) (unchecked (((byte *) pointer) + offset));
208                 }
209
210                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
211                 public static IntPtr Subtract (IntPtr pointer, int offset)
212                 {
213                         return (IntPtr) (unchecked (((byte *) pointer) - offset));
214                 }
215
216                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
217                 public static IntPtr operator + (IntPtr pointer, int offset)
218                 {
219                         return (IntPtr) (unchecked (((byte *) pointer) + offset));
220                 }
221
222                 [ReliabilityContract (Consistency.MayCorruptInstance, Cer.MayFail)]
223                 public static IntPtr operator - (IntPtr pointer, int offset)
224                 {
225                         return (IntPtr) (unchecked (((byte *) pointer) - offset));
226                 }
227
228                 // fast way to compare IntPtr to (IntPtr)0 while IntPtr.Zero doesn't work due to slow statics access
229                 [Pure]
230                 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
231                 internal unsafe bool IsNull()
232                 {
233                         return m_value == null;
234                 }
235         }
236 }