.. _program_listing_file_include_rclcpp_wait_result.hpp: Program Listing for File wait_result.hpp ======================================== |exhale_lsh| :ref:`Return to documentation for file ` (``include/rclcpp/wait_result.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp // Copyright 2020 Open Source Robotics Foundation, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #ifndef RCLCPP__WAIT_RESULT_HPP_ #define RCLCPP__WAIT_RESULT_HPP_ #include #include #include #include "rcl/wait.h" #include "rclcpp/macros.hpp" #include "rclcpp/wait_result_kind.hpp" namespace rclcpp { // TODO(wjwwood): the union-like design of this class could be replaced with // std::variant, when we have access to that... template class WaitResult final { public: static WaitResult from_ready_wait_result_kind(WaitSetT & wait_set) { return WaitResult(WaitResultKind::Ready, wait_set); } static WaitResult from_timeout_wait_result_kind() { return WaitResult(WaitResultKind::Timeout); } static WaitResult from_empty_wait_result_kind() { return WaitResult(WaitResultKind::Empty); } WaitResultKind kind() const { return wait_result_kind_; } const WaitSetT & get_wait_set() const { if (this->kind() != WaitResultKind::Ready) { throw std::runtime_error("cannot access wait set when the result was not ready"); } // This should never happen, defensive (and debug mode) check only. assert(wait_set_pointer_); return *wait_set_pointer_; } WaitSetT & get_wait_set() { if (this->kind() != WaitResultKind::Ready) { throw std::runtime_error("cannot access wait set when the result was not ready"); } // This should never happen, defensive (and debug mode) check only. assert(wait_set_pointer_); return *wait_set_pointer_; } WaitResult(WaitResult && other) noexcept : wait_result_kind_(other.wait_result_kind_), wait_set_pointer_(std::exchange(other.wait_set_pointer_, nullptr)) {} ~WaitResult() { if (wait_set_pointer_) { wait_set_pointer_->wait_result_release(); } } private: RCLCPP_DISABLE_COPY(WaitResult) explicit WaitResult(WaitResultKind wait_result_kind) : wait_result_kind_(wait_result_kind) { // Should be enforced by the static factory methods on this class. assert(WaitResultKind::Ready != wait_result_kind); } WaitResult(WaitResultKind wait_result_kind, WaitSetT & wait_set) : wait_result_kind_(wait_result_kind), wait_set_pointer_(&wait_set) { // Should be enforced by the static factory methods on this class. assert(WaitResultKind::Ready == wait_result_kind); // Secure thread-safety (if provided) and shared ownership (if needed). wait_set_pointer_->wait_result_acquire(); } const WaitResultKind wait_result_kind_; WaitSetT * wait_set_pointer_ = nullptr; }; } // namespace rclcpp #endif // RCLCPP__WAIT_RESULT_HPP_