Apollo  6.0
Open source self driving car software
raw_message.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2018 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 #ifndef CYBER_MESSAGE_RAW_MESSAGE_H_
18 #define CYBER_MESSAGE_RAW_MESSAGE_H_
19 
20 #include <cassert>
21 #include <cstring>
22 #include <memory>
23 #include <string>
24 
26 
27 namespace apollo {
28 namespace cyber {
29 namespace message {
30 
31 struct RawMessage {
32  RawMessage() : message(""), timestamp(0) {}
33 
34  explicit RawMessage(const std::string &data) : message(data), timestamp(0) {}
35 
36  RawMessage(const std::string &data, uint64_t ts)
37  : message(data), timestamp(ts) {}
38 
39  RawMessage(const RawMessage &raw_msg)
40  : message(raw_msg.message), timestamp(raw_msg.timestamp) {}
41 
42  RawMessage &operator=(const RawMessage &raw_msg) {
43  if (this != &raw_msg) {
44  this->message = raw_msg.message;
45  this->timestamp = raw_msg.timestamp;
46  }
47  return *this;
48  }
49 
51 
52  class Descriptor {
53  public:
54  std::string full_name() const { return "apollo.cyber.message.RawMessage"; }
55  std::string name() const { return "apollo.cyber.message.RawMessage"; }
56  };
57 
58  static const Descriptor *descriptor() {
59  static Descriptor desc;
60  return &desc;
61  }
62 
63  static void GetDescriptorString(const std::string &type,
64  std::string *desc_str) {
65  ProtobufFactory::Instance()->GetDescriptorString(type, desc_str);
66  }
67 
68  bool SerializeToArray(void *data, int size) const {
69  if (data == nullptr || size < ByteSize()) {
70  return false;
71  }
72 
73  memcpy(data, message.data(), message.size());
74  return true;
75  }
76 
77  bool SerializeToString(std::string *str) const {
78  if (str == nullptr) {
79  return false;
80  }
81  *str = message;
82  return true;
83  }
84 
85  bool ParseFromArray(const void *data, int size) {
86  if (data == nullptr || size <= 0) {
87  return false;
88  }
89 
90  message.assign(reinterpret_cast<const char *>(data), size);
91  return true;
92  }
93 
94  bool ParseFromString(const std::string &str) {
95  message = str;
96  return true;
97  }
98 
99  int ByteSize() const { return static_cast<int>(message.size()); }
100 
101  static std::string TypeName() { return "apollo.cyber.message.RawMessage"; }
102 
103  std::string message;
104  uint64_t timestamp;
105 };
106 
107 } // namespace message
108 } // namespace cyber
109 } // namespace apollo
110 
111 #endif // CYBER_MESSAGE_RAW_MESSAGE_H_
RawMessage(const RawMessage &raw_msg)
Definition: raw_message.h:39
static const Descriptor * descriptor()
Definition: raw_message.h:58
uint64_t timestamp
Definition: raw_message.h:104
bool SerializeToString(std::string *str) const
Definition: raw_message.h:77
bool ParseFromString(const std::string &str)
Definition: raw_message.h:94
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
RawMessage(const std::string &data)
Definition: raw_message.h:34
Definition: raw_message.h:31
RawMessage(const std::string &data, uint64_t ts)
Definition: raw_message.h:36
std::string message
Definition: raw_message.h:103
~RawMessage()
Definition: raw_message.h:50
RawMessage()
Definition: raw_message.h:32
bool ParseFromArray(const void *data, int size)
Definition: raw_message.h:85
RawMessage & operator=(const RawMessage &raw_msg)
Definition: raw_message.h:42
static std::string TypeName()
Definition: raw_message.h:101
bool SerializeToArray(void *data, int size) const
Definition: raw_message.h:68
std::string name() const
Definition: raw_message.h:55
std::string full_name() const
Definition: raw_message.h:54
static void GetDescriptorString(const std::string &type, std::string *desc_str)
Definition: raw_message.h:63
int ByteSize() const
Definition: raw_message.h:99