Skip to content

Commit

Permalink
Implemented suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lennart Nachtigall committed Mar 22, 2024
1 parent 6499036 commit 4fce184
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
18 changes: 9 additions & 9 deletions include/realtime_tools/realtime_box_best_effort.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class RealtimeBoxBestEffort
template <typename U = T>
typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, bool> trySet(const T & value)
{
std::unique_lock<std::mutex> guard(lock_, std::defer_lock);
std::unique_lock<mutex_t> guard(lock_, std::defer_lock);
if (!guard.try_lock()) {
return false;
}
Expand All @@ -97,7 +97,7 @@ class RealtimeBoxBestEffort
*/
bool trySet(const std::function<void(T &)> & func)
{
std::unique_lock<std::mutex> guard(lock_, std::defer_lock);
std::unique_lock<mutex_t> guard(lock_, std::defer_lock);
if (!guard.try_lock()) {
return false;
}
Expand All @@ -112,7 +112,7 @@ class RealtimeBoxBestEffort
template <typename U = T>
[[nodiscard]] typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, std::optional<U>> tryGet() const
{
std::unique_lock<std::mutex> guard(lock_, std::defer_lock);
std::unique_lock<mutex_t> guard(lock_, std::defer_lock);
if (!guard.try_lock()) {
return std::nullopt;
}
Expand All @@ -125,7 +125,7 @@ class RealtimeBoxBestEffort
*/
bool tryGet(const std::function<void(const T &)> & func)
{
std::unique_lock<std::mutex> guard(lock_, std::defer_lock);
std::unique_lock<mutex_t> guard(lock_, std::defer_lock);
if (!guard.try_lock()) {
return false;
}
Expand All @@ -141,7 +141,7 @@ class RealtimeBoxBestEffort
template <typename U = T>
typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, void> set(const T & value)
{
std::lock_guard<std::mutex> guard(lock_);
std::lock_guard<mutex_t> guard(lock_);
// cppcheck-suppress missingReturn
value_ = value;
}
Expand All @@ -150,7 +150,7 @@ class RealtimeBoxBestEffort
*/
void set(const std::function<void(T &)> & func)
{
std::lock_guard<std::mutex> guard(lock_);
std::lock_guard<mutex_t> guard(lock_);
func(value_);
}

Expand All @@ -161,7 +161,7 @@ class RealtimeBoxBestEffort
template <typename U = T>
[[nodiscard]] typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, U> get() const
{
std::lock_guard<std::mutex> guard(lock_);
std::lock_guard<mutex_t> guard(lock_);
return value_;
}
/**
Expand All @@ -171,7 +171,7 @@ class RealtimeBoxBestEffort
template <typename U = T>
typename std::enable_if_t<!is_ptr_or_smart_ptr<U>, void> get(T & in) const
{
std::lock_guard<std::mutex> guard(lock_);
std::lock_guard<mutex_t> guard(lock_);
// cppcheck-suppress missingReturn
in = value_;
}
Expand All @@ -182,7 +182,7 @@ class RealtimeBoxBestEffort
*/
void get(const std::function<void(const T &)> & func)
{
std::lock_guard<std::mutex> guard(lock_);
std::lock_guard<mutex_t> guard(lock_);
func(value_);
}

Expand Down
4 changes: 4 additions & 0 deletions test/realtime_box_best_effort_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,8 @@ TEST(RealtimeBoxBestEffort, smart_ptr_type)
box.set([](auto & i) { *i = 200; });

box.get([](const auto & i) { EXPECT_EQ(*i, 200); });

box.trySet([](const auto & p) { *p = 10; });

box.tryGet([](const auto & p) { EXPECT_EQ(*p, 10); });
}

0 comments on commit 4fce184

Please sign in to comment.