mirror of
https://gitlab.com/libeigen/eigen.git
synced 2026-04-10 11:34:33 +08:00
Apply clang-format
This commit is contained in:
@@ -20,29 +20,24 @@ namespace Eigen {
|
||||
|
||||
/**
|
||||
* Serializes an object to a memory buffer.
|
||||
*
|
||||
*
|
||||
* Useful for transferring data (e.g. back-and-forth to a device).
|
||||
*/
|
||||
template<typename T, typename EnableIf = void>
|
||||
template <typename T, typename EnableIf = void>
|
||||
class Serializer;
|
||||
|
||||
// Specialization for POD types.
|
||||
template<typename T>
|
||||
class Serializer<T, typename std::enable_if_t<
|
||||
std::is_trivial<T>::value
|
||||
&& std::is_standard_layout<T>::value>> {
|
||||
template <typename T>
|
||||
class Serializer<T, typename std::enable_if_t<std::is_trivial<T>::value && std::is_standard_layout<T>::value>> {
|
||||
public:
|
||||
|
||||
/**
|
||||
* Determines the required size of the serialization buffer for a value.
|
||||
*
|
||||
*
|
||||
* \param value the value to serialize.
|
||||
* \return the required size.
|
||||
*/
|
||||
EIGEN_DEVICE_FUNC size_t size(const T& value) const {
|
||||
return sizeof(value);
|
||||
}
|
||||
|
||||
EIGEN_DEVICE_FUNC size_t size(const T& value) const { return sizeof(value); }
|
||||
|
||||
/**
|
||||
* Serializes a value to a byte buffer.
|
||||
* \param dest the destination buffer; if this is nullptr, does nothing.
|
||||
@@ -57,7 +52,7 @@ class Serializer<T, typename std::enable_if_t<
|
||||
memcpy(dest, &value, sizeof(value));
|
||||
return dest + sizeof(value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes a value from a byte buffer.
|
||||
* \param src the source buffer; if this is nullptr, does nothing.
|
||||
@@ -76,20 +71,18 @@ class Serializer<T, typename std::enable_if_t<
|
||||
|
||||
// Specialization for DenseBase.
|
||||
// Serializes [rows, cols, data...].
|
||||
template<typename Derived>
|
||||
template <typename Derived>
|
||||
class Serializer<DenseBase<Derived>, void> {
|
||||
public:
|
||||
typedef typename Derived::Scalar Scalar;
|
||||
|
||||
|
||||
struct Header {
|
||||
typename Derived::Index rows;
|
||||
typename Derived::Index cols;
|
||||
};
|
||||
|
||||
EIGEN_DEVICE_FUNC size_t size(const Derived& value) const {
|
||||
return sizeof(Header) + sizeof(Scalar) * value.size();
|
||||
}
|
||||
|
||||
|
||||
EIGEN_DEVICE_FUNC size_t size(const Derived& value) const { return sizeof(Header) + sizeof(Scalar) * value.size(); }
|
||||
|
||||
EIGEN_DEVICE_FUNC uint8_t* serialize(uint8_t* dest, uint8_t* end, const Derived& value) {
|
||||
if (EIGEN_PREDICT_FALSE(dest == nullptr)) return nullptr;
|
||||
if (EIGEN_PREDICT_FALSE(dest + size(value) > end)) return nullptr;
|
||||
@@ -102,7 +95,7 @@ class Serializer<DenseBase<Derived>, void> {
|
||||
memcpy(dest, value.data(), data_bytes);
|
||||
return dest + data_bytes;
|
||||
}
|
||||
|
||||
|
||||
EIGEN_DEVICE_FUNC const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, Derived& value) const {
|
||||
if (EIGEN_PREDICT_FALSE(src == nullptr)) return nullptr;
|
||||
if (EIGEN_PREDICT_FALSE(src + sizeof(Header) > end)) return nullptr;
|
||||
@@ -119,102 +112,97 @@ class Serializer<DenseBase<Derived>, void> {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
|
||||
class Serializer<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > : public
|
||||
Serializer<DenseBase<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > > {};
|
||||
|
||||
template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
|
||||
class Serializer<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > : public
|
||||
Serializer<DenseBase<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> > > {};
|
||||
|
||||
template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
|
||||
class Serializer<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
|
||||
: public Serializer<DenseBase<Matrix<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>> {};
|
||||
|
||||
template <typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
|
||||
class Serializer<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>
|
||||
: public Serializer<DenseBase<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols>>> {};
|
||||
|
||||
namespace internal {
|
||||
|
||||
|
||||
// Recursive serialization implementation helper.
|
||||
template<size_t N, typename... Types>
|
||||
template <size_t N, typename... Types>
|
||||
struct serialize_impl;
|
||||
|
||||
template<size_t N, typename T1, typename... Ts>
|
||||
template <size_t N, typename T1, typename... Ts>
|
||||
struct serialize_impl<N, T1, Ts...> {
|
||||
using Serializer = Eigen::Serializer<typename std::decay<T1>::type>;
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
size_t serialize_size(const T1& value, const Ts&... args) {
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size(const T1& value, const Ts&... args) {
|
||||
Serializer serializer;
|
||||
size_t size = serializer.size(value);
|
||||
return size + serialize_impl<N-1, Ts...>::serialize_size(args...);
|
||||
return size + serialize_impl<N - 1, Ts...>::serialize_size(args...);
|
||||
}
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
uint8_t* serialize(uint8_t* dest, uint8_t* end, const T1& value, const Ts&... args) {
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* end, const T1& value,
|
||||
const Ts&... args) {
|
||||
Serializer serializer;
|
||||
dest = serializer.serialize(dest, end, value);
|
||||
return serialize_impl<N-1, Ts...>::serialize(dest, end, args...);
|
||||
return serialize_impl<N - 1, Ts...>::serialize(dest, end, args...);
|
||||
}
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, T1& value, Ts&... args) {
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* end,
|
||||
T1& value, Ts&... args) {
|
||||
Serializer serializer;
|
||||
src = serializer.deserialize(src, end, value);
|
||||
return serialize_impl<N-1, Ts...>::deserialize(src, end, args...);
|
||||
return serialize_impl<N - 1, Ts...>::deserialize(src, end, args...);
|
||||
}
|
||||
};
|
||||
|
||||
// Base case.
|
||||
template<>
|
||||
template <>
|
||||
struct serialize_impl<0> {
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
size_t serialize_size() { return 0; }
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
uint8_t* serialize(uint8_t* dest, uint8_t* /*end*/) { return dest; }
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const uint8_t* deserialize(const uint8_t* src, const uint8_t* /*end*/) { return src; }
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size() { return 0; }
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* /*end*/) { return dest; }
|
||||
|
||||
static EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* /*end*/) {
|
||||
return src;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
|
||||
/**
|
||||
* Determine the buffer size required to serialize a set of values.
|
||||
*
|
||||
*
|
||||
* \param args ... arguments to serialize in sequence.
|
||||
* \return the total size of the required buffer.
|
||||
*/
|
||||
template<typename... Args>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
size_t serialize_size(const Args&... args) {
|
||||
template <typename... Args>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE size_t serialize_size(const Args&... args) {
|
||||
return internal::serialize_impl<sizeof...(args), Args...>::serialize_size(args...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialize a set of values to the byte buffer.
|
||||
*
|
||||
*
|
||||
* \param dest output byte buffer; if this is nullptr, does nothing.
|
||||
* \param end the end of the output byte buffer.
|
||||
* \param args ... arguments to serialize in sequence.
|
||||
* \return the next address after all serialized values.
|
||||
*/
|
||||
template<typename... Args>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
uint8_t* serialize(uint8_t* dest, uint8_t* end, const Args&... args) {
|
||||
template <typename... Args>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE uint8_t* serialize(uint8_t* dest, uint8_t* end, const Args&... args) {
|
||||
return internal::serialize_impl<sizeof...(args), Args...>::serialize(dest, end, args...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deserialize a set of values from the byte buffer.
|
||||
*
|
||||
*
|
||||
* \param src input byte buffer; if this is nullptr, does nothing.
|
||||
* \param end the end of input byte buffer.
|
||||
* \param args ... arguments to deserialize in sequence.
|
||||
* \return the next address after all parsed values; nullptr if parsing errors are detected.
|
||||
*/
|
||||
template<typename... Args>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
|
||||
const uint8_t* deserialize(const uint8_t* src, const uint8_t* end, Args&... args) {
|
||||
template <typename... Args>
|
||||
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const uint8_t* deserialize(const uint8_t* src, const uint8_t* end,
|
||||
Args&... args) {
|
||||
return internal::serialize_impl<sizeof...(args), Args...>::deserialize(src, end, args...);
|
||||
}
|
||||
|
||||
} // namespace Eigen
|
||||
|
||||
#endif // EIGEN_SERIALIZER_H
|
||||
#endif // EIGEN_SERIALIZER_H
|
||||
|
||||
Reference in New Issue
Block a user