Skip to content

Commit 58b16fc

Browse files
committed
优化 SharedTopicClient 类的代码格式,统一指针和引用的使用风格
1 parent dec35fd commit 58b16fc

1 file changed

Lines changed: 43 additions & 49 deletions

File tree

SharedTopicClient.hpp

Lines changed: 43 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,63 @@ depends: []
1919
#include "app_framework.hpp"
2020
#include "uart.hpp"
2121

22-
class SharedTopicClient : public LibXR::Application
23-
{
22+
class SharedTopicClient : public LibXR::Application {
2423
public:
25-
typedef struct
26-
{
27-
SharedTopicClient *client;
24+
typedef struct {
25+
SharedTopicClient* client;
2826
uint32_t topic_crc32;
2927
uint32_t index;
3028
} CallbackInfo;
3129

32-
struct TopicConfig
33-
{
34-
const char *name;
35-
const char *domain = "libxr_def_domain";
30+
struct TopicConfig {
31+
const char* name;
32+
const char* domain = "libxr_def_domain";
3633

37-
TopicConfig(const char *name) : name(name) {}
34+
TopicConfig(const char* name) : name(name) {}
3835

39-
TopicConfig(const char *name, const char *domain) : name(name), domain(domain) {}
36+
TopicConfig(const char* name, const char* domain)
37+
: name(name), domain(domain) {}
4038
};
4139

42-
SharedTopicClient(LibXR::HardwareContainer &hw, LibXR::ApplicationManager &app,
43-
const char *uart_name, uint32_t task_stack_depth,
44-
uint32_t buffer_size,
40+
SharedTopicClient(LibXR::HardwareContainer& hw,
41+
LibXR::ApplicationManager& app, const char* uart_name,
42+
uint32_t task_stack_depth, uint32_t buffer_size,
4543
std::initializer_list<TopicConfig> topic_configs)
4644
: uart_(hw.template Find<LibXR::UART>(uart_name)),
4745
tx_buffer_(new uint8_t[buffer_size], buffer_size),
48-
tx_queue_(buffer_size)
49-
{
46+
tx_queue_(buffer_size) {
5047
ASSERT(uart_ != nullptr);
5148

5249
topics_pack_buffer_ = new LibXR::RawData[topic_configs.size()];
5350

5451
uint32_t i = 0;
5552

56-
for (auto config : topic_configs)
57-
{
53+
for (auto config : topic_configs) {
5854
auto domain = LibXR::Topic::Domain(config.domain);
5955
auto ans = LibXR::Topic::Find(config.name, &domain);
60-
if (ans == nullptr)
61-
{
56+
if (ans == nullptr) {
6257
XR_LOG_ERROR("Topic not found: %s/%s", config.domain, config.name);
6358
ASSERT(false);
6459
}
6560
topics_pack_buffer_[i] = LibXR::RawData(
6661
new uint8_t[ans->data_.max_length + LibXR::Topic::PACK_BASE_SIZE],
6762
ans->data_.max_length + LibXR::Topic::PACK_BASE_SIZE);
6863

69-
void (*func)(bool, CallbackInfo, LibXR::RawData &) =
70-
[](bool in_isr, CallbackInfo info, LibXR::RawData &data)
71-
{
72-
LibXR::WriteOperation op;
73-
LibXR::Topic::PackData(info.topic_crc32,
74-
info.client->topics_pack_buffer_[info.index], data);
75-
info.client->tx_queue_.PushBatch(
76-
static_cast<uint8_t *>(info.client->topics_pack_buffer_[info.index].addr_),
77-
info.client->topics_pack_buffer_[info.index].size_);
78-
info.client->tx_sem_.PostFromCallback(in_isr);
79-
};
80-
81-
auto msg_cb =
82-
LibXR::Topic::Callback::Create(func, CallbackInfo{this, ans->data_.crc32, i});
64+
void (*func)(bool, CallbackInfo, LibXR::RawData&) =
65+
[](bool in_isr, CallbackInfo info, LibXR::RawData& data) {
66+
LibXR::WriteOperation op;
67+
LibXR::Topic::PackData(info.topic_crc32,
68+
info.client->topics_pack_buffer_[info.index],
69+
data);
70+
info.client->tx_queue_.PushBatch(
71+
static_cast<uint8_t*>(
72+
info.client->topics_pack_buffer_[info.index].addr_),
73+
info.client->topics_pack_buffer_[info.index].size_);
74+
info.client->tx_sem_.PostFromCallback(in_isr);
75+
};
76+
77+
auto msg_cb = LibXR::Topic::Callback::Create(
78+
func, CallbackInfo{this, ans->data_.crc32, i});
8379

8480
LibXR::Topic topic(ans);
8581

@@ -88,37 +84,35 @@ class SharedTopicClient : public LibXR::Application
8884
i++;
8985
}
9086

91-
tx_thread_.Create(this, TxThreadFun, "SharedTopicClientTxThread", task_stack_depth,
92-
LibXR::Thread::Priority::REALTIME);
87+
tx_thread_.Create(this, TxThreadFun, "SharedTopicClientTxThread",
88+
task_stack_depth, LibXR::Thread::Priority::REALTIME);
9389

9490
app.Register(*this);
9591
}
9692

97-
static void TxThreadFun(SharedTopicClient *client)
98-
{
93+
static void TxThreadFun(SharedTopicClient* client) {
9994
LibXR::Semaphore write_op_sem;
10095
LibXR::WriteOperation op(write_op_sem);
10196
LibXR::WriteOperation op_none;
102-
while (true)
103-
{
97+
while (true) {
10498
client->tx_sem_.Wait();
105-
auto size = LibXR::min(client->tx_queue_.Size(), client->tx_buffer_.size_);
106-
if (size > 0 &&
107-
client->tx_queue_.PopBatch(static_cast<uint8_t *>(client->tx_buffer_.addr_),
108-
size) == ErrorCode::OK)
109-
{
110-
client->uart_->Write({static_cast<uint8_t *>(client->tx_buffer_.addr_), size},
111-
op_none);
99+
auto size =
100+
LibXR::min(client->tx_queue_.Size(), client->tx_buffer_.size_);
101+
if (size > 0 && client->tx_queue_.PopBatch(
102+
static_cast<uint8_t*>(client->tx_buffer_.addr_),
103+
size) == ErrorCode::OK) {
104+
client->uart_->Write(
105+
{static_cast<uint8_t*>(client->tx_buffer_.addr_), size}, op_none);
112106
}
113107
}
114108
}
115109
void OnMonitor() override {}
116110

117111
private:
118-
LibXR::UART *uart_;
112+
LibXR::UART* uart_;
119113
LibXR::RawData tx_buffer_;
120114
LibXR::LockFreeQueue<uint8_t> tx_queue_;
121-
LibXR::RawData *topics_pack_buffer_;
115+
LibXR::RawData* topics_pack_buffer_;
122116
LibXR::Semaphore tx_sem_;
123117
LibXR::Thread tx_thread_;
124118
};

0 commit comments

Comments
 (0)