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