ifm3d
deserialize_utils.hpp
1 /*
2  * Copyright 2023-present ifm electronic, gmbh
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef IFM3D_DESERIALIZE_UTILS_HPP
7 #define IFM3D_DESERIALIZE_UTILS_HPP
8 
9 namespace ifm3d
10 {
11 
12  template <typename T, size_t n>
13  void
14  mkarray(const uint8_t* data, std::array<T, n>& arr)
15  {
16  int element_index = 0;
17  for (auto& val : arr)
18  {
19  val = mkval<T>(data + sizeof(T) * element_index);
20  element_index++;
21  }
22  }
23 
24  template <typename T, size_t num_of_parameter>
26  {
27  public:
28  void
29  Read(const uint8_t* data, size_t size)
30  {
31  if (size < (num_of_parameter * sizeof(T)))
32  {
33  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
34  }
35  const uint8_t* start_ptr = data;
36  mkarray<T, num_of_parameter>(start_ptr, this->data);
37  };
38 
42  std::array<T, num_of_parameter> data;
43 
44  static auto
45  Deserialize(const Buffer& o3d_buffer)
46  {
48 
49  data.Read(o3d_buffer.ptr<uint8_t>(0), o3d_buffer.size());
50  return data;
51  }
52  };
53 
54 } // end namespace
55 
56 #endif // IFM3D_DESERIALIZE_UTILS_HPP
ifm3d::Buffer::size
size_t size() const
Return the size of the buffer in bytes.
ifm3d::Buffer
The class Buffer represent a STL container to store data from the ifm devices in 2 dimension and supp...
Definition: buffer.h:98
ifm3d::ArrayDeserialize::data
std::array< T, num_of_parameter > data
array to hold deserialize values
Definition: deserialize_utils.hpp:37
ifm3d::Buffer::ptr
T * ptr(const std::uint32_t row)
returns a pointer to the specified Buffer row.
ifm3d::Error
Definition: err.h:112
ifm3d::ArrayDeserialize
Definition: deserialize_utils.hpp:25