2002-08-20 Gonzalo Paniagua Javier <gonzalo@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         [Serializable]
37         public unsafe struct IntPtr : ISerializable {
38
39                 private void *value;
40
41                 public static readonly IntPtr Zero;
42
43                 static IntPtr ()
44                 {
45                         Zero.value = (void *) 0;
46                 }
47                 
48                 public IntPtr (int i32)
49                 {
50                         value = (void *) i32;
51                 }
52
53                 public IntPtr (long i64)
54                 {
55                         value = (void *) i64;
56                 }
57
58                 [
59                         CLSCompliant(false)
60                 ]
61                 unsafe public IntPtr (void *ptr)
62                 {
63                         value = ptr;
64                 }
65
66                 public static int Size {
67                         get {
68                                 return sizeof (void *);
69                         }
70                 }
71
72                 public void GetObjectData (SerializationInfo si, StreamingContext sc)
73                 {
74                         if( si == null )
75                                 throw new ArgumentNullException( "si" );
76         
77                         si.AddValue("value", (long) value);
78                 }
79
80                 public override bool Equals (object o)
81                 {
82                         if (!(o is System.IntPtr))
83                                 return false;
84
85                         return ((IntPtr) o).value == value;
86                 }
87
88                 public override int GetHashCode ()
89                 {
90                         return (int) value;
91                 }
92
93                 public int ToInt32 ()
94                 {
95                         return (int) value;
96                 }
97
98                 public long ToInt64 ()
99                 {
100                         return (long) value;
101                 }
102
103                 [
104                         CLSCompliant(false)
105                 ]
106                 unsafe public void *ToPointer ()
107                 {
108                         return value;
109                 }
110
111                 override public string ToString ()
112                 {
113                         if (Size == 4)
114                                 return ((int) value).ToString ();
115                         else
116                                 return ((long) value).ToString ();
117                 }
118
119                 public static bool operator == (IntPtr a, IntPtr b)
120                 {
121                         return (a.value == b.value);
122                 }
123
124                 public static bool operator != (IntPtr a, IntPtr b)
125                 {
126                         return (a.value != b.value);
127                 }
128
129                 public static explicit operator IntPtr (int value)
130                 {
131                         return new IntPtr (value);
132                 }
133
134                 public static explicit operator IntPtr (long value)
135                 {
136                         return new IntPtr (value);
137                 }
138                 
139                 [
140                         CLSCompliant(false)
141                 ]
142                 unsafe public static explicit operator IntPtr (void *value)
143                 {
144                         return new IntPtr (value);
145                 }
146
147                 public static explicit operator int (IntPtr value)
148                 {
149                         return (int) value.value;
150                 }
151
152                 public static explicit operator long (IntPtr value)
153                 {
154                         return (long) value.value;
155                 }
156
157                 [
158                         CLSCompliant(false)
159                 ]
160                 unsafe public static explicit operator void * (IntPtr value)
161                 {
162                         return value.value;
163                 }
164         }
165 }