Apollo  6.0
Open source self driving car software
bridge_header_item.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 <string>
20 
21 namespace apollo {
22 namespace bridge {
23 
24 typedef uint32_t bsize;
25 
26 enum HType {
36 
38 };
39 
41  public:
42  HeaderItemBase() = default;
43  virtual ~HeaderItemBase() {}
44 
45  public:
46  virtual char *SerializeItem(char *buf, size_t buf_size) = 0;
47  virtual const char *DiserializeItem(const char *buf,
48  size_t *diserialized_size) = 0;
49  virtual HType GetType() const = 0;
50 };
51 
52 template <enum HType t, typename T>
53 struct HeaderItem;
54 
55 template <enum HType t, typename T>
56 char *SerializeItemImp(const HeaderItem<t, T> &item, char *buf,
57  size_t buf_size) {
58  if (!buf || buf_size == 0 ||
59  buf_size < size_t(sizeof(t) + item.ValueSize() + 3)) {
60  return nullptr;
61  }
62  char *res = buf;
63  size_t item_size = item.ValueSize();
64 
65  HType type = t;
66  memcpy(res, &type, sizeof(HType));
67  res[sizeof(HType)] = ':';
68  res = res + sizeof(HType) + 1;
69 
70  memcpy(res, &item_size, sizeof(size_t));
71  res[sizeof(bsize)] = ':';
72  res = res + sizeof(bsize) + 1;
73 
74  memcpy(res, item.GetValuePtr(), item.ValueSize());
75  res[item.ValueSize()] = '\n';
76  res += item.ValueSize() + 1;
77  return res;
78 }
79 
80 template <enum HType t, typename T>
81 const char *DiserializeItemImp(HeaderItem<t, T> *item, const char *buf,
82  size_t *diserialized_size) {
83  if (!buf || !diserialized_size) {
84  return nullptr;
85  }
86  const char *res = buf;
87 
88  char p_type[sizeof(HType)] = {0};
89  memcpy(p_type, buf, sizeof(HType));
90  HType type = *(reinterpret_cast<HType *>(p_type));
91  if (type != t) {
92  return nullptr;
93  }
94  res += sizeof(HType) + 1;
95  *diserialized_size += sizeof(HType) + 1;
96 
97  char p_size[sizeof(bsize)] = {0};
98  memcpy(p_size, res, sizeof(bsize));
99  bsize size = *(reinterpret_cast<bsize *>(p_size));
100  res += sizeof(bsize) + 1;
101  *diserialized_size += sizeof(bsize) + 1;
102 
103  item->SetValue(res);
104  res += size + 1;
105  *diserialized_size += size + 1;
106  return res;
107 }
108 
109 template <enum HType t, typename T>
110 struct HeaderItem : public HeaderItemBase {
112 
113  operator T() { return value_; }
114  HeaderItem &operator=(const T &val) {
115  value_ = val;
116  return *this;
117  }
118  HType GetType() const override { return t; }
119  size_t ValueSize() const { return sizeof(value_); }
120  const T *GetValuePtr() const { return &value_; }
121  void SetValue(const char *buf) {
122  if (!buf) {
123  return;
124  }
125  value_ = *(reinterpret_cast<const T *>(buf));
126  }
127 
128  char *SerializeItem(char *buf, size_t buf_size) override {
129  return SerializeItemImp(*this, buf, buf_size);
130  }
131 
132  const char *DiserializeItem(const char *buf,
133  size_t *diserialized_size) override {
134  return DiserializeItemImp(this, buf, diserialized_size);
135  }
136 };
137 
138 template <enum HType t>
139 struct HeaderItem<t, std::string> : public HeaderItemBase {
140  std::string value_;
141  operator std::string() { return value_; }
142  HeaderItem &operator=(const std::string &val) {
143  value_ = val;
144  return *this;
145  }
146  size_t ValueSize() const { return value_.length() + 1; }
147  HType GetType() const override { return t; }
148  const char *GetValuePtr() const { return value_.c_str(); }
149  void SetValue(const char *buf) {
150  if (!buf) {
151  return;
152  }
153  value_ = std::string(buf);
154  }
155 
156  char *SerializeItem(char *buf, size_t buf_size) override {
157  return SerializeItemImp(*this, buf, buf_size);
158  }
159 
160  const char *DiserializeItem(const char *buf,
161  size_t *diserialized_size) override {
162  return DiserializeItemImp(this, buf, diserialized_size);
163  }
164 };
165 
166 } // namespace bridge
167 } // namespace apollo
HType GetType() const override
Definition: bridge_header_item.h:147
const char * GetValuePtr() const
Definition: bridge_header_item.h:148
void SetValue(const char *buf)
Definition: bridge_header_item.h:121
virtual const char * DiserializeItem(const char *buf, size_t *diserialized_size)=0
Definition: bridge_header_item.h:30
Definition: bridge_header_item.h:40
Definition: bridge_header_item.h:27
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
const char * DiserializeItem(const char *buf, size_t *diserialized_size) override
Definition: bridge_header_item.h:160
virtual ~HeaderItemBase()
Definition: bridge_header_item.h:43
Definition: bridge_header_item.h:35
std::string value_
Definition: bridge_header_item.h:140
Definition: future.h:29
HeaderItem & operator=(const std::string &val)
Definition: bridge_header_item.h:142
size_t ValueSize() const
Definition: bridge_header_item.h:119
Definition: bridge_header_item.h:53
HeaderItem & operator=(const T &val)
Definition: bridge_header_item.h:114
char * SerializeItem(char *buf, size_t buf_size) override
Definition: bridge_header_item.h:128
uint32_t bsize
Definition: bridge_header_item.h:24
size_t ValueSize() const
Definition: bridge_header_item.h:146
const T * GetValuePtr() const
Definition: bridge_header_item.h:120
T value_
Definition: bridge_header_item.h:111
Definition: bridge_header_item.h:32
Definition: bridge_header_item.h:28
void SetValue(const char *buf)
Definition: bridge_header_item.h:149
const char * DiserializeItem(const char *buf, size_t *diserialized_size) override
Definition: bridge_header_item.h:132
char * SerializeItem(char *buf, size_t buf_size) override
Definition: bridge_header_item.h:156
virtual char * SerializeItem(char *buf, size_t buf_size)=0
const char * DiserializeItemImp(HeaderItem< t, T > *item, const char *buf, size_t *diserialized_size)
Definition: bridge_header_item.h:81
Definition: bridge_header_item.h:37
HType
Definition: bridge_header_item.h:26
virtual HType GetType() const =0
char * SerializeItemImp(const HeaderItem< t, T > &item, char *buf, size_t buf_size)
Definition: bridge_header_item.h:56
Definition: bridge_header_item.h:31
HType GetType() const override
Definition: bridge_header_item.h:118
Definition: bridge_header_item.h:33
Definition: bridge_header_item.h:29
Definition: bridge_header_item.h:34