rclcpp  master
C++ ROS Client Library API
duration.hpp
Go to the documentation of this file.
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 "rcl/time.h"
23 
24 namespace rclcpp
25 {
27 {
28 public:
30 
39  Duration(int32_t seconds, uint32_t nanoseconds);
40 
42  [[deprecated(
43  "Use Duration::from_nanoseconds instead or std::chrono_literals. For example:"
44  "rclcpp::Duration::from_nanoseconds(int64_variable);"
45  "rclcpp::Duration(0ns);")]]
47 
50 
51  // This constructor matches any std::chrono value other than nanoseconds
52  // intentionally not using explicit to create a conversion constructor
53  template<class Rep, class Period>
54  // cppcheck-suppress noExplicitConstructor
55  Duration(const std::chrono::duration<Rep, Period> & duration) // NOLINT(runtime/explicit)
56  : Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(duration))
57  {}
58 
59  // cppcheck-suppress noExplicitConstructor
60  Duration(const builtin_interfaces::msg::Duration & duration_msg); // NOLINT(runtime/explicit)
61 
63 
66  explicit Duration(const rcl_duration_t & duration);
67 
68  Duration(const Duration & rhs);
69 
70  virtual ~Duration() = default;
71 
72  operator builtin_interfaces::msg::Duration() const;
73 
74  // cppcheck-suppress operatorEq // this is a false positive from cppcheck
75  Duration &
76  operator=(const Duration & rhs);
77 
78  Duration &
79  operator=(const builtin_interfaces::msg::Duration & duration_msg);
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  bool
94  operator>=(const rclcpp::Duration & rhs) const;
95 
96  bool
97  operator>(const rclcpp::Duration & rhs) const;
98 
99  Duration
100  operator+(const rclcpp::Duration & rhs) const;
101 
102  Duration
103  operator-(const rclcpp::Duration & rhs) const;
104 
106 
109  static
110  Duration
111  max();
112 
113  Duration
114  operator*(double scale) const;
115 
117 
121  nanoseconds() const;
122 
124 
129  double
130  seconds() const;
131 
133  static Duration
134  from_seconds(double seconds);
135 
137  static Duration
138  from_nanoseconds(rcl_duration_value_t nanoseconds);
139 
140  static Duration
141  from_rmw_time(rmw_time_t duration);
142 
144  template<class DurationT>
145  DurationT
146  to_chrono() const
147  {
148  return std::chrono::duration_cast<DurationT>(std::chrono::nanoseconds(this->nanoseconds()));
149  }
150 
152  rmw_time_t
153  to_rmw_time() const;
154 
155 private:
156  rcl_duration_t rcl_duration_;
157 
158  Duration() = default;
159 };
160 
161 } // namespace rclcpp
162 
163 #endif // RCLCPP__DURATION_HPP_
rmw_time_t
rclcpp::Duration::to_chrono
DurationT to_chrono() const
Convert Duration into a std::chrono::Duration.
Definition: duration.hpp:146
time.h
std::chrono::nanoseconds
rclcpp::operator+
Time operator+(const rclcpp::Duration &lhs, const rclcpp::Time &rhs)
rclcpp::Duration::Duration
Duration(const std::chrono::duration< Rep, Period > &duration)
Definition: duration.hpp:55
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
RCLCPP_PUBLIC
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
rclcpp::operator==
bool operator==(const NetworkFlowEndpoint &left, const NetworkFlowEndpoint &right)
Check if two NetworkFlowEndpoint instances are equal.
rclcpp::Duration
Definition: duration.hpp:26
visibility_control.hpp
std
rcl_duration_value_t
rcutils_duration_value_t rcl_duration_value_t
rclcpp::operator!=
bool operator!=(const NetworkFlowEndpoint &left, const NetworkFlowEndpoint &right)
Check if two NetworkFlowEndpoint instances are not equal.
rcl_duration_t