serialization - 如何序列化 boost::interprocess::containers::vector

标签 serialization boost vector containers interprocess

我想使用带有 boost::interprocess::containers::vector 的 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
    }
}

我收到错误:

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’

最佳答案

基于 header ,我编写了以下 header ,它允许进程间向量的序列化。 更多详细信息,请参阅 my github repo .

问候语 马库斯

#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

关于serialization - 如何序列化 boost::interprocess::containers::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14519884/

相关文章:

c# - 使用 NewtonSoft 将 JSON 对象反序列化为 .Net 对象

class - 即使是 Scala 中简单的序列化示例也不起作用。为什么?

ruby - 解决 yaml for Ruby 中的意外行为——驻留的 unicode 字符串

c++ - 使用带有 split_iterator 的分类器

C++ vector 数组/While 循环

java - 使用 HTTP 客户端将序列化对象从 Android 发送到 servlet

c++ - mpl::transform on boost::fusion::tuple

c++ - 如何 boost Boost ASIO、UDP 客户端应用程序的吞吐量

android - KSOAP2 处理复杂响应(向量)

c++ - 将 vector<double> 写入二进制文件并再次读取