Lagrange
Array.serialization.h
1/*
2 * Copyright 2020 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#include <lagrange/utils/assert.h>
13#include <lagrange/utils/warning.h>
14#include <cassert>
15
17#if !defined(__clang__) && defined(__gnuc__)
18#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
19#endif
20
21namespace lagrange {
22namespace experimental {
23namespace internal {
24
25template <typename T>
27{
28 template <typename Archive>
29 static void serialize(ArrayBase* arr, Archive& ar)
30 {
31 if (arr->is_row_major()) {
32 using EigenType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
33 auto M = arr->template view<EigenType>();
34 ar& M;
35 } else {
36 using EigenType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>;
37 auto M = arr->template view<EigenType>();
38 ar& M;
39 }
40 }
41
42 template <int StorageOrder, typename Archive>
43 static std::unique_ptr<ArrayBase> deserialize(Archive& ar)
44 {
45 using EigenType = Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic, StorageOrder>;
46 EigenType matrix;
47 ar& matrix;
48 return std::make_unique<EigenArray<EigenType>>(std::move(matrix));
49 }
50};
51
52template <typename Archive>
53void serialize_array(ArrayBase* arr, Archive& ar)
54{
55 assert(!ar.is_input());
56 enum { SCALAR_TYPE = 0, IS_ROW_MAJOR = 1, DATA = 2 };
57
58 ar.object([&](auto& ar) {
59 auto stype = arr->get_scalar_type();
60 auto is_row_major = arr->is_row_major();
61 ar("scalar_type", SCALAR_TYPE) & stype;
62 ar("is_row_major", IS_ROW_MAJOR) & is_row_major;
63
64 auto data_ar = ar("data", DATA);
65
66 switch (stype) {
67#define LAGRANGE_SERIALIZATION_ADD_CASE(type) \
68 case type: \
69 ArraySerialization<EnumToScalar_t<type>>::serialize(arr, data_ar); \
70 break;
71 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT8)
72 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT16)
73 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT32)
74 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT64)
75 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT8)
76 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT16)
77 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT32)
78 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT64)
79 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::FLOAT)
80 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::DOUBLE)
81 default: throw std::runtime_error("Unsupported scalar type detected!");
82#undef LAGRANGE_SERIALIZATION_ADD_CASE
83 }
84 });
85}
86
87template <typename Archive>
88std::unique_ptr<ArrayBase> deserialize_array(Archive& ar)
89{
90 enum { SCALAR_TYPE = 0, IS_ROW_MAJOR = 1, DATA = 2 };
91 std::unique_ptr<ArrayBase> ptr;
92
93 ar.object([&](auto& ar) {
94 ScalarEnum stype;
95 bool is_row_major;
96 ar("scalar_type", SCALAR_TYPE) & stype;
97 ar("is_row_major", IS_ROW_MAJOR) & is_row_major;
98
99 auto data_ar = ar("data", DATA);
100
101 switch (stype) {
102#define LAGRANGE_SERIALIZATION_ADD_CASE(type) \
103 case type: \
104 ptr = \
105 is_row_major \
106 ? ArraySerialization<EnumToScalar_t<type>>::deserialize<Eigen::RowMajor>(data_ar) \
107 : ArraySerialization<EnumToScalar_t<type>>::deserialize<Eigen::ColMajor>(data_ar); \
108 break;
109 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT8)
110 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT16)
111 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT32)
112 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::INT64)
113 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT8)
114 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT16)
115 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT32)
116 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::UINT64)
117 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::FLOAT)
118 LAGRANGE_SERIALIZATION_ADD_CASE(ScalarEnum::DOUBLE)
119 default: throw std::runtime_error("Unsupported scalar type detected!");
120 }
121#undef LAGRANGE_SERIALIZATION_ADD_CASE
122 });
123 assert(ptr != nullptr);
124 return ptr;
125}
126
127} // namespace internal
128
136template <typename Archive>
137void serialize(std::shared_ptr<ArrayBase>& arr, Archive& ar)
138{
139 if (ar.is_input()) {
140 arr = internal::deserialize_array(ar);
141 } else {
142 internal::serialize_array(arr.get(), ar);
143 }
144}
145
153template <typename Archive>
154void serialize(std::unique_ptr<ArrayBase>& arr, Archive& ar)
155{
156 if (ar.is_input()) {
157 arr = internal::deserialize_array(ar);
158 } else {
159 internal::serialize_array(arr.get(), ar);
160 }
161}
162
163} // experimental
164} // namespace lagrange
165
166LA_IGNORE_SHADOW_WARNING_END
Definition: Array.h:35
#define LA_IGNORE_SHADOW_WARNING_BEGIN
Ignore shadow warnings.
Definition: warning.h:68
Main namespace for Lagrange.
Definition: AABBIGL.h:30
Definition: Array.serialization.h:27