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  // This constructor matches any numeric value - ints or floats
32  explicit Duration(rcl_duration_value_t nanoseconds);
33 
34  explicit Duration(std::chrono::nanoseconds nanoseconds);
35 
36  // This constructor matches any std::chrono value other than nanoseconds
37  // intentionally not using explicit to create a conversion constructor
38  template<class Rep, class Period>
39  // cppcheck-suppress noExplicitConstructor
40  Duration(const std::chrono::duration<Rep, Period> & duration) // NOLINT(runtime/explicit)
41  : Duration(std::chrono::duration_cast<std::chrono::nanoseconds>(duration))
42  {}
43 
44  // cppcheck-suppress noExplicitConstructor
45  Duration(const builtin_interfaces::msg::Duration & duration_msg); // NOLINT(runtime/explicit)
46 
47  explicit Duration(const rcl_duration_t & duration);
48 
49  Duration(const Duration & rhs);
50 
51  virtual ~Duration() = default;
52 
53  operator builtin_interfaces::msg::Duration() const;
54 
55  // cppcheck-suppress operatorEq // this is a false positive from cppcheck
56  Duration &
57  operator=(const Duration & rhs);
58 
59  Duration &
60  operator=(const builtin_interfaces::msg::Duration & Duration_msg);
61 
62  bool
63  operator==(const rclcpp::Duration & rhs) const;
64 
65  bool
66  operator<(const rclcpp::Duration & rhs) const;
67 
68  bool
69  operator<=(const rclcpp::Duration & rhs) const;
70 
71  bool
72  operator>=(const rclcpp::Duration & rhs) const;
73 
74  bool
75  operator>(const rclcpp::Duration & rhs) const;
76 
77  Duration
78  operator+(const rclcpp::Duration & rhs) const;
79 
80  Duration
81  operator-(const rclcpp::Duration & rhs) const;
82 
83  static
84  Duration
85  max();
86 
87  Duration
88  operator*(double scale) const;
89 
91  nanoseconds() const;
92 
96  double
97  seconds() const;
98 
99  // Create a duration object from a floating point number representing seconds
100  static Duration
101  from_seconds(double seconds);
102 
103  template<class DurationT>
104  DurationT
105  to_chrono() const
106  {
107  return std::chrono::duration_cast<DurationT>(std::chrono::nanoseconds(this->nanoseconds()));
108  }
109 
110  rmw_time_t
111  to_rmw_time() const;
112 
113 private:
114  rcl_duration_t rcl_duration_;
115 };
116 
117 } // namespace rclcpp
118 
119 #endif // RCLCPP__DURATION_HPP_
DurationT to_chrono() const
Definition: duration.hpp:105
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
T duration_cast(T... args)
Duration(const std::chrono::duration< Rep, Period > &duration)
Definition: duration.hpp:40
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)