Skip to content

Commit d7fc4d2

Browse files
committed
优化 SharedTopic 类的 TopicConfig 结构,统一指针风格并简化构造函数
1 parent bc4aaee commit d7fc4d2

1 file changed

Lines changed: 35 additions & 47 deletions

File tree

SharedTopic.hpp

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,36 +29,33 @@ depends: []
2929
#include "thread.hpp"
3030
#include "uart.hpp"
3131

32-
class SharedTopic : public LibXR::Application
33-
{
32+
class SharedTopic : public LibXR::Application {
3433
public:
35-
struct TopicConfig
36-
{
37-
const char *name;
38-
const char *domain = "libxr_def_domain";
34+
struct TopicConfig {
35+
const char* name;
36+
const char* domain = "libxr_def_domain";
3937

40-
TopicConfig(const char *name) : name(name) {}
38+
TopicConfig(const char* name) : name(name) {}
4139

42-
TopicConfig(const char *name, const char *domain) : name(name), domain(domain) {}
40+
TopicConfig(const char* name, const char* domain)
41+
: name(name), domain(domain) {}
4342
};
4443

45-
SharedTopic(LibXR::HardwareContainer &hw, LibXR::ApplicationManager &app,
46-
const char *uart_name, uint32_t task_stack_depth, uint32_t buffer_size,
44+
SharedTopic(LibXR::HardwareContainer& hw, LibXR::ApplicationManager& app,
45+
const char* uart_name, uint32_t task_stack_depth,
46+
uint32_t buffer_size,
4747
std::initializer_list<TopicConfig> topic_configs)
4848
: uart_(hw.template Find<LibXR::UART>(uart_name)),
4949
server_(buffer_size),
5050
rx_buffer_(new uint8_t[buffer_size], buffer_size),
5151
cmd_name_(new char[sizeof("shared_topic:") + strlen(uart_name)]),
5252
cmd_file_((strcpy(cmd_name_, "shared_topic:"),
5353
strcpy(cmd_name_ + strlen("shared_topic:"), uart_name),
54-
LibXR::RamFS::CreateFile(cmd_name_, CommandFunc, this)))
55-
{
56-
for (auto config : topic_configs)
57-
{
54+
LibXR::RamFS::CreateFile(cmd_name_, CommandFunc, this))) {
55+
for (auto config : topic_configs) {
5856
auto domain = LibXR::Topic::Domain(config.domain);
5957
auto topic = LibXR::Topic::Find(config.name, &domain);
60-
if (topic == nullptr)
61-
{
58+
if (topic == nullptr) {
6259
XR_LOG_ERROR("Topic not found: %s/%s", config.domain, config.name);
6360
ASSERT(false);
6461
}
@@ -67,26 +64,24 @@ class SharedTopic : public LibXR::Application
6764

6865
hw.template FindOrExit<LibXR::RamFS>({"ramfs"})->Add(cmd_file_);
6966

70-
rx_thread_.Create(this, RxThreadFun, "SharedTopic::RxThread", task_stack_depth,
71-
LibXR::Thread::Priority::REALTIME);
67+
rx_thread_.Create(this, RxThreadFun, "SharedTopic::RxThread",
68+
task_stack_depth, LibXR::Thread::Priority::REALTIME);
7269

7370
app.Register(*this);
7471
}
7572

76-
static void RxThreadFun(SharedTopic *self)
77-
{
73+
static void RxThreadFun(SharedTopic* self) {
7874
LibXR::Semaphore sem;
7975
LibXR::ReadOperation op(sem);
80-
while (true)
81-
{
76+
while (true) {
8277
self->uart_->Read({nullptr, 0}, op);
83-
auto size =
84-
LibXR::max(sizeof(LibXR::Topic::PackedDataHeader),
85-
LibXR::min(self->uart_->read_port_->Size(), self->rx_buffer_.size_));
86-
auto ans = self->uart_->Read(LibXR::RawData{self->rx_buffer_.addr_, size}, op);
78+
auto size = LibXR::max(
79+
sizeof(LibXR::Topic::PackedDataHeader),
80+
LibXR::min(self->uart_->read_port_->Size(), self->rx_buffer_.size_));
81+
auto ans =
82+
self->uart_->Read(LibXR::RawData{self->rx_buffer_.addr_, size}, op);
8783

88-
if (ans == ErrorCode::OK)
89-
{
84+
if (ans == ErrorCode::OK) {
9085
self->server_.ParseData(LibXR::RawData{self->rx_buffer_.addr_, size});
9186
}
9287

@@ -96,34 +91,27 @@ class SharedTopic : public LibXR::Application
9691

9792
void OnMonitor() override {}
9893

99-
static int CommandFunc(SharedTopic *self, int argc, char **argv)
100-
{
101-
if (argc == 1)
102-
{
94+
static int CommandFunc(SharedTopic* self, int argc, char** argv) {
95+
if (argc == 1) {
10396
LibXR::STDIO::Printf("Usage:\r\n");
104-
LibXR::STDIO::Printf(" monitor [time_ms] [interval_ms] - test received speed\r\n");
97+
LibXR::STDIO::Printf(
98+
" monitor [time_ms] [interval_ms] - test received speed\r\n");
10599
return 0;
106-
}
107-
else if (argc == 4)
108-
{
109-
if (strcmp(argv[1], "monitor") == 0)
110-
{
100+
} else if (argc == 4) {
101+
if (strcmp(argv[1], "monitor") == 0) {
111102
int time = atoi(argv[2]);
112103
int delay = atoi(argv[3]);
113104
auto start = self->rx_count_;
114-
while (time > 0)
115-
{
105+
while (time > 0) {
116106
LibXR::Thread::Sleep(delay);
117107
LibXR::STDIO::Printf(
118-
"%f Mbps\r\n", static_cast<float>(self->rx_count_ - start) * 8.0 / 1024.0 /
119-
1024.0 / delay * 1000.0);
108+
"%f Mbps\r\n", static_cast<float>(self->rx_count_ - start) * 8.0 /
109+
1024.0 / 1024.0 / delay * 1000.0);
120110
time -= delay;
121111
start = self->rx_count_;
122112
}
123113
}
124-
}
125-
else
126-
{
114+
} else {
127115
LibXR::STDIO::Printf("Error: Invalid arguments.\r\n");
128116
return -1;
129117
}
@@ -132,15 +120,15 @@ class SharedTopic : public LibXR::Application
132120
}
133121

134122
private:
135-
LibXR::UART *uart_;
123+
LibXR::UART* uart_;
136124

137125
LibXR::Topic::Server server_;
138126

139127
LibXR::RawData rx_buffer_;
140128

141129
size_t rx_count_ = 0;
142130

143-
char *cmd_name_;
131+
char* cmd_name_;
144132

145133
LibXR::RamFS::File cmd_file_;
146134

0 commit comments

Comments
 (0)