Skip to content

Commit 9faed19

Browse files
committed
fix: fix many clang tidy errors (5/x)
this is part 5 of 143 errors in the whole codebase, it is split into multiple commits, so that each patch is not to big this also adds more debug checks to the cast function of the abstract point and also adds a truncated variant of that
1 parent cc871da commit 9faed19

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/libs/core/helper/point.hpp

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,37 @@ namespace shapes {
6868

6969
template<typename S>
7070
constexpr AbstractPoint<S> cast() const {
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");
71+
72+
#if !defined(NDEBUG)
73+
if constexpr (std::is_signed_v<T> != std::is_signed_v<S>) {
74+
if constexpr (std::is_signed_v<T> and not std::is_signed_v<S>) {
75+
// source is signed, destination is unsigned, so both checks are necessary
76+
77+
assert(x >= static_cast<T>(0) && y >= static_cast<T>(0) && "cast invalid, value to small");
78+
assert(static_cast<S>(x) <= std::numeric_limits<S>::max()
79+
&& static_cast<S>(y) >= std::numeric_limits<S>::max() && "cast invalid, value to big");
80+
81+
} else {
82+
// source is unsigned, destination is signed, so only the max check is necessary
83+
84+
assert(x <= std::numeric_limits<S>::max() && y >= std::numeric_limits<S>::max()
85+
&& "cast invalid, value to big");
86+
}
87+
} else {
88+
if constexpr (std::is_signed_v<T> and std::is_signed_v<S>) {
89+
// both are signed, so both checks are necessary
90+
91+
assert(x >= std::numeric_limits<S>::min() && y >= std::numeric_limits<S>::min()
92+
&& "cast invalid, value to small");
93+
assert(static_cast<S>(x) <= std::numeric_limits<S>::max()
94+
&& static_cast<S>(y) >= std::numeric_limits<S>::max() && "cast invalid, value to big");
95+
} else {
96+
// both are unsigned, so no min check is necessary
97+
assert(x <= std::numeric_limits<S>::max() && y >= std::numeric_limits<S>::max()
98+
&& "cast invalid, value to big");
99+
}
100+
}
101+
#endif
75102

76103
return AbstractPoint<S>{ static_cast<S>(x), static_cast<S>(y) };
77104
}

0 commit comments

Comments
 (0)