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