Boost mpfr_float serialization
I would like to serialize a custom class that contains boost :: multiprecision :: mpfr_float as a member. It says here in the Boost.Serialization documentation that a type T
is serializable if at least one of the 5 properties is true, and here in the Multiprecision documentation that the class number
has end-to-end support that requires the underlying server to be serializable.
For the Boost.Multiprecision type mpfr_float
, I know:
- It is not a primitive type.
- It is a class type, but it has no specific functionality
serialize
. - It is not a pointer to a Serializable type.
- This is not a reference to the Serializable type.
- It is not a native C ++ array of type Serializable.
So it looks like if I want to serialize the mpfr_float type, I have to provide a function serialize
for that type.
My question is, how can I extend a type mpfr_float
for serialization by writing a function serialize
myself? I feel like I need to access the mpfr backend and play with the underlying data, and I'm not sure how to proceed. Advice from someone with experience is greatly appreciated. Drag serialization of previously unserialized classes. [/ P>
Final decision
Based on the answer from sehe, I came up with a solution that is perfect for travel with 100 and 1000 precision:
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 removes the need for paragraph (2) above, which indicates the need to add features serialize
. Thanks for the help.
source to share