boost serialization of mpfr_float

前端 未结 2 1357
旧时难觅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>;
    
    #define MPFR_BUFFER_SIZE 1000
    
        namespace boost {
            namespace serialization {
                template
                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(x.backend().data()));
                    fseek(fid, 0L, SEEK_END);
                    long length = ftell(fid);
                    ar& length;
                    ar& boost::serialization::make_array(buffer, length);
                    fclose(fid);
                }
    
                template
                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
                inline void
                    serialize(Archive& ar, realtype& t, const unsigned int file_version) {
                    split_free(ar, t, file_version);
                }
            }
        }
    

提交回复
热议问题