Autoware.Auto
byte_reader.hpp
Go to the documentation of this file.
1 // Copyright 2017-2019 the Autoware Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Co-developed by Tier IV, Inc. and Apex.AI, Inc.
18 
19 #ifndef HELPER_FUNCTIONS__BYTE_READER_HPP_
20 #define HELPER_FUNCTIONS__BYTE_READER_HPP_
21 
22 #include <cstdint>
23 #include <cstring>
24 #include <vector>
25 
26 namespace autoware
27 {
28 namespace common
29 {
30 namespace helper_functions
31 {
34 {
35 private:
36  const std::vector<uint8_t> & byte_vector_;
37  std::size_t index_;
38 
39 public:
42  explicit ByteReader(const std::vector<uint8_t> & byte_vector)
43  : byte_vector_(byte_vector),
44  index_(0U)
45  {
46  }
47 
48  // brief Read bytes and store it in the argument passed in big-endian order
50  template<typename T>
51  void read(T & value)
52  {
53  constexpr std::size_t kTypeSize = sizeof(T);
54  union {
55  T value;
56  uint8_t byte_vector[kTypeSize];
57  } tmp;
58 
59  for (std::size_t i = 0; i < kTypeSize; ++i) {
60  tmp.byte_vector[i] = byte_vector_[index_ + kTypeSize - 1 - i];
61  }
62 
63  value = tmp.value;
64 
65  index_ += kTypeSize;
66  }
67 
68  void skip(std::size_t count)
69  {
70  index_ += count;
71  }
72 };
73 } // namespace helper_functions
74 } // namespace common
75 } // namespace autoware
76 
77 #endif // HELPER_FUNCTIONS__BYTE_READER_HPP_
autoware::common::helper_functions::ByteReader
A utility class to read byte vectors in big-endian order.
Definition: byte_reader.hpp:33
autoware::common::helper_functions::ByteReader::read
void read(T &value)
Definition: byte_reader.hpp:51
autoware::common::helper_functions::ByteReader::ByteReader
ByteReader(const std::vector< uint8_t > &byte_vector)
Default constructor, byte reader class.
Definition: byte_reader.hpp:42
autoware::common::helper_functions::ByteReader::skip
void skip(std::size_t count)
Definition: byte_reader.hpp:68
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24