@@ -32,6 +32,10 @@ namespace glz
32
32
static void op (auto & value, is_context auto && ctx, auto && it, auto && end)
33
33
{
34
34
++it;
35
+ if (it >= end) [[unlikely]] {
36
+ ctx.error = error_code::unexpected_end;
37
+ return ;
38
+ }
35
39
constexpr uint8_t layout = uint8_t (!T::IsRowMajor);
36
40
if (uint8_t (*it) != layout) {
37
41
ctx.error = error_code::syntax_error;
@@ -45,22 +49,29 @@ namespace glz
45
49
}
46
50
};
47
51
52
+ // A dynamic matrix in both rows and columns
48
53
template <matrix_t T>
49
- requires (T::RowsAtCompileTime < 0 || T::ColsAtCompileTime < 0 )
54
+ requires (T::RowsAtCompileTime < 0 && T::ColsAtCompileTime < 0 )
50
55
struct from <BEVE, T>
51
56
{
52
57
template <auto Opts>
53
58
static void op (auto & value, is_context auto && ctx, auto && it, auto && end)
54
59
{
55
60
++it;
61
+ if (it >= end) [[unlikely]] {
62
+ ctx.error = error_code::unexpected_end;
63
+ return ;
64
+ }
56
65
constexpr uint8_t layout = uint8_t (!T::IsRowMajor);
57
66
if (uint8_t (*it) != layout) {
58
67
ctx.error = error_code::syntax_error;
68
+ return ;
59
69
}
60
70
++it;
61
71
std::array<Eigen::Index, 2 > extents;
62
72
detail::read <BEVE>::op<Opts>(extents, ctx, it, end);
63
73
74
+ value.resize (extents[0 ], extents[1 ]);
64
75
std::span<typename T::Scalar> view (value.data (), extents[0 ] * extents[1 ]);
65
76
detail::read <BEVE>::op<Opts>(view, ctx, it, end);
66
77
}
@@ -88,8 +99,9 @@ namespace glz
88
99
}
89
100
};
90
101
102
+ // A dynamic matrix in both rows and columns
91
103
template <matrix_t T>
92
- requires (T::RowsAtCompileTime < 0 || T::ColsAtCompileTime < 0 )
104
+ requires (T::RowsAtCompileTime < 0 && T::ColsAtCompileTime < 0 )
93
105
struct to <BEVE, T>
94
106
{
95
107
template <auto Opts>
@@ -134,6 +146,55 @@ namespace glz
134
146
detail::to<JSON, Value>::template op<Opts>(view, ctx, b, ix);
135
147
}
136
148
};
149
+
150
+ // A dynamic matrix in both rows and columns
151
+ template <matrix_t T>
152
+ requires (T::RowsAtCompileTime < 0 && T::ColsAtCompileTime < 0 )
153
+ struct to <JSON, T>
154
+ {
155
+ template <auto Opts>
156
+ static void op (auto && value, is_context auto && ctx, auto && b, auto && ix)
157
+ {
158
+ dump<' [' >(b, ix);
159
+ using RowColT = std::array<Eigen::Index, 2 >;
160
+ RowColT extents{value.rows (), value.cols ()};
161
+ detail::to<JSON, RowColT>::template op<Opts>(extents, ctx, b, ix);
162
+ dump<' ,' >(b, ix);
163
+
164
+ std::span<typename T::Scalar> view (value.data (), value.size ());
165
+ using Value = std::remove_cvref_t <decltype (view)>;
166
+ detail::to<JSON, Value>::template op<Opts>(view, ctx, b, ix);
167
+ dump<' ]' >(b, ix);
168
+ }
169
+ };
170
+
171
+ // A dynamic matrix in both rows and columns
172
+ template <matrix_t T>
173
+ requires (T::RowsAtCompileTime < 0 && T::ColsAtCompileTime < 0 )
174
+ struct from <JSON, T>
175
+ {
176
+ template <auto Opts>
177
+ static void op (auto & value, is_context auto && ctx, auto && it, auto && end)
178
+ {
179
+ GLZ_MATCH_OPEN_BRACKET;
180
+ std::array<Eigen::Index, 2 > extents; // NOLINT
181
+ detail::read <JSON>::op<Opts>(extents, ctx, it, end);
182
+ value.resize (extents[0 ], extents[1 ]);
183
+ if (*it == ' ,' ) {
184
+ // we have data
185
+ ++it;
186
+ if constexpr (not Opts.null_terminated ) {
187
+ if (it == end) [[unlikely]] {
188
+ ctx.error = error_code::unexpected_end;
189
+ return ;
190
+ }
191
+ }
192
+ std::span<typename T::Scalar> view (value.data (), extents[0 ] * extents[1 ]);
193
+ detail::read <JSON>::op<Opts>(view, ctx, it, end);
194
+ }
195
+ GLZ_MATCH_CLOSE_BRACKET;
196
+ }
197
+ };
137
198
} // namespace detail
138
199
} // namespace glaze
139
200
0 commit comments