(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System / IntPtr.cs
blob2b876f02bf87b4706189d6a238c08a97c8e2bf8e
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
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.
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:
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.
45 using System.Globalization;
46 using System.Runtime.Serialization;
48 namespace System
50 [Serializable]
51 public unsafe struct IntPtr : ISerializable
53 private void *value;
55 public static readonly IntPtr Zero;
57 public IntPtr (int i32)
59 value = (void *) i32;
62 public IntPtr (long i64)
64 if (((i64 > Int32.MaxValue) || (i64 < Int32.MinValue)) && (IntPtr.Size < 8)) {
65 throw new OverflowException (
66 Locale.GetText ("This isn't a 64bits machine."));
69 value = (void *) i64;
72 [CLSCompliant (false)]
73 unsafe public IntPtr (void *ptr)
75 value = ptr;
78 private IntPtr (SerializationInfo info, StreamingContext context)
80 long savedValue = info.GetInt64 ("value");
81 value = (void *) savedValue;
84 public static int Size {
85 get {
86 return sizeof (void *);
90 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
92 if (info == null)
93 throw new ArgumentNullException ("info");
95 info.AddValue ("value", (long) value);
98 public override bool Equals (object o)
100 if (!(o is System.IntPtr))
101 return false;
103 return ((IntPtr) o).value == value;
106 public override int GetHashCode ()
108 return (int) value;
111 public int ToInt32 ()
113 return (int) value;
116 public long ToInt64 ()
118 return (long) value;
121 [CLSCompliant (false)]
122 unsafe public void *ToPointer ()
124 return value;
127 override public string ToString ()
129 if (Size == 4)
130 return ((int) value).ToString ();
131 else
132 return ((long) value).ToString ();
135 public static bool operator == (IntPtr a, IntPtr b)
137 return (a.value == b.value);
140 public static bool operator != (IntPtr a, IntPtr b)
142 return (a.value != b.value);
145 public static explicit operator IntPtr (int value)
147 return new IntPtr (value);
150 public static explicit operator IntPtr (long value)
152 return new IntPtr (value);
155 [CLSCompliant (false)]
156 unsafe public static explicit operator IntPtr (void *value)
158 return new IntPtr (value);
161 public static explicit operator int (IntPtr value)
163 return (int) value.value;
166 public static explicit operator long (IntPtr value)
168 return (long) value.value;
171 [CLSCompliant (false)]
172 unsafe public static explicit operator void * (IntPtr value)
174 return value.value;