Apollo  6.0
Open source self driving car software
util.h
Go to the documentation of this file.
1 /******************************************************************************
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 <cstring>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
26 
27 namespace apollo {
28 namespace bridge {
29 
30 const int HEADER_BUF_SIZE = sizeof(size_t);
31 template <typename T>
32 void WriteToBuffer(BridgeBuffer<char> *buf, const std::shared_ptr<T> &pb_msg) {
33  if (!buf) {
34  return;
35  }
36  size_t msg_len = pb_msg->ByteSize();
37  size_t total_size = HEADER_BUF_SIZE + msg_len;
38 
39  buf->reset(total_size);
40 
41  buf->write(0, reinterpret_cast<char *>(&msg_len), sizeof(size_t));
42  pb_msg->SerializeToArray(reinterpret_cast<char *>(buf + sizeof(size_t)),
43  static_cast<int>(msg_len));
44 }
45 
46 template <typename T>
47 bool RemoveItem(std::vector<T *> *list, const T *t) {
48  if (!list) {
49  return false;
50  }
51  typename std::vector<T *>::iterator itor = list->begin();
52  for (; itor != list->end();) {
53  if (*itor == t) {
54  T *tmp = *itor;
55  FREE_POINTER(tmp);
56  itor = list->erase(itor);
57  continue;
58  }
59  ++itor;
60  }
61  return true;
62 }
63 
64 template <typename T>
65 bool RemoveItem(std::vector<std::shared_ptr<T>> *list, std::shared_ptr<T> t) {
66  if (!list) {
67  return false;
68  }
69  typename std::vector<std::shared_ptr<T>>::iterator itor = list->begin();
70  for (; itor != list->end();) {
71  if (itor->get() == t.get()) {
72  itor = list->erase(itor);
73  continue;
74  }
75  ++itor;
76  }
77  return true;
78 }
79 
80 int GetProtoSize(const char *buf, size_t size);
81 
82 } // namespace bridge
83 } // namespace apollo
bool RemoveItem(std::vector< T *> *list, const T *t)
Definition: util.h:47
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void WriteToBuffer(BridgeBuffer< char > *buf, const std::shared_ptr< T > &pb_msg)
Definition: util.h:32
void write(size_t index, const T *data, size_t size)
const int HEADER_BUF_SIZE
Definition: util.h:30
int GetProtoSize(const char *buf, size_t size)
#define FREE_POINTER(p)
Definition: macro.h:30
Definition: bridge_buffer.h:25