#include <Eigen/Core>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
//Boost Serialization of Eigen Matrix
namespace boost {
template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline void serialize(Archive & ar,
Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>& t,
const unsigned int file_version) {
size_t rows = t.rows(), cols = t.cols();
ar & rows;
ar & cols;
if (rows * cols != t.size())
t.resize(rows, cols);
ar & boost::serialization::make_array(t.data(), t.size());
}
}
//Simple helper for serialization to a file
template <typename T>
bool serialize(const T& data, const std::string& filename) {
std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary);
if (!ofs.is_open())
return false;
{ // use scope to ensure archive goes out of scope before stream
boost::archive::binary_oarchive oa(ofs);
oa << data;
}
ofs.close();
return true;
}
//Simple helper for de-serialization from a file
template <typename T>
bool deSerialize(T& data, const std::string& filename) {
std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
if (!ifs.is_open())
return false;
{ // use scope to ensure archive goes out of scope before stream
boost::archive::binary_iarchive ia(ifs);
ia >> data;
}
ifs.close();
return true;
}
int main(int argc, char *argv[]) {
typedef Eigen::Vector3d VectorType;
VectorType* vec = new VectorType(1, 2, 3);
serialize(vec, "v4d.dat");
std::cout << "v4d =\n" << *vec << std::endl;
VectorType* vec_in;
deSerialize(vec_in, "v4d.dat");
std::cout << "v4d_in =\n" << *vec_in << std::endl;
return 0;
}
boost/archive/detail/iserializer.hpp(237): error C2665: 'Eigen::PlainObjectBase<Eigen::Matrix<double,3,1,0,3,1>>::operator delete' : none of the 3 overloads could convert all the argument types
2> c:\users\abhijit\codes\installs\eigen\eigen-install\include\eigen3\eigen\src/Core/PlainObjectBase.h(132): could be 'void Eigen::PlainObjectBase<Eigen::Matrix<double,3,1,0,3,1>>::operator delete(void *,const std::nothrow_t &) throw()'
2> c:\users\abhijit\codes\installs\eigen\eigen-install\include\eigen3\eigen\src/Core/PlainObjectBase.h(132): or 'void Eigen::PlainObjectBase<Eigen::Matrix<double,3,1,0,3,1>>::operator delete(void *,void *) throw()'
2> while trying to match the argument list '(VectorType *, size_t)'
2> C:\Boost\boost-1_56_0_b1\boost/archive/detail/iserializer.hpp(224) : while compiling class template member function 'void boost::archive::detail::heap_allocation<T>::has_new_operator::invoke_delete(T *)'
There is also the following note, but I cannot seem to understand this.
// if compilation fails here, the likely cause that the class
// T has a class specific new operator but no class specific
// delete operator which matches the following signature. Fix
// your program to have this. Note that adding operator delete
// with only one parameter doesn't seem correct to me since
// the standard(3.7.4.2) says "
// "If a class T has a member deallocation function named
// 'operator delete' with exactly one parameter, then that function
// is a usual (non-placement) deallocation function" which I take
// to mean that it will call the destructor of type T which we don't
// want to do here.
I want to understand this issue because this breaks a lot of existing code.