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_CPP_GUARD_CONDITION_HPP
11#define NROS_CPP_GUARD_CONDITION_HPP
12
13#include <cstdint>
14#include <cstddef>
15
16#include "nros/config.hpp"
17#include "nros/result.hpp"
18
19#ifdef NROS_CPP_STD
20#include <functional>
21#include <memory>
22#endif
23
24#include "nros_cpp_ffi.h"
25
26namespace nros {
27
46 public:
51 if (!initialized_) return Result(ErrorCode::NotInitialized);
53 }
54
56 bool is_valid() const { return initialized_; }
57
60 if (initialized_) {
62 initialized_ = false;
63 }
64 // closure_ (if any) destructs here.
65 }
66
67 // Move semantics (non-copyable). Relocation goes through the
68 // `nros_cpp_guard_condition_relocate` runtime call (Phase 84.C1).
70 : initialized_(other.initialized_)
72 ,
74#endif
75 {
76 if (other.initialized_) {
77 nros_cpp_guard_condition_relocate(other.storage_, storage_);
78 other.initialized_ = false;
79 }
80 }
81
83 if (this != &other) {
84 if (initialized_) {
86 initialized_ = false;
87 }
88 if (other.initialized_) {
89 nros_cpp_guard_condition_relocate(other.storage_, storage_);
90 initialized_ = true;
91 other.initialized_ = false;
92 }
93#ifdef NROS_CPP_STD
94 closure_ = std::move(other.closure_);
95#endif
96 }
97 return *this;
98 }
99
102 GuardCondition() : storage_(), initialized_(false) {}
103
104#ifdef NROS_CPP_STD
108 void attach_std_closure(std::unique_ptr<std::function<void()>> closure) {
109 closure_ = std::move(closure);
110 }
111#endif
112
113 private:
114 GuardCondition(const GuardCondition&) = delete;
115 GuardCondition& operator=(const GuardCondition&) = delete;
116
117 friend class Node;
118
119 alignas(8) uint8_t storage_[NROS_GUARD_CONDITION_SIZE];
120 bool initialized_;
121
122#ifdef NROS_CPP_STD
123 std::unique_ptr<std::function<void()>> closure_;
124#endif
125};
126
127} // namespace nros
128
129#endif // NROS_CPP_GUARD_CONDITION_HPP
Definition future.hpp:40
Definition guard_condition.hpp:45
GuardCondition()
Definition guard_condition.hpp:102
~GuardCondition()
Destructor — releases guard condition resources.
Definition guard_condition.hpp:59
Result trigger()
Definition guard_condition.hpp:50
GuardCondition(GuardCondition &&other)
Definition guard_condition.hpp:69
bool is_valid() const
Check if the guard condition is initialized and valid.
Definition guard_condition.hpp:56
GuardCondition & operator=(GuardCondition &&other)
Definition guard_condition.hpp:82
Definition node.hpp:158
Definition result.hpp:52
Inline storage-size macros for opaque entity buffers.
Definition nros.hpp:42
nros::Result, nros::ErrorCode, and the NROS_TRY macro.