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 #include <cstdint>
10 #include <array>
11 #include <ifm3d/device/err.h>
12 #include <ifm3d/fg/buffer.h>
13 #include <ifm3d/fg/organizer_utils.h>
14 
15 namespace ifm3d
16 {
17 
18  template <typename T, std::size_t n>
19  void
20  mkarray(const std::uint8_t* data, std::array<T, n>& arr)
21  {
22  int element_index = 0;
23  for (auto& val : arr)
24  {
25  val = mkval<T>(data + sizeof(T) * element_index);
26  element_index++;
27  }
28  }
29 
30  template <typename T, std::size_t num_of_parameter>
32  {
33  public:
34  void
35  Read(const std::uint8_t* data, std::size_t size)
36  {
37  if (size < (num_of_parameter * sizeof(T)))
38  {
39  throw ifm3d::Error(IFM3D_CORRUPTED_STRUCT);
40  }
41  const std::uint8_t* start_ptr = data;
42  mkarray<T, num_of_parameter>(start_ptr, this->data);
43  };
44 
48  std::array<T, num_of_parameter> data;
49 
50  static auto
51  Deserialize(const Buffer& o3d_buffer)
52  {
54 
55  data.Read(o3d_buffer.ptr<uint8_t>(0), o3d_buffer.size());
56  return data;
57  }
58  };
59 
60 } // end namespace
61 
62 #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:99
ifm3d::ArrayDeserialize::data
std::array< T, num_of_parameter > data
array to hold deserialize values
Definition: deserialize_utils.hpp:43
ifm3d::Buffer::ptr
T * ptr(const std::uint32_t row)
returns a pointer to the specified Buffer row.
ifm3d::Error
Definition: err.h:114
ifm3d::ArrayDeserialize
Definition: deserialize_utils.hpp:31