Eigen  3.4.90 (git rev 5a9f66fb35d03a4da9ef8976e67a61b30aa16dcf)
 
Loading...
Searching...
No Matches
Serializer.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2021 The Eigen Team
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_SERIALIZER_H
11#define EIGEN_SERIALIZER_H
12
13#include <type_traits>
14
15// The Serializer class encodes data into a memory buffer so it can be later
16// reconstructed. This is mainly used to send objects back-and-forth between
17// the CPU and GPU.
18
19namespace Eigen {
20
26template <typename T, typename EnableIf = void>
28
29// Specialization for POD types.
30template <typename T>
31class Serializer<T, typename std::enable_if_t<std::is_trivial<T>::value && std::is_standard_layout<T>::value>> {
32 public:
39 EIGEN_DEVICE_FUNC size_t size(const T& value) const { return sizeof(value); }
40
48 EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end, const T& value) {
49 if (EIGEN_PREDICT_FALSE(dest == nullptr)) return nullptr;
50 if (EIGEN_PREDICT_FALSE(dest + sizeof(value) > end)) return nullptr;
51 EIGEN_USING_STD(memcpy)
52 memcpy(dest, &value, sizeof(value));
53 return dest + sizeof(value);
54 }
55
63 EIGEN_DEVICE_FUNC const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, T& value) const {
64 if (EIGEN_PREDICT_FALSE(src == nullptr)) return nullptr;
65 if (EIGEN_PREDICT_FALSE(src + sizeof(value) > end)) return nullptr;
66 EIGEN_USING_STD(memcpy)
67 memcpy(&value, src, sizeof(value));
68 return src + sizeof(value);
69 }
70};
71
72// Specialization for DenseBase.
73// Serializes [rows, cols, data...].
74template <typename Derived>
75class Serializer<DenseBase<Derived>, void> {
76 public:
77 typedef typename Derived::Scalar Scalar;
78
79 struct Header {
80 typename Derived::Index rows;
81 typename Derived::Index cols;
82 };
83
84 EIGEN_DEVICE_FUNC size_t size(const Derived& value) const { return sizeof(Header) + sizeof(Scalar) * value.size(); }
85
86 EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end, const Derived& value) {
87 if (EIGEN_PREDICT_FALSE(dest == nullptr)) return nullptr;
88 if (EIGEN_PREDICT_FALSE(dest + size(value) > end)) return nullptr;
89 const size_t header_bytes = sizeof(Header);
90 const size_t data_bytes = sizeof(Scalar) * value.size();
91 Header header = {value.rows(), value.cols()};
92 EIGEN_USING_STD(memcpy)
93 memcpy(dest, &header, header_bytes);
94 dest += header_bytes;
95 memcpy(dest, value.data(), data_bytes);
96 return dest + data_bytes;
97 }
98
99 EIGEN_DEVICE_FUNC const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, Derived& value) const {
100 if (EIGEN_PREDICT_FALSE(src == nullptr)) return nullptr;
101 if (EIGEN_PREDICT_FALSE(src + sizeof(Header) > end)) return nullptr;
102 const size_t header_bytes = sizeof(Header);
103 Header header;
104 EIGEN_USING_STD(memcpy)
105 memcpy(&header, src, header_bytes);
106 src += header_bytes;
107 const size_t data_bytes = sizeof(Scalar) * header.rows * header.cols;
108 if (EIGEN_PREDICT_FALSE(src + data_bytes > end)) return nullptr;
109 value.resize(header.rows, header.cols);
110 memcpy(value.data(), src, data_bytes);
111 return src + data_bytes;
112 }
113};
114
115template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
116class Serializer<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
117 : public Serializer<DenseBase<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>> {};
118
119template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
120class Serializer<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
121 : public Serializer<DenseBase<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>> {};
122
123namespace internal {
124
125// Recursive serialization implementation helper.
126template <size_t N, typename... Types>
127struct serialize_impl;
128
129template <size_t N, typename T1, typename... Ts>
130struct serialize_impl<N, T1, Ts...> {
132
133 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size(const T1& value, const Ts&... args) {
134 Serializer serializer;
135 size_t size = serializer.size(value);
136 return size + serialize_impl<N - 1, Ts...>::serialize_size(args...);
137 }
138
139 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* end, const T1& value,
140 const Ts&... args) {
141 Serializer serializer;
142 dest = serializer.serialize(dest, end, value);
143 return serialize_impl<N - 1, Ts...>::serialize(dest, end, args...);
144 }
145
146 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* end,
147 T1& value, Ts&... args) {
148 Serializer serializer;
149 src = serializer.deserialize(src, end, value);
150 return serialize_impl<N - 1, Ts...>::deserialize(src, end, args...);
151 }
152};
153
154// Base case.
155template <>
156struct serialize_impl<0> {
157 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size() { return 0; }
158
159 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* /*end*/) { return dest; }
160
161 static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* /*end*/) {
162 return src;
163 }
164};
165
166} // namespace internal
167
174template <typename... Args>
175EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size(const Args&... args) {
176 return internal::serialize_impl<sizeof...(args), Args...>::serialize_size(args...);
177}
178
187template <typename... Args>
188EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* end, const Args&... args) {
189 return internal::serialize_impl<sizeof...(args), Args...>::serialize(dest, end, args...);
190}
191
200template <typename... Args>
201EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* end,
202 Args&... args) {
203 return internal::serialize_impl<sizeof...(args), Args...>::deserialize(src, end, args...);
204}
205
206} // namespace Eigen
207
208#endif // EIGEN_SERIALIZER_H
Definition Serializer.h:27
Namespace containing all symbols from the Eigen library.
Definition Core:137
uint8_t * serialize(uint8_t *dest, uint8_t *end, const Args &... args)
Definition Serializer.h:188
size_t serialize_size(const Args &... args)
Definition Serializer.h:175
const uint8_t * deserialize(const uint8_t *src, const uint8_t *end, Args &... args)
Definition Serializer.h:201