2002-09-13 Nick Drochak <ndrochak@gol.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 [CLSCompliant(false)]
26 [Serializable]
27 public unsafe struct UIntPtr : ISerializable
28 {
29     public static readonly UIntPtr Zero = new UIntPtr(0u);
30     private void* _pointer;
31
32     [
33         CLSCompliant(false)
34     ]
35     public UIntPtr(ulong value)
36     {
37         _pointer = (void*) value;
38     }
39     
40     [
41         CLSCompliant(false)
42     ]
43     public UIntPtr(uint value)
44     {
45         _pointer = (void*)value;
46     }
47
48     [
49         CLSCompliant(false)
50     ]
51     public unsafe UIntPtr(void* value)
52     {
53         _pointer = value;
54     }
55
56     public override bool Equals(object obj)
57     {
58         if( obj is UIntPtr )
59         {
60             UIntPtr obj2 = (UIntPtr)obj;
61             return this._pointer == obj2._pointer;
62         }   
63
64         return false;
65     }
66     public override int GetHashCode()
67     {
68         return (int)_pointer;
69     }
70     
71     [
72         CLSCompliant(false)
73     ]
74     public uint ToUInt32()
75     {
76         return (uint) _pointer;
77     }
78
79     [
80         CLSCompliant(false)
81     ]
82     public ulong ToUInt64()
83     {
84         return (ulong) _pointer;
85     }
86
87     [
88         CLSCompliant(false)
89     ]
90     public unsafe void* ToPointer()
91     {
92         return _pointer;
93     }
94     public override string ToString()
95     {
96         return ((uint) _pointer).ToString();
97     }
98
99     // Interface ISerializable
100     void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
101     {
102         if( info == null )
103             throw new ArgumentNullException( "info" );
104         // if( context == null ) -- context is struct can't be null.
105         //    throw new ArgumentNullException( "context" );
106         
107         info.AddValue("pointer", (ulong)_pointer);
108     }
109
110     public static bool operator ==(UIntPtr value1, UIntPtr value2)
111     {
112         return value1._pointer == value2._pointer;
113     }
114     public static bool operator !=(UIntPtr value1, UIntPtr value2)
115     {
116         return value1._pointer != value2._pointer;
117     }
118
119     [
120         CLSCompliant(false)
121     ]
122     public static explicit operator ulong(UIntPtr value)
123     {
124         return (ulong)value._pointer;
125     }
126
127     [
128         CLSCompliant(false)
129     ]
130     public static explicit operator uint(UIntPtr value)
131     {
132         return (uint)value._pointer;
133     }
134     
135     [
136         CLSCompliant(false)
137     ]
138     public static explicit operator UIntPtr(ulong value)
139     {
140         return new UIntPtr(value);
141     }
142
143     [
144         CLSCompliant(false)
145     ]
146     public unsafe static explicit operator UIntPtr(void* value)
147     {
148         return new UIntPtr(value);
149     }
150
151     [
152         CLSCompliant(false)
153     ]
154     public unsafe static explicit operator void*(UIntPtr value)
155     {
156         return value.ToPointer();
157     }
158     
159     [
160         CLSCompliant(false)
161     ]
162     public static explicit operator UIntPtr(uint value)
163     {
164         return new UIntPtr(value);
165     }
166
167     public static int Size
168     {
169         get
170         {   
171             return sizeof(void*); 
172         }
173     }
174 }
175
176 } // Namespace
177