ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
duration.hpp
1 // Copyright 2017 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef RCLCPP__DURATION_HPP_
16 #define RCLCPP__DURATION_HPP_
17 
18 #include <chrono>
19 
20 #include "builtin_interfaces/msg/duration.hpp"
21 #include "builtin_interfaces/msg/time.hpp"
22 #include "rcl/time.h"
23 #include "rclcpp/visibility_control.hpp"
24 
25 namespace rclcpp
26 {
27 class RCLCPP_PUBLIC Duration
28 {
29 public:
31 
40  Duration(int32_t seconds, uint32_t nanoseconds);
41 
43  explicit Duration(std::chrono::nanoseconds nanoseconds);
44 
45  // This constructor matches any std::chrono value other than nanoseconds
46  // intentionally not using explicit to create a conversion constructor
47  template<class Rep, class Period>
48  // cppcheck-suppress noExplicitConstructor
49  Duration(const std::chrono::duration<Rep, Period> & duration) // NOLINT(runtime/explicit)
50  : Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(duration))
51  {}
52 
53  // cppcheck-suppress noExplicitConstructor
54  Duration(const builtin_interfaces::msg::Duration & duration_msg); // NOLINT(runtime/explicit)
55 
57 
60  explicit Duration(const rcl_duration_t & duration);
61 
62  Duration(const Duration & rhs);
63 
64  virtual ~Duration() = default;
65 
66  operator builtin_interfaces::msg::Duration() const;
67 
68  // cppcheck-suppress operatorEq // this is a false positive from cppcheck
69  Duration &
70  operator=(const Duration & rhs);
71 
72  Duration &
73  operator=(const builtin_interfaces::msg::Duration & duration_msg);
74 
75  bool
76  operator==(const rclcpp::Duration & rhs) const;
77 
78  bool
79  operator!=(const rclcpp::Duration & rhs) const;
80 
81  bool
82  operator<(const rclcpp::Duration & rhs) const;
83 
84  bool
85  operator<=(const rclcpp::Duration & rhs) const;
86 
87  bool
88  operator>=(const rclcpp::Duration & rhs) const;
89 
90  bool
91  operator>(const rclcpp::Duration & rhs) const;
92 
93  Duration
94  operator+(const rclcpp::Duration & rhs) const;
95 
96  Duration & operator+=(const rclcpp::Duration & rhs);
97 
98  Duration
99  operator-(const rclcpp::Duration & rhs) const;
100 
101  Duration & operator-=(const rclcpp::Duration & rhs);
102 
104 
107  static
108  Duration
109  max();
110 
111  Duration
112  operator*(double scale) const;
113 
114  Duration &
115  operator*=(double scale);
116 
118 
122  nanoseconds() const;
123 
125 
130  double
131  seconds() const;
132 
134  static Duration
135  from_seconds(double seconds);
136 
138  static Duration
139  from_nanoseconds(rcl_duration_value_t nanoseconds);
140 
141  static Duration
142  from_rmw_time(rmw_time_t duration);
143 
145  template<class DurationT>
146  DurationT
147  to_chrono() const
148  {
149  return std::chrono::duration_cast<DurationT>(std::chrono::nanoseconds(this->nanoseconds()));
150  }
151 
153  rmw_time_t
154  to_rmw_time() const;
155 
156 private:
157  rcl_duration_t rcl_duration_;
158 
159  Duration() = default;
160 };
161 
162 RCLCPP_PUBLIC
163 builtin_interfaces::msg::Time
164 operator+(const builtin_interfaces::msg::Time & lhs, const rclcpp::Duration & rhs);
165 
166 RCLCPP_PUBLIC
167 builtin_interfaces::msg::Time
168 operator-(const builtin_interfaces::msg::Time & lhs, const rclcpp::Duration & rhs);
169 
170 } // namespace rclcpp
171 
172 #endif // RCLCPP__DURATION_HPP_
DurationT to_chrono() const
Convert Duration into a std::chrono::Duration.
Definition: duration.hpp:147
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC bool operator==(const NetworkFlowEndpoint &left, const NetworkFlowEndpoint &right)
Check if two NetworkFlowEndpoint instances are equal.
RCLCPP_PUBLIC bool operator!=(const NetworkFlowEndpoint &left, const NetworkFlowEndpoint &right)
Check if two NetworkFlowEndpoint instances are not equal.
A duration of time, measured in nanoseconds and its source.
Definition: time.h:75
rcutils_duration_value_t rcl_duration_value_t
A duration of time, measured in nanoseconds.
Definition: time.h:48