Apollo 10.0
自动驾驶开放平台
raw_message.h
浏览该文件的文档.
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
27
28namespace apollo {
29namespace cyber {
30namespace message {
31
32struct RawMessage {
34
35 explicit RawMessage(const std::string &data) : message(data), timestamp(0) {}
36
37 RawMessage(const std::string &data, uint64_t ts)
38 : message(data), timestamp(ts) {}
39
40 RawMessage(const RawMessage &raw_msg)
41 : message(raw_msg.message), timestamp(raw_msg.timestamp) {}
42
43 RawMessage &operator=(const RawMessage &raw_msg) {
44 if (this != &raw_msg) {
45 this->message = raw_msg.message;
46 this->timestamp = raw_msg.timestamp;
47 }
48 return *this;
49 }
50
52
53 class Descriptor {
54 public:
55 std::string full_name() const { return "apollo.cyber.message.RawMessage"; }
56 std::string name() const { return "apollo.cyber.message.RawMessage"; }
57 };
58
59 static const Descriptor *descriptor() {
60 static Descriptor desc;
61 return &desc;
62 }
63
64 static void GetDescriptorString(const std::string &type,
65 std::string *desc_str) {
66 ProtobufFactory::Instance()->GetDescriptorString(type, desc_str);
67 }
68
69 bool SerializeToArray(void *data, int size) const {
70 if (data == nullptr || size < ByteSize()) {
71 return false;
72 }
73
74 memcpy(data, message.data(), message.size());
75 return true;
76 }
77
78 bool SerializeToString(std::string *str) const {
79 if (str == nullptr) {
80 return false;
81 }
82 *str = message;
83 return true;
84 }
85
86 // bool SerializeToArenaMessageWrapper(ArenaMessageWrapper *wrapper) const {
87 // return true;
88 // }
89
90 bool ParseFromArray(const void *data, int size) {
91 if (data == nullptr || size <= 0) {
92 return false;
93 }
94
95 message.assign(reinterpret_cast<const char *>(data), size);
96 return true;
97 }
98
99 bool ParseFromString(const std::string &str) {
100 message = str;
101 return true;
102 }
103
104 // bool ParseFromArenaMessageWrapper(const ArenaMessageWrapper &wrapper) {
105 // return true;
106 // }
107
108 int ByteSize() const { return static_cast<int>(message.size()); }
109
110 static std::string TypeName() { return "apollo.cyber.message.RawMessage"; }
111
112 std::string message;
113 uint64_t timestamp;
114};
115
116} // namespace message
117} // namespace cyber
118} // namespace apollo
119
120#endif // CYBER_MESSAGE_RAW_MESSAGE_H_
class register implement
Definition arena_queue.h:37
bool ParseFromString(const std::string &str)
Definition raw_message.h:99
RawMessage(const RawMessage &raw_msg)
Definition raw_message.h:40
bool SerializeToArray(void *data, int size) const
Definition raw_message.h:69
RawMessage(const std::string &data)
Definition raw_message.h:35
static void GetDescriptorString(const std::string &type, std::string *desc_str)
Definition raw_message.h:64
RawMessage(const std::string &data, uint64_t ts)
Definition raw_message.h:37
bool ParseFromArray(const void *data, int size)
Definition raw_message.h:90
bool SerializeToString(std::string *str) const
Definition raw_message.h:78
RawMessage & operator=(const RawMessage &raw_msg)
Definition raw_message.h:43
static const Descriptor * descriptor()
Definition raw_message.h:59