nros C++ API
Lightweight ROS 2 client for embedded real-time systems (C++ headers)
Loading...
Searching...
No Matches
guard_condition.hpp
Go to the documentation of this file.
1// nros-cpp: Guard condition class
2// Freestanding C++ — no exceptions, no STL required
3
10#ifndef NROS_RCLCPP_GUARD_CONDITION_HPP
11#define NROS_RCLCPP_GUARD_CONDITION_HPP
12
13#include <cstdint>
14#include <cstddef>
15
17#include "rclcpp/result.hpp"
18#include "rclcpp/detail/hosted_block.hpp"
19
20#include "rclcpp/detail/nros_cpp_ffi.h"
21
22// phase-427 W7 — `Node` is DEFINED in `rclcpp::` (RFC-0089: that namespace is
23// the home). The friend declaration below is qualified, and a qualified friend
24// names an existing entity rather than introducing one, so the name has to be
25// declared first — and in `rclcpp::`, because an elaborated `class Node;` in
26// `rclcpp::` would declare a second, distinct class.
27namespace rclcpp {
28class Node;
29}
30
31namespace rclcpp {
32
51 public:
56 if (!initialized_) return Result(ErrorCode::NotInitialized);
58 }
59
61 bool is_valid() const { return initialized_; }
62
73 bool is_triggered() const {
74 if (!initialized_) return false;
76 }
77
81 if (!initialized_) return Result(ErrorCode::NotInitialized);
82 return Result(nros_cpp_guard_condition_clear(storage_));
83 }
84
91 release();
92 // The closure block (if any) is freed here, after the entry that could
93 // call into it.
94 detail::destroy_hosted_block(closure_);
95 }
96
97 // Move semantics (non-copyable). Relocation goes through the
98 // `nros_cpp_guard_condition_relocate` runtime call (Phase 84.C1).
100 : initialized_(other.initialized_), closure_(other.closure_), executor_(other.executor_),
101 handle_id_(other.handle_id_) {
102 if (other.initialized_) {
103 nros_cpp_guard_condition_relocate(other.storage_, storage_);
104 other.initialized_ = false;
105 }
106 other.closure_ = nullptr;
107 other.executor_ = nullptr;
108 other.handle_id_ = 0;
109 }
110
112 if (this != &other) {
113 release();
114 if (other.initialized_) {
115 nros_cpp_guard_condition_relocate(other.storage_, storage_);
116 initialized_ = true;
117 other.initialized_ = false;
118 }
119 executor_ = other.executor_;
120 handle_id_ = other.handle_id_;
121 other.executor_ = nullptr;
122 other.handle_id_ = 0;
123 detail::destroy_hosted_block(closure_);
124 closure_ = other.closure_;
125 other.closure_ = nullptr;
126 }
127 return *this;
128 }
129
133 : storage_(), initialized_(false), closure_(nullptr), executor_(nullptr), handle_id_(0) {}
134
138 void attach_closure_block(detail::HostedBlockBase* block) {
139 detail::destroy_hosted_block(closure_);
140 closure_ = block;
141 }
142
143 private:
145 void release() {
146 if (initialized_) {
147 if (executor_ != nullptr) {
148 (void)nros_cpp_guard_condition_release(executor_, handle_id_);
149 }
150 nros_cpp_guard_condition_destroy(storage_);
151 initialized_ = false;
152 }
153 executor_ = nullptr;
154 handle_id_ = 0;
155 }
156
157 GuardCondition(const GuardCondition&) = delete;
158 GuardCondition& operator=(const GuardCondition&) = delete;
159
160 friend class ::rclcpp::Node;
161
162 alignas(8) uint8_t storage_[NROS_GUARD_CONDITION_SIZE];
163 bool initialized_;
164
167 void* closure_;
168
170 void* executor_;
171 size_t handle_id_;
172};
173
174} // namespace rclcpp
175
176#endif // NROS_RCLCPP_GUARD_CONDITION_HPP
Definition client.hpp:129
Definition guard_condition.hpp:50
~GuardCondition()
Definition guard_condition.hpp:90
bool is_triggered() const
Definition guard_condition.hpp:73
bool is_valid() const
Check if the guard condition is initialized and valid.
Definition guard_condition.hpp:61
GuardCondition & operator=(GuardCondition &&other)
Definition guard_condition.hpp:111
Result trigger()
Definition guard_condition.hpp:55
Result clear()
Definition guard_condition.hpp:80
GuardCondition(GuardCondition &&other)
Definition guard_condition.hpp:99
GuardCondition()
Definition guard_condition.hpp:132
void attach_closure_block(detail::HostedBlockBase *block)
Definition guard_condition.hpp:138
Definition result.hpp:216
Inline storage-size macros for opaque entity buffers.
Definition rclcpp.hpp:75
ResultOf< void > Result
Definition result.hpp:246
rclcpp::Result, rclcpp::ErrorCode, and the RCLCPP_TRY macro.