2002-03-25 Miguel de Icaza <miguel@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:                     Requires '/unsafe' compiler option.  This class uses void*,
13 //                                      in overloaded constructors, conversion, and cast members in 
14 //                                      the public interface.  Using pointers is not valid CLS and 
15 //                                      the methods in question have been marked with  the 
16 //                                      CLSCompliant attribute that avoid compiler warnings.
17 //
18 // FIXME: How do you specify a native int in C#?  I am going to have to do some figuring out
19 //
20
21 using System;
22 using System.Runtime.Serialization;
23
24 #if __MonoCS__
25 #else
26 [
27     assembly: System.CLSCompliant(true)
28 ]
29 #endif
30
31 namespace System {
32
33         [
34                 CLSCompliant(true)
35         ]
36         public unsafe struct IntPtr : ISerializable {
37
38                 private void *value;
39
40                 public static IntPtr Zero;
41
42                 static IntPtr ()
43                 {
44                         Zero.value = (void *) 0;
45                 }
46                 
47                 public IntPtr (int i32)
48                 {
49                         value = (void *) i32;
50                 }
51
52                 public IntPtr (long i64)
53                 {
54                         value = (void *) i64;
55                 }
56
57                 [
58                         CLSCompliant(false)
59                 ]
60                 unsafe public IntPtr (void *ptr)
61                 {
62                         value = ptr;
63                 }
64
65                 public static int Size {
66                         get {
67                                 return sizeof (void *);
68                         }
69                 }
70
71                 public void GetObjectData (SerializationInfo si, StreamingContext sc)
72                 {
73                         if( si == null )
74                                 throw new ArgumentNullException( "si" );
75         
76                         si.AddValue("value", (long) value);
77                 }
78
79                 public override bool Equals (object o)
80                 {
81                         if (!(o is System.IntPtr))
82                                 return false;
83
84                         return ((IntPtr) o).value == value;
85                 }
86
87                 public override int GetHashCode ()
88                 {
89                         return (int) value;
90                 }
91
92                 public int ToInt32 ()
93                 {
94                         return (int) value;
95                 }
96
97                 public long ToInt64 ()
98                 {
99                         return (long) value;
100                 }
101
102                 [
103                         CLSCompliant(false)
104                 ]
105                 unsafe public void *ToPointer ()
106                 {
107                         return value;
108                 }
109
110                 override public string ToString ()
111                 {
112                         if (Size == 4)
113                                 return ((int) value).ToString ();
114                         else
115                                 return ((long) value).ToString ();
116                 }
117
118                 public static bool operator == (IntPtr a, IntPtr b)
119                 {
120                         return (a.value == b.value);
121                 }
122
123                 public static bool operator != (IntPtr a, IntPtr b)
124                 {
125                         return (a.value != b.value);
126                 }
127
128                 public static explicit operator IntPtr (int value)
129                 {
130                         return new IntPtr (value);
131                 }
132
133                 public static explicit operator IntPtr (long value)
134                 {
135                         return new IntPtr (value);
136                 }
137                 
138                 [
139                         CLSCompliant(false)
140                 ]
141                 unsafe public static explicit operator IntPtr (void *value)
142                 {
143                         return new IntPtr (value);
144                 }
145
146                 public static explicit operator int (IntPtr value)
147                 {
148                         return (int) value.value;
149                 }
150
151                 public static explicit operator long (IntPtr value)
152                 {
153                         return (long) value.value;
154                 }
155
156                 [
157                         CLSCompliant(false)
158                 ]
159                 unsafe public static explicit operator void * (IntPtr value)
160                 {
161                         return value.value;
162                 }
163         }
164 }