Serialization 如何序列化boost::进程间::容器::向量

Serialization 如何序列化boost::进程间::容器::向量,serialization,boost,vector,containers,interprocess,Serialization,Boost,Vector,Containers,Interprocess,我想将boost序列化与boost::进程间::容器::向量一起使用 std::vector的序列化可以通过包括 #include <boost/interprocess/containers/vector.hpp> #包括 但我有一个包含共享向量的类 class MyClass { public: typedef boost::interprocess::allocator<double, SegmentManager> Allocator; t

我想将boost序列化与boost::进程间::容器::向量一起使用

std::vector的序列化可以通过包括

#include <boost/interprocess/containers/vector.hpp>
#包括
但我有一个包含共享向量的类

class MyClass {
  public:
    typedef boost::interprocess::allocator<double, SegmentManager> Allocator;
    typedef boost::interprocess::vector<double, Allocator > VectorDouble;
    VectorDouble *pVar;

    template<class archive>  
    void serialize ( archive &ar, const unsigned int version ) {
        using boost::serialization::make_nvp;
        ar & make_nvp ( "data", *pVar; );  # This does not work
        # what works it creating a std::vector and copy the data
    }
...
    MyClass(){
      # creating the shared memory and the pointer ot pVarß
    }

    ~MyClass(){
      # release data
    }
}
class-MyClass{
公众:
typedef boost::进程间::分配器分配器;
typedef boost::进程间::向量向量double;
矢量双*pVar;
模板
无效序列化(存档和ar,常量未签名整数版本){
使用boost::serialization::make_nvp;
ar&make_nvp(“数据”,*pVar;)35;这不起作用
#如何创建std::vector并复制数据
}
...
MyClass(){
#创建共享内存和pVarß的指针
}
~MyClass(){
#发布数据
}
}
我得到一个错误:

error: ‘class boost::container::vector<double, boost::interprocess::allocator<double, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family, boost::interprocess::offset_ptr<void>, 0u>, boost::interprocess::iset_index> > >’ has no member named ‘serialize’
错误:“class boost::container::vector”没有名为“serialize”的成员

如果查看boost序列化库,您必须手动定义如何序列化某些内容。它所能做的就是为您序列化基类型-您必须为它“拆开”。因此,正如错误所说,它没有所需的serialize方法,或者您可以创建自己的外部serialize方法,具体取决于您是否希望它具有侵入性

这里的教程非常有用:):


基于标题,我编写了以下标题,它允许进程间向量的序列化。 有关更多详细信息,请参阅

问候 马库斯

#ifndef SHMFW_序列化_进程间_向量_HPP
#定义SHMFW_序列化_进程间_向量_HPP
#包括
#包括
名称空间提升{
命名空间序列化{
模板
内联作废保存(
存档和ar,
常量boost::进程间::向量&t,
常量无符号整数文件\u版本
){
boost::serialization::stl::save\u集合(
ar,t
);
}
模板
线内空隙荷载(
存档和ar,
boost::进程间::向量&t,
常量无符号整数文件\u版本
){
#ifdef增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮增压涡轮
if(ar.get_library_version()==boost::archive::library_version_type(5))
{
加载(ar、t、文件版本、boost::is_算术());
返回;
}
#恩迪夫
boost::serialization::stl::load\u集合<
档案文件
boost::进程间::向量,
boost::serialization::stl::归档\u输入\u seq<
归档,boost::进程间::向量
>,
boost::serialization::stl::reserve\u imp
>(ar,t);
}
//将非侵入式序列化函数成员拆分为单独的
//非侵入式保存/加载成员函数
模板
内联空序列化(
存档和ar,
boost::进程间::向量&t,
常量无符号整数文件\u版本
){
boost::serialization::split_free(ar、t、file_版本);
}
#if!BOOST_变通方法(BOOST_MSVC、BOOST_序列化_NVP(计数);
t、 清除();
同时(计数-->0){
布尔一世;
ar>>boost::serialization::make_nvp(“项目”,i);
t、 推回(i);
}
}
//将非侵入式序列化函数成员拆分为单独的
//非侵入式保存/加载成员函数
模板
内联空序列化(
存档和ar,
boost::进程间::向量&t,
常量无符号整数文件\u版本
){
boost::serialization::split_free(ar、t、file_版本);
}
#endif//BOOST\u解决方法
};
};
#endif//SHMFW\u序列化\u进程间\u向量\u HPP
#ifndef SHMFW_SERIALIZATION_INTERPROCESS_VECTOR_HPP
#define SHMFW_SERIALIZATION_INTERPROCESS_VECTOR_HPP


#include <boost/serialization/vector.hpp>
#include <boost/interprocess/containers/vector.hpp>

namespace boost { 
namespace serialization {

template<class Archive, class U, class Allocator>

inline void save(
    Archive & ar,
    const boost::interprocess::vector<U, Allocator> &t,
    const unsigned int file_version
){
    boost::serialization::stl::save_collection<Archive, boost::interprocess::vector<U, Allocator> >(
        ar, t
    );
}

template<class Archive, class U, class Allocator>
inline void load(
    Archive & ar,
    boost::interprocess::vector<U, Allocator> &t,
    const unsigned int file_version
){
#ifdef BOOST_SERIALIZATION_VECTOR_135_HPP
    if (ar.get_library_version()==boost::archive::library_version_type(5))
    {
      load(ar,t,file_version, boost::is_arithmetic<U>());
      return;
    }
#endif
    boost::serialization::stl::load_collection<
        Archive,
        boost::interprocess::vector<U, Allocator>,
        boost::serialization::stl::archive_input_seq<
            Archive, boost::interprocess::vector<U, Allocator> 
        >,
        boost::serialization::stl::reserve_imp<boost::interprocess::vector<U, Allocator> >
    >(ar, t);
}

// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<class Archive, class U, class Allocator>
inline void serialize(
    Archive & ar,
    boost::interprocess::vector<U, Allocator> & t,
    const unsigned int file_version
){
    boost::serialization::split_free(ar, t, file_version);
}

#if ! BOOST_WORKAROUND(BOOST_MSVC, <= 1300)

/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
// vector<bool>
template<class Archive, class Allocator>
inline void save(
    Archive & ar,
    const boost::interprocess::vector<bool, Allocator> &t,
    const unsigned int /* file_version */
){
    // record number of elements
    collection_size_type count (t.size());
    ar << BOOST_SERIALIZATION_NVP(count);
    boost::interprocess::vector<bool>::const_iterator it = t.begin();
    while(count-- > 0){
        bool tb = *it++;
        ar << boost::serialization::make_nvp("item", tb);
    }
}

template<class Archive, class Allocator>
inline void load(
    Archive & ar,
    boost::interprocess::vector<bool, Allocator> &t,
    const unsigned int /* file_version */
){
    // retrieve number of elements
    collection_size_type count;
    ar >> BOOST_SERIALIZATION_NVP(count);
    t.clear();
    while(count-- > 0){
        bool i;
        ar >> boost::serialization::make_nvp("item", i);
        t.push_back(i);
    }
}

// split non-intrusive serialization function member into separate
// non intrusive save/load member functions
template<class Archive, class Allocator>
inline void serialize(
    Archive & ar,
    boost::interprocess::vector<bool, Allocator> & t,
    const unsigned int file_version
){
    boost::serialization::split_free(ar, t, file_version);
}

#endif // BOOST_WORKAROUND

};
};

#endif // SHMFW_SERIALIZATION_INTERPROCESS_VECTOR_HPP