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
is serializable iff at least one of 5 properties is true, and here at the Multiprecision documentation that the number
class has pass-through support which requires the underlying backend to be serializable.
For Boost.Multiprecision's mpfr_float
type, I know:
- It is not a primitive type.
- It is a class type, but it doesn't have the
serialize
function(s) defined. - It is not a pointer to a Serializable type.
- It is not a reference to a Serializable type.
- It is not a native C++ array of Serializable type.
So, it looks like if I want to serialize the mpfr_float type, I must provide the serialize
function for that type.
My question is this: How can I extend the mpfr_float
type to be serializable by writing the serialize
function myself? I think I need to access the mpfr backend, and play with the underlying data, and I am unsure how to proceed. Tips from someone with experience Boost serializing previously-unserialized classes would be greatly appreciated.
Concluding Solution
Based on the reply from sehe, I arrived at a solution which round-trips just fine with precisions 100 and 1000:
namespace boost { namespace serialization { // insert this code to the appropriate namespaces
/**
Save a mpfr_float type to a boost archive.
*/
template <typename Archive>
void save(Archive& ar, ::boost::multiprecision::backends::mpfr_float_backend<0> const& r, unsigned /*version*/)
{
std::string tmp = r.str(0, std::ios::fixed);// 0 indicates use full precision
ar & tmp;
}
/**
Load a mpfr_float type from a boost archive.
*/
template <typename Archive>
void load(Archive& ar, ::boost::multiprecision::backends::mpfr_float_backend<0>& r, unsigned /*version*/)
{
std::string tmp;
ar & tmp;
r = tmp.c_str();
}
} } // re: namespaces
This solution addresses the need from item (2) above, which indicated the need to add the serialize
functions. Thanks for the help.
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
If you are using version
4.0.0
or higher, you can usempfr_fpif_export
andmpfr_fpif_import
to serialize/deserialize it.