@@ -15,7 +15,9 @@ namespace shapes {
1515 T y;
1616
1717 constexpr AbstractPoint () : AbstractPoint{ 0 , 0 } { }
18- constexpr AbstractPoint (T x, T y) : x{ x }, y{ y } { } // NOLINT(bugprone-easily-swappable-parameters)
18+ constexpr AbstractPoint (T x_pos, T y_pos)
19+ : x{ x_pos },
20+ y{ y_pos } { } // NOLINT(bugprone-easily-swappable-parameters)
1921
2022 static constexpr AbstractPoint<T> zero () {
2123 return AbstractPoint<T>{ 0 , 0 };
@@ -46,7 +48,7 @@ namespace shapes {
4648 }
4749
4850 constexpr AbstractPoint<T> operator -(AbstractPoint<T> rhs) const {
49- if constexpr (std::is_signed <T>::value ) {
51+ if constexpr (std::is_signed_v <T>) {
5052 return *this + (-rhs);
5153 } else {
5254 assert (x >= rhs.x && y >= rhs.y && " underflow in subtraction" );
@@ -66,11 +68,34 @@ namespace shapes {
6668
6769 template <typename S>
6870 constexpr AbstractPoint<S> cast () const {
69- if constexpr (std::is_signed<T>::value and not std::is_signed<T>::value) {
70- assert (x >= 0 && y >= 0 && " Not allowed to cast away negative number into an unsigned type" );
71- } else {
72- return AbstractPoint<S>{ static_cast <S>(x), static_cast <S>(y) };
71+ assert (x >= std::numeric_limits<S>::min () && y >= std::numeric_limits<S>::min ()
72+ && " cast invalid, value to small" );
73+ assert (x <= std::numeric_limits<S>::max () && y >= std::numeric_limits<S>::max ()
74+ && " cast invalid, value to big" );
75+
76+ return AbstractPoint<S>{ static_cast <S>(x), static_cast <S>(y) };
77+ }
78+
79+ template <typename S>
80+ constexpr AbstractPoint<S> cast_truncate () const {
81+
82+ auto x_final = x;
83+
84+ if (x < std::numeric_limits<S>::min ()) {
85+ x_final = std::numeric_limits<S>::min ();
86+ } else if (x > std::numeric_limits<S>::max ()) {
87+ x_final = std::numeric_limits<S>::max ();
7388 }
89+
90+ auto y_final = y;
91+
92+ if (y < std::numeric_limits<S>::min ()) {
93+ y_final = std::numeric_limits<S>::min ();
94+ } else if (y > std::numeric_limits<S>::max ()) {
95+ y_final = std::numeric_limits<S>::max ();
96+ }
97+
98+ return AbstractPoint<S>{ static_cast <S>(x_final), static_cast <S>(y_final) };
7499 }
75100 };
76101
0 commit comments