5 // Michael Lambert (michaellambert@email.com)
7 // (C) 2001 Michael Lambert, All Rights Reserved
10 // 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.
17 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
19 // Permission is hereby granted, free of charge, to any person obtaining
20 // a copy of this software and associated documentation files (the
21 // "Software"), to deal in the Software without restriction, including
22 // without limitation the rights to use, copy, modify, merge, publish,
23 // distribute, sublicense, and/or sell copies of the Software, and to
24 // permit persons to whom the Software is furnished to do so, subject to
25 // the following conditions:
27 // The above copyright notice and this permission notice shall be
28 // included in all copies or substantial portions of the Software.
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39 using System
.Globalization
;
40 using System
.Runtime
.Serialization
;
41 using System
.Runtime
.InteropServices
;
42 using System
.Runtime
.ConstrainedExecution
;
47 [CLSCompliant (false)]
48 [System
.Runtime
.InteropServices
.ComVisible (true)]
49 public unsafe struct UIntPtr
: ISerializable
51 public static readonly UIntPtr Zero
= new UIntPtr (0u);
52 private void* _pointer
;
54 public UIntPtr (ulong value)
56 if ((value > UInt32
.MaxValue
) && (UIntPtr
.Size
< 8)) {
57 throw new OverflowException (
58 Locale
.GetText ("This isn't a 64bits machine."));
61 _pointer
= (void*) value;
64 public UIntPtr (uint value)
66 _pointer
= (void*)value;
69 [CLSCompliant (false)]
70 public unsafe UIntPtr (void* value)
75 public override bool Equals (object obj
)
77 if( obj
is UIntPtr
) {
78 UIntPtr obj2
= (UIntPtr
)obj
;
79 return this._pointer
== obj2
._pointer
;
84 public override int GetHashCode ()
89 public uint ToUInt32 ()
91 return (uint) _pointer
;
94 public ulong ToUInt64 ()
96 return (ulong) _pointer
;
99 [CLSCompliant (false)]
100 public unsafe void* ToPointer ()
105 public override string ToString ()
107 return ((uint) _pointer
).ToString();
110 // Interface ISerializable
111 void ISerializable
.GetObjectData (SerializationInfo info
, StreamingContext context
)
114 throw new ArgumentNullException ("info");
116 info
.AddValue ("pointer", (ulong)_pointer
);
119 public static bool operator == (UIntPtr value1
, UIntPtr value2
)
121 return value1
._pointer
== value2
._pointer
;
124 public static bool operator != (UIntPtr value1
, UIntPtr value2
)
126 return value1
._pointer
!= value2
._pointer
;
129 public static explicit operator ulong (UIntPtr
value)
131 return (ulong)value._pointer
;
134 public static explicit operator uint (UIntPtr
value)
136 return (uint)value._pointer
;
139 public static explicit operator UIntPtr (ulong value)
141 return new UIntPtr (value);
144 [CLSCompliant (false)]
145 public unsafe static explicit operator UIntPtr (void* value)
147 return new UIntPtr (value);
150 [CLSCompliant (false)]
151 public unsafe static explicit operator void* (UIntPtr
value)
153 return value.ToPointer ();
156 public static explicit operator UIntPtr (uint value)
158 return new UIntPtr (value);
161 public static int Size
{
162 get { return sizeof (void*); }
166 public static UIntPtr
Add (UIntPtr pointer
, int offset
)
168 return (UIntPtr
) (unchecked (((byte *) pointer
) + offset
));
171 public static UIntPtr
Subtract (UIntPtr pointer
, int offset
)
173 return (UIntPtr
) (unchecked (((byte *) pointer
) - offset
));
176 public static UIntPtr
operator + (UIntPtr pointer
, int offset
)
178 return (UIntPtr
) (unchecked (((byte *) pointer
) + offset
));
181 public static UIntPtr
operator - (UIntPtr pointer
, int offset
)
183 return (UIntPtr
) (unchecked (((byte *) pointer
) - offset
));