Skip to content

Commit 4efcd4c

Browse files
Only accept callable with T& (#372)
1 parent b2165c8 commit 4efcd4c

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

realtime_tools/include/realtime_tools/realtime_thread_safe_box.hpp

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -205,17 +205,26 @@ class RealtimeThreadSafeBox
205205

206206
/**
207207
* @brief wait until the mutex could be locked and access the content (rw)
208+
* @note Only accepts callables that take T& as argument (not by value).
208209
*/
209-
void set(const std::function<void(T &)> & func)
210+
template <
211+
typename F,
212+
typename = std::enable_if_t<std::is_invocable_v<F, T &> && !std::is_invocable_v<F, T>>>
213+
void set(F && func)
210214
{
211215
std::lock_guard<mutex_t> guard(lock_);
212-
if (!func) {
213-
if constexpr (is_ptr_or_smart_ptr<T>) {
214-
value_ = nullptr;
215-
return;
216-
}
217-
}
218-
func(value_);
216+
std::forward<F>(func)(value_);
217+
}
218+
219+
/**
220+
* @brief wait until the mutex could be locked and access the content (rw)
221+
* @note Overload to allow setting pointer types to nullptr directly.
222+
*/
223+
template <typename U = T>
224+
typename std::enable_if_t<is_ptr_or_smart_ptr<U>, void> set(std::nullptr_t)
225+
{
226+
std::lock_guard<mutex_t> guard(lock_);
227+
value_ = nullptr;
219228
}
220229

221230
/**

0 commit comments

Comments
 (0)