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,20 @@ 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 valid shared_ptr if the callback should be executed,
107+ * an invalid shared_ptr (nullptr) if the timer was canceled.
100108 */
101109 RCLCPP_PUBLIC
102- virtual bool
110+ virtual std::shared_ptr< void >
103111 call () = 0 ;
104112
105113 // / Call the callback function when the timer signal is emitted.
114+ /* *
115+ * \param[in] data the pointer returned by the call function
116+ */
106117 RCLCPP_PUBLIC
107118 virtual void
108- execute_callback () = 0 ;
119+ execute_callback (const std::shared_ptr< void > & data ) = 0 ;
109120
110121 RCLCPP_PUBLIC
111122 std::shared_ptr<const rcl_timer_t >
@@ -193,12 +204,6 @@ class TimerBase
193204 set_on_reset_callback (rcl_event_callback_t callback, const void * user_data);
194205};
195206
196- struct TimerInfo
197- {
198- Time expected_call_time;
199- Time actual_call_time;
200- };
201-
202207using VoidCallbackType = std::function<void ()>;
203208using TimerCallbackType = std::function<void (TimerBase &)>;
204209using TimerInfoCallbackType = std::function<void (const TimerInfo &)>;
@@ -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::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 nullptr ;
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,7 +320,7 @@ 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 ()},
320326 Time{timer_call_info_.actual_call_time , clock_->get_clock_type ()}};
@@ -333,14 +339,14 @@ 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 <
340345 typename FunctorT,
341346 typename std::enable_if<
342347 rclcpp::function_traits::same_arguments<FunctorT, VoidCallbackType>::value ||
343- rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value
348+ rclcpp::function_traits::same_arguments<FunctorT, TimerCallbackType>::value ||
349+ rclcpp::function_traits::same_arguments<FunctorT, TimerInfoCallbackType>::value
344350 >::type * = nullptr
345351>
346352class WallTimer : public GenericTimer <FunctorT>
0 commit comments