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