Last active
December 24, 2021 12:07
-
-
Save HassakuTb/00e20eedc7227bc09db7f07671abba46 to your computer and use it in GitHub Desktop.
template of numeric value type for rider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// ReSharper disable RedundantCast | |
// ReSharper disable UnusedMember.Global | |
// ReSharper disable RedundantOverflowCheckingContext | |
namespace $Namespace${ | |
public readonly struct $StructName$ : IEquatable<$StructName$>, IComparable<$StructName$> { | |
readonly $Type$ value; | |
public $StructName$($Type$ value){ | |
this.value = value; | |
} | |
public $Type$ AsPrimitive() => value; | |
public bool Equals($StructName$ other) => value.Equals(other.value); | |
public override bool Equals(object obj) | |
{ | |
if (obj == null) return false; | |
var t = obj.GetType(); | |
if (t == typeof($StructName$)) | |
{ | |
return Equals(($StructName$)obj); | |
} | |
if (t == typeof($Type$)) | |
{ | |
return value.Equals(($Type$)obj); | |
} | |
return value.Equals(obj); | |
} | |
public override int GetHashCode() => value.GetHashCode(); | |
public override string ToString() => $"$StructName$({value})"; | |
public static bool operator ==(in $StructName$ x, in $StructName$ y){ | |
return x.value.Equals(y.value); | |
} | |
public static bool operator !=(in $StructName$ x, in $StructName$ y){ | |
return !x.value.Equals(y.value); | |
} | |
public static $StructName$ operator +(in $StructName$ x, in $StructName$ y) => new(checked(($Type$)(x.value + y.value))); | |
public static $StructName$ operator -(in $StructName$ x, in $StructName$ y) => new(checked(($Type$)(x.value - y.value))); | |
public static $StructName$ operator *(in $StructName$ x, in $StructName$ y) => new(checked(($Type$)(x.value * y.value))); | |
public static $StructName$ operator /(in $StructName$ x, in $StructName$ y) => new(checked(($Type$)(x.value / y.value))); | |
public static $StructName$ operator ++(in $StructName$ x) => new(checked(($Type$)(x.value + 1))); | |
public static $StructName$ operator --(in $StructName$ x) => new(checked(($Type$)(x.value - 1))); | |
public static $StructName$ operator +(in $StructName$ x, in $Type$ y) => new(checked(($Type$)(x.value + y))); | |
public static $StructName$ operator -(in $StructName$ x, in $Type$ y) => new(checked(($Type$)(x.value - y))); | |
public static $StructName$ operator *(in $StructName$ x, in $Type$ y) => new(checked(($Type$)(x.value * y))); | |
public static $StructName$ operator /(in $StructName$ x, in $Type$ y) => new(checked(($Type$)(x.value / y))); | |
public int CompareTo($StructName$ other) => value.CompareTo(other.value); | |
public static bool operator >(in $StructName$ x, in $StructName$ y) => x.value > y.value; | |
public static bool operator <(in $StructName$ x, in $StructName$ y) => x.value < y.value; | |
public static bool operator >=(in $StructName$ x, in $StructName$ y) => x.value >= y.value; | |
public static bool operator <=(in $StructName$ x, in $StructName$ y) => x.value <= y.value; | |
public static $StructName$ Min($StructName$ x, $StructName$ y) => new (Math.Min(x.value, y.value)); | |
public static $StructName$ Max($StructName$ x, $StructName$ y) => new (Math.Max(x.value, y.value)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
// ReSharper disable RedundantCast | |
// ReSharper disable UnusedMember.Global | |
namespace $Namespace${ | |
public readonly struct $StructName$ : IEquatable<$StructName$> { | |
readonly $Type$ value; | |
public $StructName$($Type$ value){ | |
this.value = value; | |
} | |
public $Type$ AsPrimitive() => value; | |
public bool Equals($StructName$ other) => value.Equals(other.value); | |
public override bool Equals(object obj) | |
{ | |
if (obj == null) return false; | |
var t = obj.GetType(); | |
if (t == typeof($StructName$)) | |
{ | |
return Equals(($StructName$)obj); | |
} | |
return value.Equals(obj); | |
} | |
public override int GetHashCode() => value.GetHashCode(); | |
public override string ToString() => $"$StructName$({value})"; | |
public static bool operator ==(in $StructName$ x, in $StructName$ y){ | |
return x.value.Equals(y.value); | |
} | |
public static bool operator !=(in $StructName$ x, in $StructName$ y){ | |
return !x.value.Equals(y.value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment