From 4fce1842977971ae5a7400f48692c51ed4d42743 Mon Sep 17 00:00:00 2001 From: Lennart Nachtigall Date: Fri, 22 Mar 2024 08:01:23 +0100 Subject: [PATCH] Implemented suggestions --- .../realtime_tools/realtime_box_best_effort.h | 18 +++++++++--------- test/realtime_box_best_effort_tests.cpp | 4 ++++ 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/include/realtime_tools/realtime_box_best_effort.h b/include/realtime_tools/realtime_box_best_effort.h index fae1c3c9..4763c5a0 100644 --- a/include/realtime_tools/realtime_box_best_effort.h +++ b/include/realtime_tools/realtime_box_best_effort.h @@ -83,7 +83,7 @@ class RealtimeBoxBestEffort template typename std::enable_if_t, bool> trySet(const T & value) { - std::unique_lock guard(lock_, std::defer_lock); + std::unique_lock guard(lock_, std::defer_lock); if (!guard.try_lock()) { return false; } @@ -97,7 +97,7 @@ class RealtimeBoxBestEffort */ bool trySet(const std::function & func) { - std::unique_lock guard(lock_, std::defer_lock); + std::unique_lock guard(lock_, std::defer_lock); if (!guard.try_lock()) { return false; } @@ -112,7 +112,7 @@ class RealtimeBoxBestEffort template [[nodiscard]] typename std::enable_if_t, std::optional> tryGet() const { - std::unique_lock guard(lock_, std::defer_lock); + std::unique_lock guard(lock_, std::defer_lock); if (!guard.try_lock()) { return std::nullopt; } @@ -125,7 +125,7 @@ class RealtimeBoxBestEffort */ bool tryGet(const std::function & func) { - std::unique_lock guard(lock_, std::defer_lock); + std::unique_lock guard(lock_, std::defer_lock); if (!guard.try_lock()) { return false; } @@ -141,7 +141,7 @@ class RealtimeBoxBestEffort template typename std::enable_if_t, void> set(const T & value) { - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); // cppcheck-suppress missingReturn value_ = value; } @@ -150,7 +150,7 @@ class RealtimeBoxBestEffort */ void set(const std::function & func) { - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); func(value_); } @@ -161,7 +161,7 @@ class RealtimeBoxBestEffort template [[nodiscard]] typename std::enable_if_t, U> get() const { - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); return value_; } /** @@ -171,7 +171,7 @@ class RealtimeBoxBestEffort template typename std::enable_if_t, void> get(T & in) const { - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); // cppcheck-suppress missingReturn in = value_; } @@ -182,7 +182,7 @@ class RealtimeBoxBestEffort */ void get(const std::function & func) { - std::lock_guard guard(lock_); + std::lock_guard guard(lock_); func(value_); } diff --git a/test/realtime_box_best_effort_tests.cpp b/test/realtime_box_best_effort_tests.cpp index 16b869cc..1cda8186 100644 --- a/test/realtime_box_best_effort_tests.cpp +++ b/test/realtime_box_best_effort_tests.cpp @@ -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); }); }