libxr  1.0
Want to be the best embedded framework
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,
14 WAITING = 1,
15 DATA_READY = UINT32_MAX
16};
17
23{
24 void* buff_addr;
25 void (*copy_payload)(void* dst,
26 void* payload_addr);
28 std::atomic<ASyncSubscriberState> state =
30};
31
38template <typename Data>
40{
41 public:
48 ASyncSubscriber(const char* name, Domain* domain = nullptr)
49 : ASyncSubscriber(Topic(WaitTopic(name, UINT32_MAX, domain)))
50 {
51 }
52
60 {
62
65 block_->data_.timestamp = MicrosecondTimestamp();
67 block_->data_.copy_payload = &Topic::CopyPayload<Data>;
68 topic.block_->data_.subers.Add(*block_);
69 }
70
76 ASyncSubscriber(const ASyncSubscriber& other) = delete;
77
84 ASyncSubscriber& operator=(const ASyncSubscriber& other) = delete;
85
94 ASyncSubscriber(ASyncSubscriber&& other) noexcept : block_(other.block_)
95 {
96 other.block_ = nullptr;
97 }
98
110 {
111 if (this != &other)
112 {
113 block_ = other.block_;
114 other.block_ = nullptr;
115 }
116 return *this;
117 }
118
124 {
125 return block_->data_.state.load(std::memory_order_acquire) ==
127 }
128
139 Data& GetData()
140 {
141 if (block_->data_.state.load(std::memory_order_acquire) ==
143 {
144 block_->data_.state.store(ASyncSubscriberState::IDLE, std::memory_order_release);
145 }
146 return *reinterpret_cast<Data*>(block_->data_.buff_addr);
147 }
148
153 MicrosecondTimestamp GetTimestamp() const { return block_->data_.timestamp; }
154
162 {
163 if (block_->data_.state.load(std::memory_order_acquire) ==
165 {
166 block_->data_.state.store(ASyncSubscriberState::WAITING,
167 std::memory_order_release);
168 }
169 }
170
172};
173} // 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:247
ASyncSubscriber(Topic topic)
通过 Topic 句柄构造异步订阅者 / Construct an asynchronous subscriber from a Topic handle
Definition async.hpp:59
ASyncSubscriber(ASyncSubscriber &&other) noexcept
移动构造异步订阅者 / Move-construct one asynchronous subscriber
Definition async.hpp:94
ASyncSubscriber(const ASyncSubscriber &other)=delete
禁止拷贝异步订阅者 / Copy construction is disabled for asynchronous subscribers
Data & GetData()
获取当前数据 / Retrieve the current data
Definition async.hpp:139
ASyncSubscriber & operator=(const ASyncSubscriber &other)=delete
禁止拷贝赋值异步订阅者 / Copy assignment is disabled for asynchronous subscribers
LockFreeList::Node< ASyncBlock > * block_
订阅者数据块。Subscriber data block.
Definition async.hpp:171
bool Available()
检查数据是否可用 / Check whether data is available
Definition async.hpp:123
MicrosecondTimestamp GetTimestamp() const
获取最近一次接收的消息时间戳 / Get the latest received message timestamp
Definition async.hpp:153
ASyncSubscriber(const char *name, Domain *domain=nullptr)
通过主题名称构造异步订阅者 / Construct an asynchronous subscriber by topic name
Definition async.hpp:48
ASyncSubscriber & operator=(ASyncSubscriber &&other) noexcept
移动赋值异步订阅者 / Move-assign one asynchronous subscriber
Definition async.hpp:109
void StartWaiting()
开始等待数据更新 / Start waiting for a data update
Definition async.hpp:161
topic 所属的命名域 / Naming domain that groups topics
Definition topic.hpp:179
发布订阅主题 / Publish-subscribe topic
Definition topic.hpp:57
@ ASYNC
异步本地缓冲型订阅者。Asynchronous local-buffer subscriber.
static void * AllocateSubscriberBuffer()
为订阅者分配一个长期存在的本地接收对象 / Allocate one long-lived local receive object for a subscriber
Definition topic.hpp:590
ASyncSubscriberState
异步订阅者本地缓冲区的状态 / State of the async subscriber's local buffer
Definition async.hpp:12
@ WAITING
等待下一次发布填充本地缓冲区。Waiting for the next publish to fill the local buffer.
@ DATA_READY
本地缓冲区已有一份待消费的新数据。One unread fresh sample is buffered locally.
@ IDLE
当前没有等待,也没有待消费的新数据。No wait is pending and no unread data is buffered.
static void CopyPayload(void *dst, void *payload_addr)
按精确类型把一份 payload 拷到订阅者缓冲区 / Copy one payload into a subscriber buffer using the exact type
Definition topic.hpp:605
static void CheckSubscriberType(Topic topic)
断言订阅者看到的精确 payload 类型与 topic 契约一致 / Assert that the exact payload type seen by a subscriber matches t...
Definition topic.hpp:574
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:191
TopicHandle block_
当前 topic 视图绑定的状态块。Runtime state block bound to the current topic view.
Definition topic.hpp:533
LibXR 命名空间
Definition ch32_can.hpp:14
异步订阅者自己挂的数据块 / Data block owned by one asynchronous subscriber
Definition async.hpp:23
std::atomic< ASyncSubscriberState > state
当前异步订阅状态。Current async subscriber state.
Definition async.hpp:28
void * buff_addr
长期存在的本地接收对象地址。Long-lived local receive object address.
Definition async.hpp:24
void(* copy_payload)(void *dst, void *payload_addr)
按订阅精确类型执行负载拷贝的适配函数。Adapter that copies one payload using the subscriber's exact type.
Definition async.hpp:25
MicrosecondTimestamp timestamp
最近接收的消息时间戳。Latest received message timestamp.
Definition async.hpp:27
所有订阅块共用的公共头 / Common header shared by all subscriber blocks
Definition topic.hpp:207