Apollo 10.0
自动驾驶开放平台
bridge_proto_diserialized_buf.h
浏览该文件的文档.
1/******************************************************************************der
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
17#pragma once
18
19#include <memory>
20#include <string>
21#include <vector>
22
23#include "cyber/cyber.h"
26
27namespace apollo {
28namespace bridge {
29
30constexpr uint32_t INT_BITS = static_cast<uint32_t>(sizeof(uint32_t) * 8);
31
33 public:
36
37 virtual bool Initialize(const BridgeHeader &header,
38 std::shared_ptr<cyber::Node> node) = 0;
39
40 virtual bool DiserializedAndPub() = 0;
41 virtual bool IsReadyDiserialize() const = 0;
42 virtual bool IsTheProto(const BridgeHeader &header) = 0;
43 virtual void UpdateStatus(uint32_t frame_index) = 0;
44 virtual uint32_t GetMsgID() const = 0;
45 virtual std::string GetMsgName() const = 0;
46 virtual char *GetBuf(size_t offset) = 0;
47};
48
49template <typename T>
51 public:
53 explicit BridgeProtoDiserializedBuf(const std::string &topic_name)
54 : topic_name_(topic_name) {}
56
57 virtual bool DiserializedAndPub();
58 virtual bool Initialize(const BridgeHeader &header,
59 std::shared_ptr<cyber::Node> node);
60
61 virtual bool IsReadyDiserialize() const { return is_ready_diser; }
62 virtual void UpdateStatus(uint32_t frame_index);
63 virtual bool IsTheProto(const BridgeHeader &header);
64
65 bool Initialize(const BridgeHeader &header);
66 bool Diserialized(std::shared_ptr<T> proto);
67 virtual char *GetBuf(size_t offset) { return proto_buf_ + offset; }
68 virtual uint32_t GetMsgID() const { return sequence_num_; }
69 virtual std::string GetMsgName() const { return proto_name_; }
70
71 private:
72 size_t total_frames_ = 0;
73 size_t total_size_ = 0;
74 std::string proto_name_ = "";
75 std::vector<uint32_t> status_list_;
76 char *proto_buf_ = nullptr;
77 bool is_ready_diser = false;
78 uint32_t sequence_num_ = 0;
79 std::shared_ptr<cyber::Writer<T>> writer_;
80 std::string topic_name_ = "";
81};
82
83template <typename T>
87
88template <typename T>
89bool BridgeProtoDiserializedBuf<T>::Diserialized(std::shared_ptr<T> proto) {
90 if (!proto_buf_ || !proto) {
91 return false;
92 }
93 proto->ParseFromArray(proto_buf_, static_cast<int>(total_size_));
94 return true;
95}
96
97template <typename T>
99 size_t status_size = status_list_.size();
100 uint32_t status_index = frame_index / INT_BITS;
101 if (status_size == 0 ||
102 static_cast<std::size_t>(status_index) >= status_size) {
103 is_ready_diser = false;
104 return;
105 }
106
107 status_list_[status_index] |= (1 << (frame_index % INT_BITS));
108 for (size_t i = 0; i < status_size; i++) {
109 if (i == status_size - 1) {
110 if (static_cast<int>(status_list_[i]) ==
111 (1 << total_frames_ % INT_BITS) - 1) {
112 AINFO << "diserialized is ready";
113 is_ready_diser = true;
114 } else {
115 is_ready_diser = false;
116 break;
117 }
118 } else {
119 if (status_list_[i] != 0xffffffff) {
120 is_ready_diser = false;
121 break;
122 }
123 is_ready_diser = true;
124 }
125 }
126}
127
128template <typename T>
130 if (strcmp(proto_name_.c_str(), header.GetMsgName().c_str()) == 0 &&
131 sequence_num_ == header.GetMsgID()) {
132 return true;
133 }
134 return false;
135}
136
137template <typename T>
139 total_size_ = header.GetMsgSize();
140 total_frames_ = header.GetTotalFrames();
141 proto_name_ = header.GetMsgName();
142 sequence_num_ = header.GetMsgID();
143 if (total_frames_ == 0) {
144 return false;
145 }
146 int status_size = static_cast<int>(total_frames_ / INT_BITS +
147 ((total_frames_ % INT_BITS) ? 1 : 0));
148 if (status_list_.empty()) {
149 for (int i = 0; i < status_size; i++) {
150 status_list_.push_back(0);
151 }
152 }
153
154 if (!proto_buf_) {
155 try {
156 proto_buf_ = new char[total_size_];
157 } catch (const std::bad_alloc& e) {
158 AERROR << "Memory allocation failed: " << e.what();
159 return false;
160 }
161 }
162 return true;
163}
164
165template <typename T>
167 const BridgeHeader &header, std::shared_ptr<cyber::Node> node) {
168 writer_ = node->CreateWriter<T>(topic_name_.c_str());
169 return Initialize(header);
170}
171
172template <typename T>
174 auto pb_msg = std::make_shared<T>();
175 if (!Diserialized(pb_msg)) {
176 return false;
177 }
178 writer_->Write(pb_msg);
179 return true;
180}
181
182} // namespace bridge
183} // namespace apollo
std::string GetMsgName() const
uint32_t GetTotalFrames() const
virtual bool Initialize(const BridgeHeader &header, std::shared_ptr< cyber::Node > node)
virtual bool IsTheProto(const BridgeHeader &header)
virtual uint32_t GetMsgID() const =0
virtual char * GetBuf(size_t offset)=0
virtual void UpdateStatus(uint32_t frame_index)=0
virtual bool Initialize(const BridgeHeader &header, std::shared_ptr< cyber::Node > node)=0
virtual std::string GetMsgName() const =0
virtual bool IsTheProto(const BridgeHeader &header)=0
virtual bool IsReadyDiserialize() const =0
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
#define FREE_ARRY(arry)
Definition macro.h:24
class register implement
Definition arena_queue.h:37