Branch data Line data Source code
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. 16 : : /// \file 17 : : /// \brief This file includes common helper functions 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 : : { 32 : : /// \brief A utility class to read byte vectors in big-endian order 33 : : class ByteReader 34 : : { 35 : : private: 36 : : const std::vector<uint8_t> & byte_vector_; 37 : : std::size_t index_; 38 : : 39 : : public: 40 : : /// \brief Default constructor, byte reader class 41 : : /// \param[in] byte_vector A vector to read bytes from 42 : 1 : explicit ByteReader(const std::vector<uint8_t> & byte_vector) 43 : 1 : : byte_vector_(byte_vector), 44 : 1 : index_(0U) 45 : : { 46 : 1 : } 47 : : 48 : : // brief Read bytes and store it in the argument passed in big-endian order 49 : : /// \param[inout] value Read and store the bytes from the vector matching the size of the argument 50 : : template<typename T> 51 : 3 : void read(T & value) 52 : : { 53 : 3 : constexpr std::size_t kTypeSize = sizeof(T); 54 : : union { 55 : : T value; 56 : : uint8_t byte_vector[kTypeSize]; 57 : : } tmp; 58 : : 59 [ + + ]: 17 : for (std::size_t i = 0; i < kTypeSize; ++i) { 60 : 14 : tmp.byte_vector[i] = byte_vector_[index_ + kTypeSize - 1 - i]; 61 : : } 62 : : 63 : 3 : value = tmp.value; 64 : : 65 : 3 : index_ += kTypeSize; 66 : 3 : } 67 : : 68 : 1 : void skip(std::size_t count) 69 : : { 70 : 1 : index_ += count; 71 : 1 : } 72 : : }; 73 : : } // namespace helper_functions 74 : : } // namespace common 75 : : } // namespace autoware 76 : : 77 : : #endif // HELPER_FUNCTIONS__BYTE_READER_HPP_