10#ifndef NROS_RCLCPP_TIMER_HPP
11#define NROS_RCLCPP_TIMER_HPP
17#include "rclcpp/detail/hosted_block.hpp"
21#include "rclcpp/duration.hpp"
28#include "rclcpp/detail/nros_cpp_ffi.h"
31#include "rclcpp/clock.hpp"
33#include "rclcpp/inplace_fn.hpp"
52inline ClockValuePtr
timer_clock(
void* executor,
size_t handle_id) {
53 uint8_t clock_type =
static_cast<uint8_t
>(RCL_CLOCK_UNINITIALIZED);
54 if (executor ==
nullptr ||
55 nros_cpp_timer_get_clock_type(executor, handle_id, &clock_type) != NROS_CPP_RET_OK) {
56 clock_type =
static_cast<uint8_t
>(RCL_CLOCK_UNINITIALIZED);
58 return ClockValuePtr(
static_cast<rcl_clock_type_t
>(clock_type));
73#if defined(NROS_CPP_STD)
74 ::rclcpp::detail::StatementResult
cancel() {
82#if defined(NROS_CPP_STD)
83 ::rclcpp::detail::StatementResult
reset() {
112 return Duration::from_nanoseconds(ns);
121 constexpr TimerOps(
void* executor,
size_t handle_id)
169 explicit constexpr operator bool()
const {
return executor_ !=
nullptr; }
191 return a.executor_ == b.executor_ && a.handle_id_ == b.handle_id_;
197 return a.executor_ ==
nullptr;
200 return a.executor_ !=
nullptr;
203 return a.executor_ ==
nullptr;
206 return a.executor_ !=
nullptr;
246#if defined(NROS_CPP_STD)
247 ::rclcpp::detail::StatementResult
cancel() {
251 if (!initialized_)
return Result(ErrorCode::NotInitialized);
252 return Result(nros_cpp_timer_cancel(executor_, handle_id_));
257#if defined(NROS_CPP_STD)
258 ::rclcpp::detail::StatementResult
reset() {
262 if (!initialized_)
return Result(ErrorCode::NotInitialized);
263 return Result(nros_cpp_timer_reset(executor_, handle_id_));
268 if (!initialized_)
return true;
269 return nros_cpp_timer_is_canceled(executor_, handle_id_);
285 if (!initialized_)
return false;
286 return nros_cpp_timer_is_ready(executor_, handle_id_);
313 if (!initialized_)
return Duration();
315 if (nros_cpp_timer_time_until_next_call_ns(executor_, handle_id_, &ns) != NROS_CPP_RET_OK) {
318 return Duration::from_nanoseconds(ns);
337 return detail::timer_clock(initialized_ ? executor_ :
nullptr, handle_id_);
346 nros_cpp_timer_cancel(executor_, handle_id_);
347 initialized_ =
false;
351 detail::destroy_hosted_block(closure_);
356 : executor_(other.executor_), handle_id_(other.handle_id_),
357 initialized_(other.initialized_), closure_(other.closure_) {
358 other.executor_ =
nullptr;
359 other.initialized_ =
false;
360 other.closure_ =
nullptr;
364 if (
this != &other) {
366 nros_cpp_timer_cancel(executor_, handle_id_);
368 executor_ = other.executor_;
369 handle_id_ = other.handle_id_;
370 initialized_ = other.initialized_;
371 detail::destroy_hosted_block(closure_);
372 closure_ = other.closure_;
373 other.executor_ =
nullptr;
374 other.initialized_ =
false;
375 other.closure_ =
nullptr;
382 Timer() : executor_(nullptr), handle_id_(0), initialized_(false), closure_(nullptr) {}
396 detail::destroy_hosted_block(closure_);
404 friend class ::rclcpp::Node;
Timer & operator=(Timer &&other)
Definition timer.hpp:363
Timer(Timer &&other)
Definition timer.hpp:355
~Timer()
Destructor — cancels the timer.
Definition timer.hpp:344
Timer()
Definition timer.hpp:382
bool is_canceled() const
Check if the timer is cancelled.
Definition timer.hpp:267
Result reset()
Definition timer.hpp:260
::rclcpp::TimerHandle SharedPtr
Definition timer.hpp:236
bool is_ready() const
Definition timer.hpp:284
Duration time_until_trigger() const
Definition timer.hpp:312
void attach_closure_block(detail::HostedBlockBase *block)
Definition timer.hpp:395
Result cancel()
Definition timer.hpp:249
bool is_valid() const
Check if the timer is initialized and valid.
Definition timer.hpp:341
::rclcpp::TimerHandle UniquePtr
Definition timer.hpp:241
detail::ClockValuePtr get_clock() const
Definition timer.hpp:336
::rclcpp::TimerHandle ConstSharedPtr
Timer::ConstSharedPtr — the same handle; see SharedPtr.
Definition timer.hpp:238
friend constexpr bool operator!=(decltype(nullptr), const TimerHandle &a)
Definition timer.hpp:205
constexpr TimerHandle(decltype(nullptr))
Definition timer.hpp:163
friend constexpr bool operator==(decltype(nullptr), const TimerHandle &a)
Definition timer.hpp:202
friend constexpr bool operator==(const TimerHandle &a, decltype(nullptr))
Definition timer.hpp:196
::rclcpp::Timer element_type
Definition timer.hpp:158
constexpr TimerHandle(void *executor, size_t handle_id)
Definition timer.hpp:164
constexpr TimerHandle()
Definition timer.hpp:160
friend constexpr bool operator!=(const TimerHandle &a, const TimerHandle &b)
Definition timer.hpp:193
friend constexpr bool operator==(const TimerHandle &a, const TimerHandle &b)
Definition timer.hpp:190
constexpr size_t handle_id() const
The packed executor handle this refers to. For introspection and tests.
Definition timer.hpp:188
TimerOps * operator->()
timer_->cancel() and friends.
Definition timer.hpp:172
void reset()
Definition timer.hpp:179
friend constexpr bool operator!=(const TimerHandle &a, decltype(nullptr))
Definition timer.hpp:199
const TimerOps * operator->() const
Const overload of operator->.
Definition timer.hpp:174
Duration time_until_trigger() const
Definition timer.hpp:106
constexpr TimerOps(void *executor, size_t handle_id)
Definition timer.hpp:121
Result cancel()
Definition timer.hpp:76
bool is_ready() const
Definition timer.hpp:98
bool is_canceled() const
Definition timer.hpp:92
void * executor_
Definition timer.hpp:124
Result reset()
Restart the timer from zero elapsed time; un-cancels a cancelled one.
Definition timer.hpp:85
size_t handle_id_
Definition timer.hpp:125
detail::ClockValuePtr get_clock() const
Definition timer.hpp:118
Definition rclcpp.hpp:418
ClockValuePtr timer_clock(void *executor, size_t handle_id)
Definition timer.hpp:52
InplaceFn< void(const TimerInfo &)> TimerInfoCallbackType
Definition timer.hpp:566
::rclcpp::Timer TimerBase
Definition timer.hpp:535
ResultOf< void > Result
Definition result.hpp:247
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.
Time expected_call_time
Definition timer.hpp:552
Time actual_call_time
Definition timer.hpp:553