rclcpp  master
C++ ROS Client Library API
wait_result.hpp
Go to the documentation of this file.
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"
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  {
76  }
77 
79  static
82  {
84  }
85 
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 
131  {
132  if (wait_set_pointer_) {
133  wait_set_pointer_->wait_result_release();
134  }
135  }
136 
137 private:
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_
rclcpp::Ready
@ Ready
Definition: wait_result_kind.hpp:26
rclcpp::WaitResultKind
WaitResultKind
Represents the various kinds of results from waiting on a wait set.
Definition: wait_result_kind.hpp:24
RCLCPP_DISABLE_COPY
#define RCLCPP_DISABLE_COPY(...)
Definition: macros.hpp:26
wait.h
rclcpp::WaitResult::from_ready_wait_result_kind
static WaitResult from_ready_wait_result_kind(WaitSetT &wait_set)
Create WaitResult from a "ready" result.
Definition: wait_result.hpp:65
rclcpp::WaitResult::WaitResult
WaitResult(WaitResult &&other) noexcept
Definition: wait_result.hpp:125
rclcpp::WaitResult::from_timeout_wait_result_kind
static WaitResult from_timeout_wait_result_kind()
Create WaitResult from a "timeout" result.
Definition: wait_result.hpp:73
rclcpp
This header provides the get_node_base_interface() template function.
Definition: allocator_common.hpp:24
rclcpp::WaitResult::from_empty_wait_result_kind
static WaitResult from_empty_wait_result_kind()
Create WaitResult from a "empty" result.
Definition: wait_result.hpp:81
rclcpp::WaitResult::get_wait_set
WaitSetT & get_wait_set()
Return the rcl wait set.
Definition: wait_result.hpp:115
macros.hpp
rclcpp::Timeout
@ Timeout
Definition: wait_result_kind.hpp:27
rclcpp::WaitResult::~WaitResult
~WaitResult()
Definition: wait_result.hpp:130
std::runtime_error
rclcpp::WaitResult::get_wait_set
const WaitSetT & get_wait_set() const
Return the rcl wait set.
Definition: wait_result.hpp:99
wait_result_kind.hpp
rclcpp::WaitResult::kind
WaitResultKind kind() const
Return the kind of the WaitResult.
Definition: wait_result.hpp:88
rclcpp::Empty
@ Empty
Definition: wait_result_kind.hpp:28
rclcpp::WaitResult
Interface for introspecting a wait set after waiting on it.
Definition: wait_result.hpp:54