Skip to content

Commit bad95bc

Browse files
committed
serialization/eigen: enhance genericity
1 parent 19ebc94 commit bad95bc

File tree

1 file changed

+14
-32
lines changed

1 file changed

+14
-32
lines changed

include/proxsuite/serialization/eigen.hpp

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,65 +16,47 @@
1616
namespace cereal {
1717

1818
// dense matrices
19-
template<class Archive,
20-
class _Scalar,
21-
int _Rows,
22-
int _Cols,
23-
int _Options,
24-
int _MaxRows,
25-
int _MaxCols>
19+
template<class Archive, class Derived>
2620
inline void
27-
save(
28-
Archive& ar,
29-
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> const& m)
21+
save(Archive& ar, Eigen::PlainObjectBase<Derived> const& m)
3022
{
23+
typedef Eigen::PlainObjectBase<Derived> PlainType;
24+
3125
Eigen::Index rows = m.rows();
3226
Eigen::Index cols = m.cols();
3327
ar(CEREAL_NVP(rows));
3428
ar(CEREAL_NVP(cols));
35-
int storage_order;
36-
if (m.IsRowMajor) {
37-
storage_order = 1;
38-
} else {
39-
storage_order = 0;
40-
}
41-
ar(CEREAL_NVP(storage_order));
29+
bool is_row_major = PlainType::IsRowMajor;
30+
ar(CEREAL_NVP(is_row_major));
4231

4332
for (Eigen::Index i = 0; i < m.size(); i++)
4433
ar(m.data()[i]);
4534
}
4635

47-
template<class Archive,
48-
class _Scalar,
49-
int _Rows,
50-
int _Cols,
51-
int _Options,
52-
int _MaxRows,
53-
int _MaxCols>
36+
template<class Archive, class Derived>
5437
inline void
55-
load(Archive& ar,
56-
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& m)
38+
load(Archive& ar, Eigen::PlainObjectBase<Derived>& m)
5739
{
40+
typedef Eigen::PlainObjectBase<Derived> PlainType;
41+
5842
Eigen::Index rows;
5943
Eigen::Index cols;
60-
int storage_order;
44+
bool is_row_major;
6145
ar(CEREAL_NVP(rows));
6246
ar(CEREAL_NVP(cols));
63-
ar(CEREAL_NVP(storage_order));
47+
ar(CEREAL_NVP(is_row_major));
6448

6549
m.resize(rows, cols);
6650

6751
for (Eigen::Index i = 0; i < m.size(); i++)
6852
ar(m.data()[i]);
6953

7054
// Account for different storage orders
71-
if (!(storage_order == m.IsRowMajor)) {
55+
if (is_row_major != PlainType::IsRowMajor) {
7256
#if EIGEN_VERSION_AT_LEAST(3, 4, 0)
7357
m.transposeInPlace();
7458
#else
75-
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> m_t(
76-
m.transpose());
77-
m = m_t;
59+
m = m.transpose().eval();
7860
#endif
7961
}
8062
}

0 commit comments

Comments
 (0)