Apollo  6.0
Open source self driving car software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bridge_proto_diserialized_buf.h
Go to the documentation of this file.
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 
27 namespace apollo {
28 namespace bridge {
29 
30 constexpr 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 
49 template <typename T>
51  public:
53  explicit BridgeProtoDiserializedBuf(const std::string &topic_name)
54  : topic_name_(topic_name) {}
55  virtual ~BridgeProtoDiserializedBuf();
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 
83 template <typename T>
85  FREE_ARRY(proto_buf_);
86 }
87 
88 template <typename T>
89 bool 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 
97 template <typename T>
98 void BridgeProtoDiserializedBuf<T>::UpdateStatus(uint32_t frame_index) {
99  size_t status_size = status_list_.size();
100  if (status_size == 0) {
101  is_ready_diser = false;
102  return;
103  }
104 
105  uint32_t status_index = frame_index / INT_BITS;
106  status_list_[status_index] |= (1 << (frame_index % INT_BITS));
107  for (size_t i = 0; i < status_size; i++) {
108  if (i == status_size - 1) {
109  if (static_cast<int>(status_list_[i]) ==
110  (1 << total_frames_ % INT_BITS) - 1) {
111  AINFO << "diserialized is ready";
112  is_ready_diser = true;
113  } else {
114  is_ready_diser = false;
115  break;
116  }
117  } else {
118  if (status_list_[i] != 0xffffffff) {
119  is_ready_diser = false;
120  break;
121  }
122  is_ready_diser = true;
123  }
124  }
125 }
126 
127 template <typename T>
129  if (strcmp(proto_name_.c_str(), header.GetMsgName().c_str()) == 0 &&
130  sequence_num_ == header.GetMsgID()) {
131  return true;
132  }
133  return false;
134 }
135 
136 template <typename T>
138  total_size_ = header.GetMsgSize();
139  total_frames_ = header.GetTotalFrames();
140  if (total_frames_ == 0) {
141  return false;
142  }
143  int status_size = static_cast<int>(total_frames_ / INT_BITS +
144  ((total_frames_ % INT_BITS) ? 1 : 0));
145  if (status_list_.empty()) {
146  for (int i = 0; i < status_size; i++) {
147  status_list_.push_back(0);
148  }
149  }
150 
151  if (!proto_buf_) {
152  proto_buf_ = new char[total_size_];
153  }
154  return true;
155 }
156 
157 template <typename T>
159  const BridgeHeader &header, std::shared_ptr<cyber::Node> node) {
160  writer_ = node->CreateWriter<T>(topic_name_.c_str());
161  return Initialize(header);
162 }
163 
164 template <typename T>
166  auto pb_msg = std::make_shared<T>();
167  if (!Diserialized(pb_msg)) {
168  return false;
169  }
170  writer_->Write(pb_msg);
171  return true;
172 }
173 
174 } // namespace bridge
175 } // namespace apollo
virtual bool Initialize(const BridgeHeader &header, std::shared_ptr< cyber::Node > node)=0
virtual bool IsTheProto(const BridgeHeader &header)=0
virtual bool IsReadyDiserialize() const
Definition: bridge_proto_diserialized_buf.h:61
BridgeProtoDiserializedBuf()
Definition: bridge_proto_diserialized_buf.h:52
virtual char * GetBuf(size_t offset)=0
virtual std::string GetMsgName() const =0
virtual void UpdateStatus(uint32_t frame_index)=0
ProtoDiserializedBufBase()
Definition: bridge_proto_diserialized_buf.h:34
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
virtual bool IsTheProto(const BridgeHeader &header)
Definition: bridge_proto_diserialized_buf.h:128
std::string GetMsgName() const
Definition: bridge_header.h:49
bsize GetMsgSize() const
Definition: bridge_header.h:54
Definition: bridge_header.h:33
Definition: bridge_proto_diserialized_buf.h:32
virtual bool IsReadyDiserialize() const =0
virtual bool Initialize(const BridgeHeader &header, std::shared_ptr< cyber::Node > node)
Definition: bridge_proto_diserialized_buf.h:158
virtual uint32_t GetMsgID() const
Definition: bridge_proto_diserialized_buf.h:68
virtual char * GetBuf(size_t offset)
Definition: bridge_proto_diserialized_buf.h:67
virtual void UpdateStatus(uint32_t frame_index)
Definition: bridge_proto_diserialized_buf.h:98
uint32_t GetMsgID() const
Definition: bridge_header.h:50
virtual bool DiserializedAndPub()
Definition: bridge_proto_diserialized_buf.h:165
#define FREE_ARRY(arry)
Definition: macro.h:24
Definition: bridge_proto_diserialized_buf.h:50
uint32_t GetTotalFrames() const
Definition: bridge_header.h:51
virtual std::string GetMsgName() const
Definition: bridge_proto_diserialized_buf.h:69
virtual ~ProtoDiserializedBufBase()
Definition: bridge_proto_diserialized_buf.h:35
constexpr uint32_t INT_BITS
Definition: bridge_proto_diserialized_buf.h:30
BridgeProtoDiserializedBuf(const std::string &topic_name)
Definition: bridge_proto_diserialized_buf.h:53
#define AINFO
Definition: log.h:42
virtual ~BridgeProtoDiserializedBuf()
Definition: bridge_proto_diserialized_buf.h:84
virtual uint32_t GetMsgID() const =0
bool Diserialized(std::shared_ptr< T > proto)
Definition: bridge_proto_diserialized_buf.h:89