Skip to content

Commit cc871da

Browse files
committed
fix: fix many clang tidy errors (4/x)
this is part 4 of 143 errors in the whole codebase, it is split into multiple commits, so that each patch is not to big
1 parent 5952e37 commit cc871da

File tree

6 files changed

+72
-32
lines changed

6 files changed

+72
-32
lines changed

src/libs/core/helper/color.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ namespace { //NOLINT(cert-dcl59-cpp,google-build-namespaces)
8787
return std::fmod(value, divisor);
8888
}
8989

90-
return value - static_cast<T>(static_cast<u64>(value / divisor)) * divisor;
90+
return value - (static_cast<T>(static_cast<u64>(value / divisor)) * divisor);
9191
}
9292

9393
template<typename T>

src/libs/core/helper/errors.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "./types.hpp"
66
#include "./windows.hpp"
77

8+
#include <core/helper/types.hpp>
9+
810
#include <exception>
911
#include <string>
1012

src/libs/core/helper/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ core_src_files += files(
66
'random.cpp',
77
'sleep.cpp',
88
'string_manipulation.cpp',
9+
'timer.cpp',
910
)
1011

1112
_header_files = files(

src/libs/core/helper/point.hpp

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/libs/core/helper/timer.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
3+
#include "./timer.hpp"
4+
5+
6+
helper::Timer::Timer(Callback callback, const Duration& interval)
7+
: m_callback{ std::move(callback) },
8+
m_interval{ interval } { }
9+
10+
void helper::Timer::start() {
11+
m_running = true;
12+
m_last_measured = Clock::now();
13+
}
14+
15+
void helper::Timer::stop() {
16+
m_running = false;
17+
}
18+
19+
void helper::Timer::check() {
20+
if (not m_running) {
21+
return;
22+
}
23+
24+
const auto now = Clock::now();
25+
26+
const auto difference = now - m_last_measured;
27+
28+
if (difference >= m_interval) {
29+
m_callback();
30+
m_last_measured = now;
31+
}
32+
}

src/libs/core/helper/timer.hpp

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,19 @@ namespace helper {
1414
using Duration = std::chrono::nanoseconds;
1515

1616
bool m_running{ false };
17-
std::chrono::time_point<Clock> last_measured;
17+
std::chrono::time_point<Clock> m_last_measured;
1818
Callback m_callback;
1919
Duration m_interval;
2020

2121

2222
public:
23-
Timer(Callback callback, const Duration& interval)
24-
: m_callback{ std::move(callback) },
25-
m_interval{ interval } { }
23+
Timer(Callback callback, const Duration& interval);
2624

27-
void start() {
28-
m_running = true;
29-
last_measured = Clock::now();
30-
}
25+
void start();
3126

32-
void stop() {
33-
m_running = false;
34-
}
27+
void stop();
3528

36-
void check() {
37-
if (not m_running) {
38-
return;
39-
}
40-
41-
const auto now = Clock::now();
42-
43-
const auto difference = now - last_measured;
44-
45-
if (difference >= m_interval) {
46-
m_callback();
47-
last_measured = now;
48-
}
49-
}
29+
void check();
5030
};
5131

5232

0 commit comments

Comments
 (0)