1818#include < atomic>
1919#include < chrono>
2020#include < functional>
21+ #include < optional>
2122#include < memory>
2223#include < sstream>
2324#include < thread>
4344namespace rclcpp
4445{
4546
47+ struct TimerInfo
48+ {
49+ Time expected_call_time;
50+ Time actual_call_time;
51+ };
52+
4653class TimerBase
4754{
4855public:
@@ -96,16 +103,19 @@ class TimerBase
96103 * The multithreaded executor takes advantage of this to avoid scheduling
97104 * the callback multiple times.
98105 *
99- * \return `true` if the callback should be executed, `false` if the timer was canceled.
106+ * \return a shared_ptr if the callback should be executed, std::nullopt if the timer was canceled.
100107 */
101108 RCLCPP_PUBLIC
102- virtual bool
109+ virtual std::optional<std::shared_ptr< void >>
103110 call () = 0 ;
104111
105112 // / Call the callback function when the timer signal is emitted.
113+ /* *
114+ * \param[in] data the pointer returned by the call function
115+ */
106116 RCLCPP_PUBLIC
107117 virtual void
108- execute_callback () = 0 ;
118+ execute_callback (const std::shared_ptr< void > & data ) = 0 ;
109119
110120 RCLCPP_PUBLIC
111121 std::shared_ptr<const rcl_timer_t >
@@ -193,11 +203,6 @@ class TimerBase
193203 set_on_reset_callback (rcl_event_callback_t callback, const void * user_data);
194204};
195205
196- struct TimerInfo
197- {
198- Time expected_call_time;
199- Time actual_call_time;
200- };
201206
202207using VoidCallbackType = std::function<void ()>;
203208using TimerCallbackType = std::function<void (TimerBase &)>;
@@ -257,27 +262,28 @@ class GenericTimer : public TimerBase
257262 * \sa rclcpp::TimerBase::call
258263 * \throws std::runtime_error if it failed to notify timer that callback will occurr
259264 */
260- bool
265+ std::optional<std::shared_ptr< void >>
261266 call () override
262267 {
268+ rcl_timer_call_info_t timer_call_info_;
263269 rcl_ret_t ret = rcl_timer_call_with_info (timer_handle_.get (), &timer_call_info_);
264270 if (ret == RCL_RET_TIMER_CANCELED) {
265- return false ;
271+ return std:: nullopt ;
266272 }
267273 if (ret != RCL_RET_OK) {
268274 throw std::runtime_error (" Failed to notify timer that callback occurred" );
269275 }
270- return true ;
276+ return std::make_shared< rcl_timer_call_info_t >(timer_call_info_) ;
271277 }
272278
273279 /* *
274280 * \sa rclcpp::TimerBase::execute_callback
275281 */
276282 void
277- execute_callback () override
283+ execute_callback (const std::shared_ptr< void > & data ) override
278284 {
279285 TRACEPOINT (callback_start, reinterpret_cast <const void *>(&callback_), false );
280- execute_callback_delegate<>();
286+ execute_callback_delegate<>(* static_cast < rcl_timer_call_info_t *>(data. get ()) );
281287 TRACEPOINT (callback_end, reinterpret_cast <const void *>(&callback_));
282288 }
283289
@@ -289,7 +295,7 @@ class GenericTimer : public TimerBase
289295 >::type * = nullptr
290296 >
291297 void
292- execute_callback_delegate ()
298+ execute_callback_delegate (const rcl_timer_call_info_t & )
293299 {
294300 callback_ ();
295301 }
@@ -301,7 +307,7 @@ class GenericTimer : public TimerBase
301307 >::type * = nullptr
302308 >
303309 void
304- execute_callback_delegate ()
310+ execute_callback_delegate (const rcl_timer_call_info_t & )
305311 {
306312 callback_ (*this );
307313 }
@@ -314,10 +320,10 @@ class GenericTimer : public TimerBase
314320 >::type * = nullptr
315321 >
316322 void
317- execute_callback_delegate ()
323+ execute_callback_delegate (const rcl_timer_call_info_t & timer_call_info_ )
318324 {
319325 const TimerInfo info{Time{timer_call_info_.expected_call_time , clock_->get_clock_type ()},
320- Time{timer_call_info_.actual_call_time , clock_->get_clock_type ()} };
326+ Time{timer_call_info_.actual_call_time , clock_->get_clock_type ()}};
321327 callback_ (info);
322328 }
323329
@@ -333,7 +339,6 @@ class GenericTimer : public TimerBase
333339 RCLCPP_DISABLE_COPY (GenericTimer)
334340
335341 FunctorT callback_;
336- rcl_timer_call_info_t timer_call_info_;
337342};
338343
339344template <
0 commit comments