ROS 2 rclcpp + rcl - humble  humble
ROS 2 C++ Client Library with ROS Client Library
wait_result.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_RESULT_HPP_
16 #define RCLCPP__WAIT_RESULT_HPP_
17 
18 #include <cassert>
19 #include <functional>
20 #include <stdexcept>
21 
22 #include "rcl/wait.h"
23 
24 #include "rclcpp/macros.hpp"
25 #include "rclcpp/wait_result_kind.hpp"
26 
27 namespace rclcpp
28 {
29 
30 // TODO(wjwwood): the union-like design of this class could be replaced with
31 // std::variant, when we have access to that...
33 
53 template<class WaitSetT>
54 class WaitResult final
55 {
56 public:
58 
63  static
65  from_ready_wait_result_kind(WaitSetT & wait_set)
66  {
67  return WaitResult(WaitResultKind::Ready, wait_set);
68  }
69 
71  static
74  {
75  return WaitResult(WaitResultKind::Timeout);
76  }
77 
79  static
82  {
83  return WaitResult(WaitResultKind::Empty);
84  }
85 
87  WaitResultKind
88  kind() const
89  {
90  return wait_result_kind_;
91  }
92 
94 
98  const WaitSetT &
99  get_wait_set() const
100  {
101  if (this->kind() != WaitResultKind::Ready) {
102  throw std::runtime_error("cannot access wait set when the result was not ready");
103  }
104  // This should never happen, defensive (and debug mode) check only.
105  assert(wait_set_pointer_);
106  return *wait_set_pointer_;
107  }
108 
110 
114  WaitSetT &
116  {
117  if (this->kind() != WaitResultKind::Ready) {
118  throw std::runtime_error("cannot access wait set when the result was not ready");
119  }
120  // This should never happen, defensive (and debug mode) check only.
121  assert(wait_set_pointer_);
122  return *wait_set_pointer_;
123  }
124 
125  WaitResult(WaitResult && other) noexcept
126  : wait_result_kind_(other.wait_result_kind_),
127  wait_set_pointer_(std::exchange(other.wait_set_pointer_, nullptr))
128  {}
129 
130  ~WaitResult()
131  {
132  if (wait_set_pointer_) {
133  wait_set_pointer_->wait_result_release();
134  }
135  }
136 
137 private:
138  RCLCPP_DISABLE_COPY(WaitResult)
139 
140  explicit WaitResult(WaitResultKind wait_result_kind)
141  : wait_result_kind_(wait_result_kind)
142  {
143  // Should be enforced by the static factory methods on this class.
144  assert(WaitResultKind::Ready != wait_result_kind);
145  }
146 
147  WaitResult(WaitResultKind wait_result_kind, WaitSetT & wait_set)
148  : wait_result_kind_(wait_result_kind),
149  wait_set_pointer_(&wait_set)
150  {
151  // Should be enforced by the static factory methods on this class.
152  assert(WaitResultKind::Ready == wait_result_kind);
153  // Secure thread-safety (if provided) and shared ownership (if needed).
154  wait_set_pointer_->wait_result_acquire();
155  }
156 
157  const WaitResultKind wait_result_kind_;
158 
159  WaitSetT * wait_set_pointer_ = nullptr;
160 };
161 
162 } // namespace rclcpp
163 
164 #endif // RCLCPP__WAIT_RESULT_HPP_
Interface for introspecting a wait set after waiting on it.
Definition: wait_result.hpp:55
static WaitResult from_ready_wait_result_kind(WaitSetT &wait_set)
Create WaitResult from a "ready" result.
Definition: wait_result.hpp:65
static WaitResult from_empty_wait_result_kind()
Create WaitResult from a "empty" result.
Definition: wait_result.hpp:81
static WaitResult from_timeout_wait_result_kind()
Create WaitResult from a "timeout" result.
Definition: wait_result.hpp:73
const WaitSetT & get_wait_set() const
Return the rcl wait set.
Definition: wait_result.hpp:99
WaitResultKind kind() const
Return the kind of the WaitResult.
Definition: wait_result.hpp:88
WaitSetT & get_wait_set()
Return the rcl wait set.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.