mrs_lib
Various reusable classes, functions and utilities for use in MRS projects
Loading...
Searching...
No Matches
timeout_manager.hpp
1#ifndef MRS_LIB_TIMEOUT_MANAGER_HPP_
2#define MRS_LIB_TIMEOUT_MANAGER_HPP_
3
4
5#include <chrono>
6#include <functional>
7#include <memory>
8#include <mutex>
9
10#include <rclcpp/node_interfaces/node_base_interface.hpp>
11#include <rclcpp/node_interfaces/node_clock_interface.hpp>
12#include <rclcpp/node_interfaces/node_interfaces.hpp>
13#include <rclcpp/node_interfaces/node_timers_interface.hpp>
14#include <rclcpp/time.hpp>
15
18
19
20namespace mrs_lib
21{
23
24 namespace internal
25 {
27 }
28
29 using TimeoutManagerNodeInterfaces =
30 rclcpp::node_interfaces::NodeInterfaces<rclcpp::node_interfaces::NodeBaseInterface, rclcpp::node_interfaces::NodeTimersInterface,
31 rclcpp::node_interfaces::NodeClockInterface>;
32
33
38 {
40 std::chrono::nanoseconds timeout;
42 std::function<void(rclcpp::Time)> callback;
44 bool started = true;
46 bool oneshot = false;
47 };
48
54 {
55 private:
56 class Impl;
57
58 public:
59 using Callback = std::function<void(rclcpp::Time)>;
60
67 explicit TimeoutManager2(TimeoutManagerNodeInterfaces node_interfaces, std::chrono::nanoseconds update_period);
68
71
76
80 TimeoutManager2& operator=(TimeoutManager2&&) noexcept;
81
88
92 void start_all();
93
97 void pause_all();
98
102 void clear_deleted();
103
104 private:
105 Pimpl<Impl> impl_;
106
107 friend class internal::TimeoutManagerHandleKey;
108 };
109
110 namespace internal
111 {
112
113 class TimeoutManagerHandleKey
114 {
115 private:
116 TimeoutManagerHandleKey() = default;
117
118 friend class TimeoutManager2::Impl;
119 };
120
121 } // namespace internal
122
127 {
128 public:
139 std::shared_ptr<rclcpp::node_interfaces::NodeClockInterface> clock_interface);
140
151
158 void reset_timeout();
159
163 void start();
164
171 void pause();
172
176 void set_timeout(std::chrono::nanoseconds timeout);
177
183 [[nodiscard]] bool is_started() const;
184
190 [[nodiscard]] rclcpp::Time get_last_reset() const;
191
200
201 private:
202 struct Data
203 {
204 std::shared_ptr<rclcpp::node_interfaces::NodeClockInterface> clock_interface;
206 rclcpp::Time last_reset;
207 rclcpp::Time last_callback;
208 };
209
211 };
212
213} // namespace mrs_lib
214
215#endif // MRS_LIB_TIMEOUT_MANAGER_HPP_
A thread-safe wrapper for shared data with mutex protection.
Definition owning_mutex.hpp:26
Owning pointer wrapper with move only value semantics. Usefull for implementing the pointer to implem...
Definition pimpl.hpp:39
TimeoutManager2(TimeoutManagerNodeInterfaces node_interfaces, std::chrono::nanoseconds update_period)
Construct the timeout manager.
Definition timeout_manager2.cpp:113
~TimeoutManager2()
Destructor.
void pause_all()
Stop all timeouts managed by this manager.
Definition timeout_manager2.cpp:129
TimeoutManager2(const TimeoutManager2 &)=delete
TimeoutManager2 is not copyable.
TimeoutManager2 & operator=(const TimeoutManager2 &)=delete
TimeoutManager2 is not copyable.
std::shared_ptr< TimeoutManagerHandle > register_new(TimeoutManagerRegisterOptions options)
Register a new timeout to be handled by this manager.
Definition timeout_manager2.cpp:124
void start_all()
Start all timeouts managed by this manager.
Definition timeout_manager2.cpp:134
TimeoutManager2(TimeoutManager2 &&) noexcept
Move constructor.
void clear_deleted()
Remove all timeouts for which the handles were already deleted.
Definition timeout_manager2.cpp:139
Handle to a registered timeout, that unregisters it on destruction.
Definition timeout_manager.hpp:127
void start()
Start the timeout.
Definition timeout_manager2.cpp:168
TimeoutManagerHandle(TimeoutManagerHandle &&)=delete
TimeoutManagerHandle is immovable.
rclcpp::Time get_last_reset() const
Get time of last call to reset.
Definition timeout_manager2.cpp:193
void pause()
Stop the timeout.
Definition timeout_manager2.cpp:175
TimeoutManagerHandle & operator=(const TimeoutManagerHandle &)=delete
TimeoutManagerHandle is immovable.
~TimeoutManagerHandle()=default
Default destructor.
TimeoutManagerHandle(const TimeoutManagerHandle &)=delete
TimeoutManagerHandle is immovable.
bool is_started() const
Check if the timeout is started.
Definition timeout_manager2.cpp:187
void run_callback_if_expired(internal::TimeoutManagerHandleKey key)
Check if timeout is expired and if yes, call it.
Definition timeout_manager2.cpp:199
TimeoutManagerHandle & operator=(TimeoutManagerHandle &&)=delete
TimeoutManagerHandle is immovable.
void set_timeout(std::chrono::nanoseconds timeout)
Set a new duration after which the timeout is called.
Definition timeout_manager2.cpp:181
void reset_timeout()
Reset the timeout countdown.
Definition timeout_manager2.cpp:162
TimeoutManagerHandle(internal::TimeoutManagerHandleKey key, TimeoutManagerRegisterOptions options, std::shared_ptr< rclcpp::node_interfaces::NodeClockInterface > clock_interface)
Construct the handle.
Definition timeout_manager2.cpp:144
Definition timeout_manager.hpp:114
All mrs_lib functions, classes, variables and definitions are contained in this namespace.
Definition attitude_converter.h:24
Options for registering a timeout.
Definition timeout_manager.hpp:38
bool oneshot
Whether the timeout should stop after triggering once.
Definition timeout_manager.hpp:46
std::chrono::nanoseconds timeout
Time of inactivity after which the callback is called.
Definition timeout_manager.hpp:40
bool started
Whether the timeout should be created in the started state.
Definition timeout_manager.hpp:44
std::function< void(rclcpp::Time)> callback
Callback called when timeout is triggered.
Definition timeout_manager.hpp:42