ROS 2 rclcpp + rcl - rolling  rolling-a919a6e5
ROS 2 C++ Client Library with ROS Client Library
synchronization_policy_common.hpp
1 // Copyright 2020 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__WAIT_SET_POLICIES__DETAIL__SYNCHRONIZATION_POLICY_COMMON_HPP_
16 #define RCLCPP__WAIT_SET_POLICIES__DETAIL__SYNCHRONIZATION_POLICY_COMMON_HPP_
17 
18 #include <chrono>
19 #include <functional>
20 
21 namespace rclcpp
22 {
23 namespace wait_set_policies
24 {
25 namespace detail
26 {
27 
30 {
31 protected:
32  SynchronizationPolicyCommon() = default;
33  ~SynchronizationPolicyCommon() = default;
34 
35  std::function<bool()>
36  create_loop_predicate(
37  std::chrono::nanoseconds time_to_wait_ns,
38  std::chrono::steady_clock::time_point start)
39  {
40  if (time_to_wait_ns >= std::chrono::nanoseconds(0)) {
41  // If time_to_wait_ns is >= 0 schedule against a deadline.
42  auto deadline = start + time_to_wait_ns;
43  return [deadline]() -> bool {return std::chrono::steady_clock::now() < deadline;};
44  } else {
45  // In the case of time_to_wait_ns < 0, just always return true to loop forever.
46  return []() -> bool {return true;};
47  }
48  }
49 
50  std::chrono::nanoseconds
51  calculate_time_left_to_wait(
52  std::chrono::nanoseconds original_time_to_wait_ns,
53  std::chrono::steady_clock::time_point start)
54  {
55  std::chrono::nanoseconds time_left_to_wait;
56  if (original_time_to_wait_ns < std::chrono::nanoseconds(0)) {
57  time_left_to_wait = original_time_to_wait_ns;
58  } else {
59  time_left_to_wait = original_time_to_wait_ns - (std::chrono::steady_clock::now() - start);
60  if (time_left_to_wait < std::chrono::nanoseconds(0)) {
61  time_left_to_wait = std::chrono::nanoseconds(0);
62  }
63  }
64  return time_left_to_wait;
65  }
66 };
67 
68 } // namespace detail
69 } // namespace wait_set_policies
70 } // namespace rclcpp
71 
72 #endif // RCLCPP__WAIT_SET_POLICIES__DETAIL__SYNCHRONIZATION_POLICY_COMMON_HPP_
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.