Apollo 10.0
自动驾驶开放平台
readable_info.cc
浏览该文件的文档.
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
18
19#include <cstring>
20
21#include "cyber/common/log.h"
22
23namespace apollo {
24namespace cyber {
25namespace transport {
26
27const size_t ReadableInfo::kSize = sizeof(uint64_t) * 2 + \
28 sizeof(int32_t) + sizeof(int32_t);
29
31 : host_id_(0),
32 block_index_(0),
33 arena_block_index_(-1),
34 channel_id_(0) {}
35
36ReadableInfo::ReadableInfo(uint64_t host_id, int32_t block_index,
37 uint64_t channel_id, int32_t arena_block_index)
38 : host_id_(host_id),
39 block_index_(block_index),
40 arena_block_index_(arena_block_index),
41 channel_id_(channel_id) {}
42
44
46 if (this != &other) {
47 this->host_id_ = other.host_id_;
48 this->block_index_ = other.block_index_;
49 this->channel_id_ = other.channel_id_;
50 this->arena_block_index_ = other.arena_block_index_;
51 }
52 return *this;
53}
54
55bool ReadableInfo::SerializeTo(std::string* dst) const {
56 RETURN_VAL_IF_NULL(dst, false);
57
58 dst->assign(reinterpret_cast<char*>(const_cast<uint64_t*>(&host_id_)),
59 sizeof(host_id_));
60 dst->append(reinterpret_cast<char*>(const_cast<int32_t*>(&block_index_)),
61 sizeof(block_index_));
62 dst->append(reinterpret_cast<char*>(const_cast<uint64_t*>(&channel_id_)),
63 sizeof(channel_id_));
64 dst->append(reinterpret_cast<char*>(const_cast<int32_t*>(
65 &arena_block_index_)), sizeof(arena_block_index_));
66 return true;
67}
68
69bool ReadableInfo::DeserializeFrom(const std::string& src) {
70 return DeserializeFrom(src.data(), src.size());
71}
72
73bool ReadableInfo::DeserializeFrom(const char* src, std::size_t len) {
74 RETURN_VAL_IF_NULL(src, false);
75 if (len != kSize) {
76 AWARN << "src size[" << len << "] mismatch.";
77 return false;
78 }
79
80 char* ptr = const_cast<char*>(src);
81 memcpy(reinterpret_cast<char*>(&host_id_), ptr, sizeof(host_id_));
82 ptr += sizeof(host_id_);
83 memcpy(reinterpret_cast<char*>(&block_index_), ptr, sizeof(block_index_));
84 ptr += sizeof(block_index_);
85 memcpy(reinterpret_cast<char*>(&channel_id_), ptr, sizeof(channel_id_));
86 ptr += sizeof(channel_id_);
87 memcpy(reinterpret_cast<char*>(&arena_block_index_),
88 ptr, sizeof(arena_block_index_));
89 return true;
90}
91
92} // namespace transport
93} // namespace cyber
94} // namespace apollo
bool SerializeTo(std::string *dst) const
ReadableInfo & operator=(const ReadableInfo &other)
bool DeserializeFrom(const std::string &src)
#define RETURN_VAL_IF_NULL(ptr, val)
Definition log.h:98
#define AWARN
Definition log.h:43
class register implement
Definition arena_queue.h:37