nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
timer.hpp
Go to the documentation of this file.
1// nros-cpp: Timer class
2// Freestanding C++ — no exceptions, no STL required
3
10#ifndef NROS_CPP_TIMER_HPP
11#define NROS_CPP_TIMER_HPP
12
13#include <cstdint>
14#include <cstddef>
15
16#include "nros/result.hpp"
17
18#ifdef NROS_CPP_STD
19#include <functional>
20#include <memory>
21#endif
22
23#include "nros_cpp_ffi.h"
24
25namespace nros {
26
42class Timer {
43 public:
47 if (!initialized_) return Result(ErrorCode::NotInitialized);
48 return Result(nros_cpp_timer_cancel(executor_, handle_id_));
49 }
50
54 if (!initialized_) return Result(ErrorCode::NotInitialized);
55 return Result(nros_cpp_timer_reset(executor_, handle_id_));
56 }
57
59 bool is_cancelled() const {
60 if (!initialized_) return true;
61 return nros_cpp_timer_is_cancelled(executor_, handle_id_);
62 }
63
65 bool is_valid() const { return initialized_; }
66
69 if (initialized_) {
70 nros_cpp_timer_cancel(executor_, handle_id_);
71 initialized_ = false;
72 }
73 // closure_ (if any) destructs here; the runtime no longer
74 // holds a raw pointer to it because we cancelled above.
75 }
76
77 // Move semantics (non-copyable)
79 : executor_(other.executor_), handle_id_(other.handle_id_), initialized_(other.initialized_)
81 ,
83#endif
84 {
85 other.executor_ = nullptr;
86 other.initialized_ = false;
87 }
88
90 if (this != &other) {
91 if (initialized_) {
92 nros_cpp_timer_cancel(executor_, handle_id_);
93 }
94 executor_ = other.executor_;
95 handle_id_ = other.handle_id_;
96 initialized_ = other.initialized_;
97#ifdef NROS_CPP_STD
98 closure_ = std::move(other.closure_);
99#endif
100 other.executor_ = nullptr;
101 other.initialized_ = false;
102 }
103 return *this;
104 }
105
108 Timer() : executor_(nullptr), handle_id_(0), initialized_(false) {}
109
110#ifdef NROS_CPP_STD
117 void attach_std_closure(std::unique_ptr<std::function<void()>> closure) {
118 closure_ = std::move(closure);
119 }
120#endif
121
122 private:
123 Timer(const Timer&) = delete;
124 Timer& operator=(const Timer&) = delete;
125
126 friend class Node;
127
128 void* executor_;
129 size_t handle_id_;
130 bool initialized_;
131
132#ifdef NROS_CPP_STD
138 std::unique_ptr<std::function<void()>> closure_;
139#endif
140};
141
142} // namespace nros
143
144#endif // NROS_CPP_TIMER_HPP
Definition future.hpp:40
Definition node.hpp:158
Definition result.hpp:52
Definition timer.hpp:42
Timer(Timer &&other)
Definition timer.hpp:78
bool is_cancelled() const
Check if the timer is cancelled.
Definition timer.hpp:59
~Timer()
Destructor — cancels the timer.
Definition timer.hpp:68
Timer()
Definition timer.hpp:108
Result cancel()
Definition timer.hpp:46
Result reset()
Definition timer.hpp:53
bool is_valid() const
Check if the timer is initialized and valid.
Definition timer.hpp:65
Timer & operator=(Timer &&other)
Definition timer.hpp:89
Definition nros.hpp:42
nros::Result, nros::ErrorCode, and the NROS_TRY macro.