From 1bbc749640de21970bd2aaecb8575436185d07bb Mon Sep 17 00:00:00 2001 From: Vladimir Savkin Date: Wed, 29 Jul 2020 02:45:14 +0600 Subject: [PATCH] refactor CalcResult class --- csharp/Platform.Numbers/CalcResult.cs | 328 +++++++++++++++++++++----- 1 file changed, 274 insertions(+), 54 deletions(-) diff --git a/csharp/Platform.Numbers/CalcResult.cs b/csharp/Platform.Numbers/CalcResult.cs index eb06c42..2e3498f 100644 --- a/csharp/Platform.Numbers/CalcResult.cs +++ b/csharp/Platform.Numbers/CalcResult.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.CompilerServices; namespace Platform.Numbers { @@ -13,6 +14,8 @@ public CalcResult(T value) } #region Arithmetic operations + + [MethodImpl(MethodImplOptions.AggressiveInlining)] public CalcResult Add(T a) { if (IsValid) @@ -30,6 +33,7 @@ public CalcResult Add(T a) return this; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public CalcResult Subtract(T a) { if (IsValid) @@ -47,6 +51,7 @@ public CalcResult Subtract(T a) return this; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public CalcResult Multiply(T a) { if (IsValid) @@ -64,6 +69,7 @@ public CalcResult Multiply(T a) return this; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public CalcResult Divide(T a) { if (IsValid) @@ -81,74 +87,107 @@ public CalcResult Divide(T a) return this; } - public CalcResult Add(CalcResult a) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public CalcResult Add(CalcResult cr) { - if (IsValid &= a.IsValid) + if (IsValid) { - try + if (cr.IsValid) { - _value = Arithmetic.Add(_value, a._value); + try + { + _value = Arithmetic.Add(_value, cr._value); + } + catch (Exception e) + { + Message = e.Message; + IsValid = false; + } } - catch (Exception e) + else { - Message = e.Message; - IsValid = false; + return cr; } } return this; } - public CalcResult Subtract(CalcResult a) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public CalcResult Subtract(CalcResult cr) { - if (IsValid &= a.IsValid) + if (IsValid) { - try + if (cr.IsValid) { - _value = Arithmetic.Subtract(_value, a._value); + try + { + _value = Arithmetic.Subtract(_value, cr._value); + } + catch (Exception e) + { + Message = e.Message; + IsValid = false; + } } - catch (Exception e) + else { - Message = e.Message; - IsValid = false; + return cr; } } return this; } - public CalcResult Multiply(CalcResult a) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public CalcResult Multiply(CalcResult cr) { - if (IsValid &= a.IsValid) + if (IsValid) { - try + if (cr.IsValid) { - _value = Arithmetic.Multiply(_value, a._value); + try + { + _value = Arithmetic.Multiply(_value, cr._value); + } + catch (Exception e) + { + Message = e.Message; + IsValid = false; + } } - catch (Exception e) + else { - Message = e.Message; - IsValid = false; + return cr; } } return this; } - public CalcResult Divide(CalcResult a) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public CalcResult Divide(CalcResult cr) { - if (IsValid &= a.IsValid) + if (IsValid) { - try + if (cr.IsValid) { - _value = Arithmetic.Divide(_value, a._value); + try + { + _value = Arithmetic.Divide(_value, cr._value); + } + catch (Exception e) + { + Message = e.Message; + IsValid = false; + } } - catch (Exception e) + else { - Message = e.Message; - IsValid = false; + return cr; } } return this; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public CalcResult Increment() { if (IsValid) @@ -166,6 +205,7 @@ public CalcResult Increment() return this; } + [MethodImpl(MethodImplOptions.AggressiveInlining)] public CalcResult Decrement() { if (IsValid) @@ -184,9 +224,9 @@ public CalcResult Decrement() } #endregion - public T Value - { - get => _value; + public T Value + { + get => _value; set { _value = value; @@ -201,41 +241,221 @@ public T Value private T _value; #region Operator overloading - public static CalcResult operator ++(CalcResult x) => x.Increment(); - - public static CalcResult operator --(CalcResult x) => x.Decrement(); - - public static CalcResult operator +(CalcResult x, T y) => x.Add(y); - public static CalcResult operator -(CalcResult x, T y) => x.Subtract(y); - - public static CalcResult operator *(CalcResult x, T y) => x.Multiply(y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator ++(CalcResult cr) + { + if (cr.IsValid) + { + try + { + cr._value = Arithmetic.Increment(cr._value); + } + catch (Exception e) + { + cr.Message = e.Message; + cr.IsValid = false; + } + } + return cr; + } - public static CalcResult operator /(CalcResult x, T y) => x.Divide(y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator --(CalcResult cr) + { + if (cr.IsValid) + { + try + { + cr._value = Arithmetic.Decrement(cr._value); + } + catch (Exception e) + { + cr.Message = e.Message; + cr.IsValid = false; + } + } + return cr; + } - public static CalcResult operator +(CalcResult x, CalcResult y) => x.Add(y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator +(CalcResult cr, T v) + { + if (cr.IsValid) + { + try + { + cr._value = Arithmetic.Add(cr._value, v); + } + catch (Exception e) + { + cr.Message = e.Message; + cr.IsValid = false; + } + } + return cr; + } - public static CalcResult operator -(CalcResult x, CalcResult y) => x.Subtract(y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator -(CalcResult cr, T v) + { + if (cr.IsValid) + { + try + { + cr._value = Arithmetic.Subtract(cr._value, v); + } + catch (Exception e) + { + cr.Message = e.Message; + cr.IsValid = false; + } + } + return cr; + } - public static CalcResult operator *(CalcResult x, CalcResult y) => x.Multiply(y); + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator *(CalcResult cr, T v) + { + if (cr.IsValid) + { + try + { + cr._value = Arithmetic.Multiply(cr._value, v); + } + catch (Exception e) + { + cr.Message = e.Message; + cr.IsValid = false; + } + } + return cr; + } - public static CalcResult operator /(CalcResult x, CalcResult y) => x.Divide(y); - #endregion + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator /(CalcResult cr, T v) + { + if (cr.IsValid) + { + try + { + cr._value = Arithmetic.Divide(cr._value, v); + } + catch (Exception e) + { + cr.Message = e.Message; + cr.IsValid = false; + } + } + return cr; + } - //public static explicit operator CalcResult(T value) - //{ - // return new CalcResult(value); - //} + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator +(CalcResult a, CalcResult b) + { + if (a.IsValid) + { + if (b.IsValid) + { + try + { + a._value = Arithmetic.Add(a._value, b._value); + } + catch (Exception e) + { + a.Message = e.Message; + a.IsValid = false; + } + } + else + { + return b; + } + } + return a; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator -(CalcResult a, CalcResult b) + { + if (a.IsValid) + { + if (b.IsValid) + { + try + { + a._value = Arithmetic.Subtract(a._value, b._value); + } + catch (Exception e) + { + a.Message = e.Message; + a.IsValid = false; + } + } + else + { + return b; + } + } + return a; + } - public static implicit operator T(CalcResult v) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator *(CalcResult a, CalcResult b) { - return v.Value; + if (a.IsValid) + { + if (b.IsValid) + { + try + { + a._value = Arithmetic.Multiply(a._value, b._value); + } + catch (Exception e) + { + a.Message = e.Message; + b.IsValid = false; + } + } + else + { + return b; + } + } + return a; } - public static implicit operator CalcResult(T v) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static CalcResult operator /(CalcResult a, CalcResult b) { - return new CalcResult(v); + if (a.IsValid) + { + if (b.IsValid) + { + try + { + a._value = Arithmetic.Divide(a._value, b._value); + } + catch (Exception e) + { + a.Message = e.Message; + a.IsValid = false; + } + } + else + { + return b; + } + } + return a; } + + #endregion + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator bool(CalcResult v) => v.IsValid; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static implicit operator CalcResult(T v) => new CalcResult(v); } -} +} \ No newline at end of file