| | 1 | | namespace NLightning.Domain.Protocol.ValueObjects; |
| | 2 | |
|
| | 3 | | using Domain.Interfaces; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// Represents a variable length integer. |
| | 7 | | /// </summary> |
| | 8 | | /// <param name="value">The value of the big size.</param> |
| | 9 | | /// <remarks> |
| | 10 | | /// Initializes a new instance of the <see cref="BigSize"/> struct. |
| | 11 | | /// </remarks> |
| 2724 | 12 | | public readonly record struct BigSize(ulong Value) : IValueObject, IComparable |
| | 13 | | { |
| | 14 | | #region Implicit Conversions |
| | 15 | |
|
| 28 | 16 | | public static implicit operator ulong(BigSize bigSize) => bigSize.Value; |
| 0 | 17 | | public static implicit operator BigSize(ulong value) => new(value); |
| | 18 | |
|
| | 19 | | public static implicit operator long(BigSize bigSize) |
| | 20 | | { |
| 28 | 21 | | if (bigSize.Value > long.MaxValue) |
| | 22 | | { |
| 4 | 23 | | throw new OverflowException($"Cannot convert {bigSize.Value} to long because it's too large."); |
| | 24 | | } |
| | 25 | |
|
| 24 | 26 | | return (long)bigSize.Value; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public static implicit operator BigSize(long value) |
| | 30 | | { |
| 16 | 31 | | if (value < 0) |
| | 32 | | { |
| 0 | 33 | | throw new OverflowException($"Cannot convert {value} to BigSize because it's negative."); |
| | 34 | | } |
| | 35 | |
|
| 16 | 36 | | return new BigSize((ulong)value); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public static implicit operator uint(BigSize bigSize) |
| | 40 | | { |
| 36 | 41 | | if (bigSize.Value > uint.MaxValue) |
| | 42 | | { |
| 8 | 43 | | throw new OverflowException($"Cannot convert {bigSize.Value} to uint because it's too large."); |
| | 44 | | } |
| | 45 | |
|
| 28 | 46 | | return (uint)bigSize.Value; |
| | 47 | | } |
| | 48 | |
|
| 0 | 49 | | public static implicit operator BigSize(uint value) => new(value); |
| | 50 | |
|
| | 51 | | public static implicit operator int(BigSize bigSize) |
| | 52 | | { |
| 340 | 53 | | if (bigSize.Value > int.MaxValue) |
| | 54 | | { |
| 12 | 55 | | throw new OverflowException($"Cannot convert {bigSize.Value} to int because it's too large."); |
| | 56 | | } |
| | 57 | |
|
| 328 | 58 | | return (int)bigSize.Value; |
| | 59 | | } |
| | 60 | |
|
| | 61 | | public static implicit operator BigSize(int value) |
| | 62 | | { |
| 980 | 63 | | if (value < 0) |
| | 64 | | { |
| 0 | 65 | | throw new OverflowException($"Cannot convert {value} to BigSize because it's negative."); |
| | 66 | | } |
| | 67 | |
|
| 980 | 68 | | return new BigSize((ulong)value); |
| | 69 | | } |
| | 70 | |
|
| | 71 | | public static implicit operator ushort(BigSize bigSize) |
| | 72 | | { |
| 36 | 73 | | if (bigSize.Value > ushort.MaxValue) |
| | 74 | | { |
| 16 | 75 | | throw new OverflowException($"Cannot convert {bigSize.Value} to ushort because it's too large."); |
| | 76 | | } |
| | 77 | |
|
| 20 | 78 | | return (ushort)bigSize.Value; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | public static implicit operator BigSize(ushort value) => new(value); |
| | 82 | |
|
| | 83 | | public static implicit operator short(BigSize bigSize) |
| | 84 | | { |
| 28 | 85 | | if (bigSize.Value > (ulong)short.MaxValue) |
| | 86 | | { |
| 20 | 87 | | throw new OverflowException($"Cannot convert {bigSize.Value} to short because it's too large."); |
| | 88 | | } |
| | 89 | |
|
| 8 | 90 | | return (short)bigSize.Value; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public static implicit operator BigSize(short value) |
| | 94 | | { |
| 0 | 95 | | if (value < 0) |
| | 96 | | { |
| 0 | 97 | | throw new OverflowException($"Cannot convert {value} to BigSize because it's negative."); |
| | 98 | | } |
| | 99 | |
|
| 0 | 100 | | return new BigSize((ulong)value); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | public static implicit operator byte(BigSize bigSize) |
| | 104 | | { |
| 180 | 105 | | if (bigSize.Value > byte.MaxValue) |
| | 106 | | { |
| 24 | 107 | | throw new OverflowException($"Cannot convert {bigSize.Value} to byte because it's too large."); |
| | 108 | | } |
| | 109 | |
|
| 156 | 110 | | return (byte)bigSize.Value; |
| | 111 | | } |
| | 112 | |
|
| 0 | 113 | | public static implicit operator BigSize(byte value) => new(value); |
| | 114 | |
|
| | 115 | | #endregion |
| | 116 | |
|
| | 117 | | #region Equality |
| | 118 | |
|
| | 119 | | public int CompareTo(object? obj) |
| | 120 | | { |
| 340 | 121 | | if (obj is not BigSize bigSize) |
| 0 | 122 | | throw new ArgumentException("Object is not a BigSize"); |
| | 123 | |
|
| 340 | 124 | | return Value.CompareTo(bigSize.Value); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public static bool operator <(BigSize left, BigSize right) |
| | 128 | | { |
| 248 | 129 | | return left.Value < right.Value; |
| | 130 | | } |
| | 131 | |
|
| | 132 | | public static bool operator >(BigSize left, BigSize right) |
| | 133 | | { |
| 16 | 134 | | return left.Value > right.Value; |
| | 135 | | } |
| | 136 | |
|
| | 137 | | public static bool operator <=(BigSize left, BigSize right) |
| | 138 | | { |
| 16 | 139 | | return left.Value <= right.Value; |
| | 140 | | } |
| | 141 | |
|
| | 142 | | public static bool operator >=(BigSize left, BigSize right) |
| | 143 | | { |
| 16 | 144 | | return left.Value >= right.Value; |
| | 145 | | } |
| | 146 | |
|
| | 147 | | #endregion |
| | 148 | | } |