Loading...
Searching...
No Matches
Serialization-Impl.hpp
Go to the documentation of this file.
1// This code is based on Jet framework.
2// Copyright (c) 2018 Doyub Kim
3// CubbyFlow is voxel-based fluid simulation engine for computer games.
4// Copyright (c) 2020 CubbyFlow Team
5// Core Part: Chris Ohk, Junwoo Hwang, Jihong Sin, Seungwoo Yoo
6// AI Part: Dongheon Cho, Minseo Kim
7// We are making my contributions/submissions to this project solely in our
8// personal capacity and are not conveying any rights to any intellectual
9// property of any third parties.
10
11#ifndef CUBBYFLOW_SERIALIZATION_IMPL_HPP
12#define CUBBYFLOW_SERIALIZATION_IMPL_HPP
13
14#include <cstring>
15#include <stdexcept>
16#include <type_traits>
17
18namespace CubbyFlow
19{
20template <typename T>
21void Serialize(const ConstArrayView1<T>& array, std::vector<uint8_t>* buffer)
22{
23 static_assert(std::is_trivially_copyable_v<T>);
24
25 const size_t size = sizeof(T) * array.Length();
26 Serialize(reinterpret_cast<const uint8_t*>(array.data()), size, buffer);
27}
28
29template <typename T>
30void Deserialize(const std::vector<uint8_t>& buffer, Array1<T>* array)
31{
32 static_assert(std::is_trivially_copyable_v<T>);
33
34 std::vector<uint8_t> data;
35 Deserialize(buffer, &data);
36
37 if (data.size() % sizeof(T) != 0)
38 {
39 throw std::invalid_argument{
40 "Serialized data size must be a multiple of the element size."
41 };
42 }
43
44 array->Resize(data.size() / sizeof(T));
45 std::memcpy(array->data(), data.data(), data.size());
46}
47} // namespace CubbyFlow
48
49#endif
ValueType Length() const
Definition MatrixExpression-Impl.hpp:278
Definition Matrix.hpp:30
Pointer data()
Definition Matrix-Impl.hpp:298
Definition pybind11Utils.hpp:22
void Deserialize(const std::vector< uint8_t > &buffer, Array1< T > *array)
Deserializes buffer to data chunk using common schema.
Definition Serialization-Impl.hpp:30
Matrix< T, Rows, 1 > Vector
Definition Matrix.hpp:719
void Serialize(const ConstArrayView1< T > &array, std::vector< uint8_t > *buffer)
Serializes data chunk using common schema.
Definition Serialization-Impl.hpp:21