2002-01-14 Miguel de Icaza <miguel@ximian.com>
[mono.git] / mcs / class / corlib / System / UIntPtr.cs
1 //------------------------------------------------------------------------------
2 // 
3 // System.UIntPtr.cs 
4 //
5 // Copyright (C) 2001 Michael Lambert, All Rights Reserved
6 // 
7 // Author:          Michael Lambert, michaellambert@email.com
8 // Created:         Thu 07/31/2001 
9 //
10 // Remarks:         Requires '/unsafe' compiler option.  This class uses void*,
11 //                  ulong, and uint in overloaded constructors, conversion, and 
12 //                  cast members in the public interface.  Using pointers is not 
13 //                  valid CLS and the methods in question have been marked with  
14 //                  the CLSCompliant attribute that avoid compiler warnings.
15 //
16 //------------------------------------------------------------------------------
17
18 using System.Runtime.Serialization;
19 using System.Runtime.InteropServices;
20
21
22 namespace System
23 {
24
25 [
26     StructLayout(LayoutKind.Auto),
27     CLSCompliant(false)
28 ]
29 public unsafe struct UIntPtr : ISerializable
30 {
31     public static readonly UIntPtr Zero = new UIntPtr(0);
32     private void* _pointer;
33
34     [
35         CLSCompliant(false)
36     ]
37     public UIntPtr(ulong value)
38     {
39         _pointer = (void*) value;
40     }
41     
42     [
43         CLSCompliant(false)
44     ]
45     public UIntPtr(uint value)
46     {
47         _pointer = (void*)value;
48     }
49
50     [
51         CLSCompliant(false)
52     ]
53     public unsafe UIntPtr(void* value)
54     {
55         _pointer = value;
56     }
57
58     public override bool Equals(object obj)
59     {
60         if( obj is UIntPtr )
61         {
62             UIntPtr obj2 = (UIntPtr)obj;
63             return this._pointer == obj2._pointer;
64         }   
65
66         return false;
67     }
68     public override int GetHashCode()
69     {
70         return (int)_pointer;
71     }
72     
73     [
74         CLSCompliant(false)
75     ]
76     public uint ToUInt32()
77     {
78         return (uint) _pointer;
79     }
80
81     [
82         CLSCompliant(false)
83     ]
84     public ulong ToUInt64()
85     {
86         return (ulong) _pointer;
87     }
88
89     [
90         CLSCompliant(false)
91     ]
92     public unsafe void* ToPointer()
93     {
94         return _pointer;
95     }
96     public override string ToString()
97     {
98         return ((uint) _pointer).ToString();
99     }
100
101     // Interface ISerializable
102     void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
103     {
104         if( info == null )
105             throw new ArgumentNullException( "info" );
106         // if( context == null ) -- context is struct can't be null.
107         //    throw new ArgumentNullException( "context" );
108         
109         info.AddValue("pointer", (ulong)_pointer);
110     }
111
112     public static bool operator ==(UIntPtr value1, UIntPtr value2)
113     {
114         return value1._pointer == value2._pointer;
115     }
116     public static bool operator !=(UIntPtr value1, UIntPtr value2)
117     {
118         return value1._pointer != value2._pointer;
119     }
120
121     [
122         CLSCompliant(false)
123     ]
124     public static explicit operator ulong(UIntPtr value)
125     {
126         return (ulong)value._pointer;
127     }
128
129     [
130         CLSCompliant(false)
131     ]
132     public static explicit operator uint(UIntPtr value)
133     {
134         return (uint)value._pointer;
135     }
136     
137     [
138         CLSCompliant(false)
139     ]
140     public static explicit operator UIntPtr(ulong value)
141     {
142         return new UIntPtr(value);
143     }
144
145     [
146         CLSCompliant(false)
147     ]
148     public unsafe static explicit operator UIntPtr(void* value)
149     {
150         return new UIntPtr(value);
151     }
152
153     [
154         CLSCompliant(false)
155     ]
156     public unsafe static explicit operator void*(UIntPtr value)
157     {
158         return value.ToPointer();
159     }
160     
161     [
162         CLSCompliant(false)
163     ]
164     public static explicit operator UIntPtr(uint value)
165     {
166         return new UIntPtr(value);
167     }
168
169     public static int Size
170     {
171         get
172         {   
173             return sizeof(void*); 
174         }
175     }
176 }
177
178 } // Namespace
179