boost serialization of mpfr_float

前端 未结 2 1326
旧时难觅i
旧时难觅i 2021-01-22 20:04

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<

相关标签:
2条回答
  • 2021-01-22 20:35

    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);
                }
            }
        }
    
    0 讨论(0)
  • 2021-01-22 20:38

    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:

    • How to de/serialize a map with template class using boost::multiprecision::mpq_rational

    where I show how to (de)serialize mpq_rational

    0 讨论(0)
提交回复
热议问题