mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 20:42:21 +02:00
New nullable support
This commit is contained in:
parent
59233cc996
commit
88adb3a1cd
16 changed files with 963 additions and 143 deletions
|
@ -25,11 +25,56 @@ namespace System
|
|||
static int operator<=>(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpComparable<TRight>
|
||||
{
|
||||
static int operator<=>(Self lhs, TRight rhs);
|
||||
}
|
||||
|
||||
interface IOpAddable
|
||||
{
|
||||
static Self operator+(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpSubtractable
|
||||
{
|
||||
static Self operator-(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpMultipliable
|
||||
{
|
||||
static Self operator*(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpDividable
|
||||
{
|
||||
static Self operator/(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpBitwiseAndable
|
||||
{
|
||||
static Self operator&(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpBitwiseOrable
|
||||
{
|
||||
static Self operator|(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpExclusiveOrable
|
||||
{
|
||||
static Self operator^(Self lhs, Self rhs);
|
||||
}
|
||||
|
||||
interface IOpLeftShiftable
|
||||
{
|
||||
static Self operator^(Self lhs, int rhs);
|
||||
}
|
||||
|
||||
interface IOpRightShiftable
|
||||
{
|
||||
static Self operator^(Self lhs, int rhs);
|
||||
}
|
||||
|
||||
interface IOpNegatable
|
||||
{
|
||||
static Self operator-(Self value);
|
||||
|
|
|
@ -4,7 +4,7 @@ using System.Diagnostics;
|
|||
|
||||
namespace System
|
||||
{
|
||||
public struct Nullable<T> where T : struct
|
||||
struct Nullable<T> where T : struct
|
||||
{
|
||||
internal T mValue;
|
||||
internal bool mHasValue;
|
||||
|
@ -49,35 +49,6 @@ namespace System
|
|||
}
|
||||
}
|
||||
|
||||
/*public override bool Equals(Object other)
|
||||
{
|
||||
if (other == null)
|
||||
return mHasValue == false;
|
||||
if (!(other is Nullable<T>))
|
||||
return false;
|
||||
|
||||
return Equals((Nullable<T>)other);
|
||||
}*/
|
||||
|
||||
/*bool Equals(Nullable<T> other)
|
||||
{
|
||||
if (other.mHasValue != mHasValue)
|
||||
return false;
|
||||
|
||||
if (mHasValue == false)
|
||||
return true;
|
||||
|
||||
return other.mValue.Equals(mValue);
|
||||
}*/
|
||||
|
||||
/*public override int GetHashCode()
|
||||
{
|
||||
if (!mHasValue)
|
||||
return 0;
|
||||
|
||||
return mValue.GetHashCode();
|
||||
}*/
|
||||
|
||||
public T GetValueOrDefault()
|
||||
{
|
||||
return mValue;
|
||||
|
@ -96,16 +67,368 @@ namespace System
|
|||
str.Clear();
|
||||
}
|
||||
|
||||
//[Inline]
|
||||
[Inline]
|
||||
public static implicit operator Nullable<T>(T value)
|
||||
{
|
||||
return Nullable<T>(value);
|
||||
}
|
||||
|
||||
//[Inline]
|
||||
[Inline]
|
||||
public static explicit operator T(Nullable<T> value)
|
||||
{
|
||||
return value.mValue;
|
||||
}
|
||||
|
||||
[Inline]
|
||||
public static bool operator==(Nullable<T> lhs, T rhs)
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue == rhs;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static bool operator==<TOther>(Nullable<T> lhs, TOther rhs) where bool : operator T == TOther
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue == rhs;
|
||||
}
|
||||
|
||||
public static bool operator==<TOther>(TOther lhs, Nullable<T> rhs) where bool : operator TOther == T
|
||||
{
|
||||
if (!rhs.mHasValue) return false;
|
||||
return lhs == rhs;
|
||||
}
|
||||
|
||||
public static bool operator==<TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where bool : operator T == TOther where TOther : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return false;
|
||||
return lhs.mValue == rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static bool operator!=<TOther>(Nullable<T> lhs, TOther rhs) where bool : operator T != TOther
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue != rhs;
|
||||
}
|
||||
|
||||
public static bool operator!=<TOther>(TOther lhs, Nullable<T> rhs) where bool : operator TOther != T
|
||||
{
|
||||
if (!rhs.mHasValue) return false;
|
||||
return lhs != rhs;
|
||||
}
|
||||
|
||||
public static bool operator!=<TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where bool : operator T != TOther where TOther : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return false;
|
||||
return lhs.mValue != rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static bool operator< <TOther>(Nullable<T> lhs, TOther rhs) where bool : operator T < TOther
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue < rhs;
|
||||
}
|
||||
|
||||
public static bool operator< <TOther>(TOther lhs, Nullable<T> rhs) where bool : operator TOther < T
|
||||
{
|
||||
if (!rhs.mHasValue) return false;
|
||||
return lhs < rhs;
|
||||
}
|
||||
|
||||
public static bool operator< <TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where bool : operator T < TOther where TOther : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return false;
|
||||
return lhs.mValue < rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static bool operator<=<TOther>(Nullable<T> lhs, TOther rhs) where bool : operator T <= TOther
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue <= rhs;
|
||||
}
|
||||
|
||||
public static bool operator<=<TOther>(TOther lhs, Nullable<T> rhs) where bool : operator TOther <= T
|
||||
{
|
||||
if (!rhs.mHasValue) return false;
|
||||
return lhs <= rhs;
|
||||
}
|
||||
|
||||
public static bool operator<=<TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where bool : operator T <= TOther where TOther : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return false;
|
||||
return lhs.mValue <= rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static bool operator><TOther>(Nullable<T> lhs, TOther rhs) where bool : operator T > TOther
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue > rhs;
|
||||
}
|
||||
|
||||
public static bool operator><TOther>(TOther lhs, Nullable<T> rhs) where bool : operator TOther > T
|
||||
{
|
||||
if (!rhs.mHasValue) return false;
|
||||
return lhs > rhs;
|
||||
}
|
||||
|
||||
public static bool operator><TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where bool : operator T > TOther where TOther : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return false;
|
||||
return lhs.mValue > rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static bool operator>=<TOther>(Nullable<T> lhs, TOther rhs) where bool : operator T >= TOther
|
||||
{
|
||||
if (!lhs.mHasValue) return false;
|
||||
return lhs.mValue >= rhs;
|
||||
}
|
||||
|
||||
public static bool operator>=<TOther>(TOther lhs, Nullable<T> rhs) where bool : operator TOther >= T
|
||||
{
|
||||
if (!rhs.mHasValue) return false;
|
||||
return lhs >= rhs;
|
||||
}
|
||||
|
||||
public static bool operator>=<TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where bool : operator T >= TOther where TOther : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return false;
|
||||
return lhs.mValue >= rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static int operator<=><TOther>(Nullable<T> lhs, TOther rhs) where int : operator T <=> TOther
|
||||
{
|
||||
return lhs.mValue <=> rhs;
|
||||
}
|
||||
|
||||
public static int operator<=><TOther>(TOther lhs, Nullable<T> rhs) where int : operator TOther <=> T
|
||||
{
|
||||
return lhs <=> rhs;
|
||||
}
|
||||
|
||||
public static int operator<=><TOther>(Nullable<T> lhs, Nullable<TOther> rhs) where int : operator T <=> TOther where TOther : struct
|
||||
{
|
||||
return lhs.mValue <=> rhs.mValue;
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static TResult? operator+<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T + TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue + rhs);
|
||||
}
|
||||
public static TResult? operator+<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther + T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs + rhs.mValue);
|
||||
}
|
||||
public static TResult? operator+<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T + TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue + rhs.mValue);
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
public static TResult? operator-<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther - T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs - rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator-<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T - TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue - rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator-<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T - TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue - rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator*<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther * T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs * rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator*<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T * TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue * rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator*<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T * TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue * rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator/<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther / T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs / rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator/<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T / TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue / rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator/<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T / TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue / rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator%<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther % T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs % rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator%<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T % TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue % rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator%<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T % TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue % rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator^<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther ^ T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs ^ rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator^<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T ^ TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue ^ rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator^<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T ^ TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue ^ rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator&<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther & T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs & rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator&<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T & TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue & rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator&<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T & TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue & rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator|<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther | T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs | rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator|<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T | TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue | rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator|<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T | TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue | rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator??<TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther ?? T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs ?? rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator??<TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T ?? TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue ?? rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator??<TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T ?? TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue ?? rhs.mValue);
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
public static TResult? operator<< <TOther, TResult>(TOther lhs, Nullable<T> rhs) where TResult = operator TOther << T where TResult : struct
|
||||
{
|
||||
if (!rhs.mHasValue) return null;
|
||||
return .(lhs << rhs.mValue);
|
||||
}
|
||||
|
||||
public static TResult? operator<< <TOther, TResult>(Nullable<T> lhs, TOther rhs) where TResult = operator T << TOther where TResult : struct
|
||||
{
|
||||
if (!lhs.mHasValue) return null;
|
||||
return .(lhs.mValue << rhs);
|
||||
}
|
||||
|
||||
public static TResult? operator<< <TOther, TResult>(Nullable<T> lhs, Nullable<TOther> rhs) where TOther : struct where TResult = operator T << TOther where TResult : struct
|
||||
{
|
||||
if ((!lhs.mHasValue) || (!rhs.mHasValue)) return null;
|
||||
return .(lhs.mValue << rhs.mValue);
|
||||
}
|
||||
}
|
||||
|
||||
extension Nullable<T> : IHashable where T : IHashable
|
||||
{
|
||||
public int GetHashCode()
|
||||
{
|
||||
if (!mHasValue)
|
||||
return 0;
|
||||
return mValue.GetHashCode();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue