Files
Stephen Toub c256e409c9 More Corelib cleanup (dotnet/coreclr#26872)
* Remove unnecessary asserts

* Make several classes static

* Use is instead of as+null check

* Use T? instead of Nullable<T>

* more static classes

* Mark locals as const

* Merge declaration and initialization of some variables

* Remove unnecessary casts

* Remove unnecessary "unsafe"s

* Simplify several lambda expressions

* Remove redundant parentheses

* Remove redundant '== true'

* Remove redundant empty lines

* Simplify boolean comparison with '== false'

* Replace if-statement with return statement

* Use while for infinite loop

* Add static to all partial static class declarations

* Use ++/-- operator instead of assignment

* Use string.IsNullOrEmpty

* Use coalesce expression

* Simplify lazy initialization

* Use coalese expression

* Join string expressions

* Use regular string literal instead of verbatim string literal

* Optimize StringBuilder.Append calls

* Remove redundant assignment

* Remove unnecessary unsafe context

* Merge processor directives

* Use String.Equals instead of String.Compare

* Use Debug.Fail instead of Debug.Assert(false

* Remove Attribute suffix

* Use predefined type

* Use compound assignment

* Use while statement to create an infinite loop

* Remove redundant base ctor call

* Avoid using catch (Exception)

* Remove empty regions

* Span comparison to null

* Avoid unnecessary boxing of value type

* Expression is always equal to 'true'

* Remove unused method

* update coalesce assignment

* fix unsafe

* fix redundant parens

* Fix whitespace errors introduced

Signed-off-by: dotnet-bot <[email protected]>
2019-09-25 20:13:47 +02:00

186 lines
5.0 KiB
C#

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
#pragma warning disable SA1121 // explicitly using type aliases instead of built-in types
#if BIT64
using nuint = System.UInt64;
#else
using nuint = System.UInt32;
#endif
namespace System
{
[Serializable]
[CLSCompliant(false)]
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public readonly struct UIntPtr : IEquatable<UIntPtr>, ISerializable
{
private readonly unsafe void* _value; // Do not rename (binary serialization)
[Intrinsic]
public static readonly UIntPtr Zero;
[Intrinsic]
[NonVersionable]
public unsafe UIntPtr(uint value)
{
_value = (void*)value;
}
[Intrinsic]
[NonVersionable]
public unsafe UIntPtr(ulong value)
{
#if BIT64
_value = (void*)value;
#else
_value = (void*)checked((uint)value);
#endif
}
[Intrinsic]
[NonVersionable]
public unsafe UIntPtr(void* value)
{
_value = value;
}
private unsafe UIntPtr(SerializationInfo info, StreamingContext context)
{
ulong l = info.GetUInt64("value");
if (Size == 4 && l > uint.MaxValue)
throw new ArgumentException(SR.Serialization_InvalidPtrValue);
_value = (void*)l;
}
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
throw new ArgumentNullException(nameof(info));
info.AddValue("value", ToUInt64());
}
public override unsafe bool Equals(object? obj)
{
if (obj is UIntPtr)
{
return _value == ((UIntPtr)obj)._value;
}
return false;
}
unsafe bool IEquatable<UIntPtr>.Equals(UIntPtr other) =>
_value == other._value;
public override unsafe int GetHashCode()
{
#if BIT64
ulong l = (ulong)_value;
return unchecked((int)l) ^ (int)(l >> 32);
#else
return unchecked((int)_value);
#endif
}
[Intrinsic]
[NonVersionable]
public unsafe uint ToUInt32()
{
#if BIT64
return checked((uint)_value);
#else
return (uint)_value;
#endif
}
[Intrinsic]
[NonVersionable]
public unsafe ulong ToUInt64() => (ulong)_value;
[Intrinsic]
[NonVersionable]
public static explicit operator UIntPtr(uint value) =>
new UIntPtr(value);
[Intrinsic]
[NonVersionable]
public static explicit operator UIntPtr(ulong value) =>
new UIntPtr(value);
[Intrinsic]
[NonVersionable]
public static unsafe explicit operator UIntPtr(void* value) =>
new UIntPtr(value);
[Intrinsic]
[NonVersionable]
public static unsafe explicit operator void*(UIntPtr value) =>
value._value;
[Intrinsic]
[NonVersionable]
public static unsafe explicit operator uint(UIntPtr value) =>
#if BIT64
checked((uint)value._value);
#else
(uint)value._value;
#endif
[Intrinsic]
[NonVersionable]
public static unsafe explicit operator ulong(UIntPtr value) =>
(ulong)value._value;
[Intrinsic]
[NonVersionable]
public static unsafe bool operator ==(UIntPtr value1, UIntPtr value2) =>
value1._value == value2._value;
[Intrinsic]
[NonVersionable]
public static unsafe bool operator !=(UIntPtr value1, UIntPtr value2) =>
value1._value != value2._value;
[NonVersionable]
public static UIntPtr Add(UIntPtr pointer, int offset) =>
pointer + offset;
[Intrinsic]
[NonVersionable]
public static unsafe UIntPtr operator +(UIntPtr pointer, int offset) =>
new UIntPtr((nuint)pointer._value + (nuint)offset);
[NonVersionable]
public static UIntPtr Subtract(UIntPtr pointer, int offset) =>
pointer - offset;
[Intrinsic]
[NonVersionable]
public static unsafe UIntPtr operator -(UIntPtr pointer, int offset) =>
new UIntPtr((nuint)pointer._value - (nuint)offset);
public static int Size
{
[Intrinsic]
[NonVersionable]
get => sizeof(nuint);
}
[Intrinsic]
[NonVersionable]
public unsafe void* ToPointer() => _value;
public override unsafe string ToString() =>
((nuint)_value).ToString(CultureInfo.InvariantCulture);
}
}