提升std unique_ptrs的stl集合的序列化

时间:2012-11-12 16:47:10

标签: c++ boost c++11 boost-serialization

我希望能够序列化std :: unique_ptrs的stl容器。可以吗? 顺便说一下,单个std :: unique_ptr一切正常。 下面是我正在处理的代码,gcc给出了以下错误:

 use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const
 std::unique_ptr<_Tp, _Dp>&) [with _Tp = MyDegrees; _Dp =
 std::default_delete<MyDegrees>; std::unique_ptr<_Tp, _Dp> =
 std::unique_ptr<MyDegrees>]’

如何让代码生效?

#include <iostream>
#include <memory>
#include <fstream>
#include <map>
#include <vector>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
namespace boost {
namespace serialization {

template<class Archive, class T>
inline void save(
    Archive & ar,
    const std::unique_ptr< T > &t,
    const unsigned int file_version
) {
    // only the raw pointer has to be saved
    const T * const tx = t.get();
    //ar << tx;
    ar << boost::serialization::make_nvp("px", tx);
}
template<class Archive, class T>
inline void load(
    Archive & ar,
    std::unique_ptr< T > &t,
    const unsigned int file_version
) {
    T *pTarget;
    //ar >> pTarget;
    ar >> boost::serialization::make_nvp("px", pTarget);

#if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
    t.release();
    t = std::unique_ptr< T >(pTarget);
#else
    t.reset(pTarget);
#endif
}
template<class Archive, class T>
inline void serialize(
    Archive & ar,
    std::unique_ptr< T > &t,
    const unsigned int file_version
) {
    boost::serialization::split_free(ar, t, file_version);
}
} // namespace serialization
} // namespace boost

class MyDegrees
{
public:
    void setDeg(int d) {
        deg = d;
    }
    int getDeg()const {
        return deg;
    }
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    //{ ar & deg; }
    {
        ar & boost::serialization::make_nvp("DEGS", deg);
    }
    int deg;
};
class gps_position
{
private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    //{ ar & degrees;  }
    {
        ar & boost::serialization::make_nvp("DEGS2", degrees);
        ar & boost::serialization::make_nvp("DEGMAP", deg_map);
    }
    std::unique_ptr<MyDegrees> degrees;
    std::vector<std::unique_ptr<MyDegrees> > deg_map;
public:
    gps_position(): degrees(std::unique_ptr<MyDegrees>(new MyDegrees)) {};
    void setDeg(int d) {
        degrees->setDeg(d);
    }
    int getDeg() const {
        return degrees->getDeg();
    }
};

int TestBasicSerialize(int, char *[])
{
    int numErr = 0;
    double a;
    std::ofstream ofs("filename");
    gps_position g;
    g.setDeg(45);
    std::cout<<g.getDeg()<<std::endl;
    {
        boost::archive::text_oarchive oa(ofs);
        oa << g;
    }
    //{ boost ::archive::xml_oarchive oa(ofs); oa << g;}
    gps_position newg;
    {
        std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        ia >> newg;
        std::cout<<newg.getDeg()<<std::endl;
    }
    return numErr;

}

1 个答案:

答案 0 :(得分:6)

问题是容器反序列化器正在尝试复制构造unique_ptr。 为了演示,请考虑以下代码产生相同的错误:

std::vector< std::unique_ptr<int> > vec;
std::unique_ptr<int> p;
vec.push_back(p); // does not compile!

但是,这可以使用std::move

来解决
vec.push_back(std::move(p)); // ok

最省力的解决方案是使用可复制构造的智能指针代替, 例如boost::shared_ptr,附带 它在boost/serialization/shared_ptr.hpp中的预定义序列化实现。


下一个解决方案是在类中手动序列化矢量:

//NOTE: this replaces void serialize(...) in gps_position
template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
    ar & BOOST_SERIALIZATION_NVP(degrees);
    size_t size = deg_map.size();
    ar & BOOST_SERIALIZATION_NVP(size);
    for( auto it=deg_map.begin(), end=deg_map.end(); it!=end; ++it )
       ar & boost::serialization::make_nvp("item",*it);
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
    ar & BOOST_SERIALIZATION_NVP(degrees);
    size_t size = 0;
    ar & BOOST_SERIALIZATION_NVP(size);
    deg_map.clear();
    deg_map.reserve(size);
    while( size-- >= 0 ) {
        std::unique_ptr<MyDegrees> p;
        ar & boost::serialization::make_nvp("item",p);
        deg_map.push_back(std::move(p));
    }
}
BOOST_SERIALIZATION_SPLIT_MEMBER()

最后一个解决方案涉及编写自己的容器序列化程序, 就像你为unique_ptr所做的那样,它使用std::move来添加项目:

namespace boost { namespace serialization {
//NOTE: do not include boost/serialization/vector.hpp
template<class Archive, class T, class Allocator>
inline void save(
    Archive & ar,
    const std::vector<T, Allocator> &t,
    const unsigned int
){
    collection_size_type count (t.size());
    ar << BOOST_SERIALIZATION_NVP(count);
    for(auto it=t.begin(), end=t.end(); it!=end; ++it)
        ar << boost::serialization::make_nvp("item", (*it));
}

template<class Archive, class T, class Allocator>
inline void load(
    Archive & ar,
    std::vector<T, Allocator> &t,
    const unsigned int
){
    collection_size_type count;
    ar >> BOOST_SERIALIZATION_NVP(count);
    t.clear();
    t.reserve(count);
    while( count-- > 0 ) {
        T i;
        ar >> boost::serialization::make_nvp("item", i);
        t.push_back(std::move(i)); // use std::move
    }
}

template<class Archive, class T, class Allocator>
inline void serialize(
    Archive & ar,
    std::vector<T, Allocator> & t,
    const unsigned int file_version
){
    boost::serialization::split_free(ar, t, file_version);
}
} } // namespace boost::serialization

最后但并非最不重要的,你应该使用:

oa << BOOST_SERIALIZATION_NVP(g);
// and
ia >> BOOST_SERIALIZATION_NVP(newg);
使用xml存档时,在TestBasicSerialize()中