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:
29  Duration(int32_t seconds, uint32_t nanoseconds);
30 
31  explicit Duration(rcl_duration_value_t nanoseconds);
32 
33  explicit Duration(std::chrono::nanoseconds nanoseconds);
34 
35  // intentionally not using explicit to create a conversion constructor
36  template<class Rep, class Period>
37  // cppcheck-suppress noExplicitConstructor
38  Duration(const std::chrono::duration<Rep, Period> & duration) // NOLINT(runtime/explicit)
39  : Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(duration))
40  {}
41 
42  // cppcheck-suppress noExplicitConstructor
43  Duration(const builtin_interfaces::msg::Duration & duration_msg); // NOLINT(runtime/explicit)
44 
45  explicit Duration(const rcl_duration_t & duration);
46 
47  Duration(const Duration & rhs);
48 
49  virtual ~Duration() = default;
50 
51  operator builtin_interfaces::msg::Duration() const;
52 
53  // cppcheck-suppress operatorEq // this is a false positive from cppcheck
54  Duration &
55  operator=(const Duration & rhs);
56 
57  Duration &
58  operator=(const builtin_interfaces::msg::Duration & Duration_msg);
59 
60  bool
61  operator==(const rclcpp::Duration & rhs) const;
62 
63  bool
64  operator<(const rclcpp::Duration & rhs) const;
65 
66  bool
67  operator<=(const rclcpp::Duration & rhs) const;
68 
69  bool
70  operator>=(const rclcpp::Duration & rhs) const;
71 
72  bool
73  operator>(const rclcpp::Duration & rhs) const;
74 
75  Duration
76  operator+(const rclcpp::Duration & rhs) const;
77 
78  Duration
79  operator-(const rclcpp::Duration & rhs) const;
80 
81  static
82  Duration
83  max();
84 
85  Duration
86  operator*(double scale) const;
87 
89  nanoseconds() const;
90 
94  double
95  seconds() const;
96 
97  template<class DurationT>
98  DurationT
99  to_chrono() const
100  {
101  return std::chrono::duration_cast<DurationT>(std::chrono::nanoseconds(this->nanoseconds()));
102  }
103 
104  rmw_time_t
105  to_rmw_time() const;
106 
107 private:
108  rcl_duration_t rcl_duration_;
109 };
110 
111 } // namespace rclcpp
112 
113 #endif // RCLCPP__DURATION_HPP_
DurationT to_chrono() const
Definition: duration.hpp:99
This header provides the get_node_topics_interface() template function.
Definition: allocator_common.hpp:24
T duration_cast(T... args)
Duration(const std::chrono::duration< Rep, Period > &duration)
Definition: duration.hpp:38
Definition: duration.hpp:26
rcutils_duration_value_t rcl_duration_value_t
#define RCLCPP_PUBLIC
Definition: visibility_control.hpp:50
Time operator+(const rclcpp::Duration &lhs, const rclcpp::Time &rhs)