2013-01-25 49 views
2

我想使用升壓serilaization與升壓::進程間::容器::向量如何序列化的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’ 

回答

1

根據標題,我寫了下面的頭,讓進程間矢量的序列化。 更多細節可在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 
0

如果您查看boost序列化庫,則必須手動定義如何序列化某些內容。它所能做的就是爲你序列化基本類型 - 你必須「爲它分開」。因爲錯誤表示它沒有它需要的序列化方法,或者你可以根據你想要的是否侵入性來製作你自己的外部序列化方法。

這裏的教程是非常有幫助:):

http://www.boost.org/doc/libs/1_54_0/libs/serialization/doc/index.html