Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
eigen_serialization_addon.hpp
Go to the documentation of this file.
1
2
3
4// the following code comes from
5// https://stackoverflow.com/questions/18382457/eigen-and-boostserialize
6// and adds support for serialization of Eigen types
7//
8// question asked by user Gabriel and answered by iNFINITEi
9//
10// answer code repeated here verbatim.
11// please update this comment if this code is changed,
12// and post the modifications to the above referenced post on SO.
13
14
15
19
20#ifndef EIGEN_SERIALIZATION_ADDON_HPP
21#define EIGEN_SERIALIZATION_ADDON_HPP
22
23#pragma once
24
25
26
27
28friend class boost::serialization::access;
29template<class Archive>
30void save(Archive & ar, const unsigned int version) const {
31 derived().eval();
32 const Eigen::Index rows = derived().rows(), cols = derived().cols();
33 ar & rows;
34 ar & cols;
35 for (Index j = 0; j < cols; ++j )
36 for (Index i = 0; i < rows; ++i )
37 ar & derived().coeff(i, j);
38}
39
40template<class Archive>
41void load(Archive & ar, const unsigned int version) {
42 Eigen::Index rows, cols;
43 ar & rows;
44 ar & cols;
45 if (rows != derived().rows() || cols != derived().cols() )
46 derived().resize(rows, cols);
47 ar & boost::serialization::make_array(derived().data(), derived().size());
48}
49
50template<class Archive>
51void serialize(Archive & ar, const unsigned int file_version) {
52 boost::serialization::split_member(ar, *this, file_version);
53}
54
55#endif // EIGEN_SERIALIZATION_ADDON_HPP