I would like to serialize a custom class containing an boost::multiprecision::mpfr_float as a member. It says here in the Boost.Serialization documentation that a type T<
If you are using version 4.0.0
or higher, you can use mpfr_fpif_export
and mpfr_fpif_import
to serialize/deserialize it.
using realtype = number<mpfr_float_backend<100, allocate_stack>>;
#define MPFR_BUFFER_SIZE 1000
namespace boost {
namespace serialization {
template<class Archive>
void save(Archive& ar, const realtype& x, const boost::serialization::version_type&) {
static char buffer[MPFR_BUFFER_SIZE];
FILE* fid = fmemopen(buffer, MPFR_BUFFER_SIZE, "wb+");
mpfr_fpif_export(fid, const_cast<mpfr_ptr>(x.backend().data()));
fseek(fid, 0L, SEEK_END);
long length = ftell(fid);
ar& length;
ar& boost::serialization::make_array(buffer, length);
fclose(fid);
}
template<class Archive>
void load(Archive& ar, realtype& x, const boost::serialization::version_type&) {
static char buffer[MPFR_BUFFER_SIZE];
long length = 0;
ar& length;
ar& boost::serialization::make_array(buffer, length);
FILE* fid = fmemopen(buffer, length, "r");
mpfr_fpif_import(x.backend().data(), fid);
fclose(fid);
}
template<class Archive>
inline void
serialize(Archive& ar, realtype& t, const unsigned int file_version) {
split_free(ar, t, file_version);
}
}
}
The passthrough support implies that you have to add the serialization for the backend type, indeed.
You can use the same approach as I showed in this answer:
where I show how to (de)serialize mpq_rational