libxr
Embedded C++ framework for bare-metal, RTOS and Linux
Loading...
Searching...
No Matches
async.hpp
1#pragma once
2
3#include "../topic.hpp"
4
5namespace LibXR
6{
11enum class Topic::ASyncSubscriberState : uint32_t
12{
13 IDLE = 0,
15 WAITING = 1,
17 DATA_READY = UINT32_MAX
19};
20
26{
27 void* buff_addr;
29 void (*copy_payload)(
30 void* dst,
31 void* payload_addr);
35 std::atomic<ASyncSubscriberState> state =
37};
38
45template <typename Data>
47{
48 public:
57 ASyncSubscriber(const char* name, Domain* domain = nullptr)
58 : ASyncSubscriber(Topic(WaitTopic(name, UINT32_MAX, domain)))
59 {
60 }
61
70 {
71 Topic::CheckSubscriberType<Data>(topic);
72
75 block_->data_.timestamp = MicrosecondTimestamp();
76 block_->data_.buff_addr = Topic::AllocateSubscriberBuffer<Data>();
77 block_->data_.copy_payload = &Topic::CopyPayload<Data>;
78 topic.block_->data_.subers.Add(*block_);
79 }
80
86 ASyncSubscriber(const ASyncSubscriber& other) = delete;
87
94 ASyncSubscriber& operator=(const ASyncSubscriber& other) = delete;
95
104 ASyncSubscriber(ASyncSubscriber&& other) noexcept : block_(other.block_)
105 {
106 other.block_ = nullptr;
107 }
108
120 {
121 if (this != &other)
122 {
123 block_ = other.block_;
124 other.block_ = nullptr;
125 }
126 return *this;
127 }
128
135 {
136 return block_->data_.state.load(std::memory_order_acquire) ==
138 }
139
150 Data& GetData()
151 {
152 if (block_->data_.state.load(std::memory_order_acquire) ==
154 {
155 block_->data_.state.store(ASyncSubscriberState::IDLE, std::memory_order_release);
156 }
157 return *reinterpret_cast<Data*>(block_->data_.buff_addr);
158 }
159
164 MicrosecondTimestamp GetTimestamp() const { return block_->data_.timestamp; }
165
173 {
174 if (block_->data_.state.load(std::memory_order_acquire) == ASyncSubscriberState::IDLE)
175 {
176 block_->data_.state.store(ASyncSubscriberState::WAITING, std::memory_order_release);
177 }
178 }
179
181 nullptr;
182};
183} // namespace LibXR
数据节点模板,继承自 BaseNode,用于存储具体数据类型。 Template data node that inherits from BaseNode to store specific data...
Data data_
存储的数据 / The stored data.
微秒时间戳 / Microsecond timestamp
Data data_
存储的数据 / Stored data.
Definition rbt.hpp:98
先 StartWaiting(),再自己来取数据的订阅者 / Subscriber that first calls StartWaiting() and later pulls the data it...
Definition topic.hpp:277
ASyncSubscriber(Topic topic)
通过 Topic 句柄构造异步订阅者 / Construct an asynchronous subscriber from a Topic handle
Definition async.hpp:69
ASyncSubscriber(ASyncSubscriber &&other) noexcept
移动构造异步订阅者 / Move-construct one asynchronous subscriber
Definition async.hpp:104
ASyncSubscriber(const ASyncSubscriber &other)=delete
禁止拷贝异步订阅者 / Copy construction is disabled for asynchronous subscribers
Data & GetData()
获取当前数据 / Retrieve the current data
Definition async.hpp:150
ASyncSubscriber & operator=(const ASyncSubscriber &other)=delete
禁止拷贝赋值异步订阅者 / Copy assignment is disabled for asynchronous subscribers
LockFreeList::Node< ASyncBlock > * block_
订阅者数据块 / Subscriber data block.
Definition async.hpp:180
bool Available()
检查数据是否可用 / Check whether data is available
Definition async.hpp:134
MicrosecondTimestamp GetTimestamp() const
获取最近一次接收的消息时间戳 / Get the latest received message timestamp
Definition async.hpp:164
ASyncSubscriber(const char *name, Domain *domain=nullptr)
通过主题名称构造异步订阅者 / Construct an asynchronous subscriber by topic name
Definition async.hpp:57
ASyncSubscriber & operator=(ASyncSubscriber &&other) noexcept
移动赋值异步订阅者 / Move-assign one asynchronous subscriber
Definition async.hpp:119
void StartWaiting()
开始等待数据更新 / Start waiting for a data update
Definition async.hpp:172
topic 所属的命名域 / Naming domain that groups topics
Definition topic.hpp:207
发布订阅主题 / Publish-subscribe topic
Definition topic.hpp:56
@ ASYNC
异步本地缓冲型订阅者 / Asynchronous local-buffer subscriber.
ASyncSubscriberState
异步订阅者本地缓冲区的状态 / State of the async subscriber's local buffer
Definition async.hpp:12
static TopicHandle WaitTopic(const char *name, uint32_t timeout=UINT32_MAX, Domain *domain=nullptr)
等待指定名称的 topic 出现 / Wait until a topic with the given name exists
Definition topic.cpp:206
LibXR 命名空间 / LibXR namespace.
Definition ch32_can.hpp:14
异步订阅者自己挂的数据块 / Data block owned by one asynchronous subscriber
Definition async.hpp:26
std::atomic< ASyncSubscriberState > state
当前异步订阅状态 / Current async subscriber state.
Definition async.hpp:35
void(* copy_payload)(void *dst, void *payload_addr)
Definition async.hpp:29
MicrosecondTimestamp timestamp
最近接收的消息时间戳 / Latest received message timestamp.
Definition async.hpp:34
所有订阅块共用的公共头 / Common header shared by all subscriber blocks
Definition topic.hpp:237