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;
}