ByteOrderConversion.cpp
Go to the documentation of this file.
1 //
3 // © Copyright 2022 SCHUNK Mobile Greifsysteme GmbH, Lauffen/Neckar Germany
4 // © Copyright 2022 FZI Forschungszentrum Informatik, Karlsruhe, Germany
5 //
6 // This file is part of the Schunk SVH Library.
7 //
8 // The Schunk SVH Library is free software: you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or (at your
11 // option) any later version.
12 //
13 // The Schunk SVH Library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16 // Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License along with
19 // the Schunk SVH Library. If not, see <https://www.gnu.org/licenses/>.
20 //
22 
23 //----------------------------------------------------------------------
31 //----------------------------------------------------------------------
32 
34 
35 
36 namespace driver_svh {
37 
38 std::ostream& operator<<(std::ostream& o, const ArrayBuilder& ab)
39 {
40  for (size_t i = 0; i < ab.array.size(); i++)
41  {
42  o << "0x" << std::setw(2) << std::setfill('0') << std::hex << static_cast<int>(ab.array[i])
43  << " ";
44  }
45  // Reset Output stream to decimal output .. otherwise it may confuse people
46  std::cout << std::dec;
47  return o;
48 }
49 
50 template <>
51 size_t toLittleEndian<float>(const float& data, std::vector<uint8_t>& array, size_t& write_pos)
52 {
55  return toLittleEndian(*(reinterpret_cast<const uint32_t*>(&data)), array, write_pos);
56 }
57 
58 template <>
59 size_t toLittleEndian<double>(const double& data, std::vector<uint8_t>& array, size_t& write_pos)
60 {
63  return toLittleEndian(*(reinterpret_cast<const uint64_t*>(&data)), array, write_pos);
64 }
65 
66 template <>
67 size_t fromLittleEndian<float>(float& data, std::vector<uint8_t>& array, size_t& read_pos)
68 {
71  return fromLittleEndian(*(reinterpret_cast<uint32_t*>(&data)), array, read_pos);
72 }
73 
74 template <>
75 size_t fromLittleEndian<double>(double& data, std::vector<uint8_t>& array, size_t& read_pos)
76 {
79  return fromLittleEndian(*(reinterpret_cast<uint64_t*>(&data)), array, read_pos);
80 }
81 
82 void ArrayBuilder::reset(size_t array_size)
83 {
84  array.clear();
85  write_pos = 0;
86  read_pos = 0;
87  array.resize(array_size, 0);
88 }
89 
90 
91 } // namespace driver_svh
DRIVER_SVH_IMPORT_EXPORT size_t fromLittleEndian< double >(double &data, std::vector< uint8_t > &array, size_t &read_pos)
Template specialization for float as these have to be handled seperately.
DRIVER_SVH_IMPORT_EXPORT size_t toLittleEndian< float >(const float &data, std::vector< uint8_t > &array, size_t &write_pos)
Template specialization for float as these have to be handled seperately.
size_t write_pos
current write position in array
std::vector< uint8_t > array
array of template type TArray
driver_svh::ArrayBuilder & operator<<(driver_svh::ArrayBuilder &ab, const SVHControlCommand &data)
DRIVER_SVH_IMPORT_EXPORT size_t toLittleEndian< double >(const double &data, std::vector< uint8_t > &array, size_t &write_pos)
Template specialization for float as these have to be handled seperately.
DRIVER_SVH_IMPORT_EXPORT size_t fromLittleEndian< float >(float &data, std::vector< uint8_t > &array, size_t &read_pos)
Template specialization for float as these have to be handled seperately.
size_t fromLittleEndian(T &data, std::vector< uint8_t > &array, size_t &read_pos)
size_t read_pos
current read position in array
void reset(size_t array_size=1)
Resets the Arraybuilder to initial state, all values will be deleted.
size_t toLittleEndian(const T &data, std::vector< uint8_t > &array, size_t &write_pos)
template function for adding data to an array while converting everything into correct endianess ...


schunk_svh_library
Author(s): Georg Heppner, Lars Pfotzer, Felix Exner, Johannes Mangler, Stefan Scherzinger, Pascal Becker
autogenerated on Fri Apr 14 2023 02:26:23