如何序列化boost :: dynamic_bitset?

时间:2015-06-23 15:16:34

标签: c++ boost boost-dynamic-bitset

如何使用boost :: dynamic_bitset成员序列化一个类?

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/dynamic_bitset.hpp>
#include <boost/serialization/bitset.hpp>
#include <sstream>

class A
{
    friend class boost::serialization::access;
    boost::dynamic_bitset<> x;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int){
        ar & x;
    }
};

int main()
{
    A a;
    std::stringstream ss;
    boost::archive::text_oarchive oa(ss);
    oa << a;
    return 0;
}

编译会产生错误(提升1.57)

In file included from /usr/include/boost/serialization/extended_type_info_typeid.hpp:37:0,
                 from /usr/include/boost/archive/detail/oserializer.hpp:38,
                 from /usr/include/boost/archive/detail/interface_oarchive.hpp:23,
                 from /usr/include/boost/archive/detail/common_oarchive.hpp:22,
                 from /usr/include/boost/archive/basic_text_oarchive.hpp:32,
                 from /usr/include/boost/archive/text_oarchive.hpp:31,
                 from dynamic_bitset_setial.cpp:1:
/usr/include/boost/serialization/access.hpp: In static member function ‘static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’:
/usr/include/boost/serialization/serialization.hpp:69:5:   instantiated from ‘void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’
/usr/include/boost/serialization/serialization.hpp:128:9:   instantiated from ‘void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’
/usr/include/boost/archive/detail/oserializer.hpp:148:5:   instantiated from ‘void boost::archive::detail::oserializer<Archive, T>::save_object_data(boost::archive::detail::basic_oarchive&, const void*) const [with Archive = boost::archive::text_oarchive, T = boost::dynamic_bitset<>]’
dynamic_bitset_setial.cpp:25:1:   instantiated from here
/usr/include/boost/serialization/access.hpp:118:9: error: ‘class boost::dynamic_bitset<>’ has no member named ‘serialize’

2 个答案:

答案 0 :(得分:5)

我继续 filed the pull request 为Boost Dynamic Bitset添加序列化支持

  

使用公共界面进行序列化并非最佳,因为to_block_range() / from_block_range()需要复制m_bits(以及后续resize())。

     

我在Boost Dynamic Bitset中添加了一个通用实现。这些更改与开发或主控(1_58_0)完全合并。

     

更改

     

实施添加了

     
      
  • 最小的侵入性,只有一个嵌套的朋友(class serialization_impl;)已向前宣布为&#34; key-hole&#34;通过
  • 访问所需的朋友   
  • 此类以及Boost Serialization的实际ADL挂钩在单独的头文件(dynamic_bitset/serialization.hpp中实现,类似于其他具有序列化支持的boost库)。
  •   
  • 这意味着除非实际包含boost/dynamic_bitset/serialization.hpp,否则存在对Boost序列化内容的零依赖性
  •   
  • 实现零拷贝(利用Boost序列化中std::vector<Block>的内置支持)
  •   
     

测试

     

pull请求中的第二次提交会添加此功能的测试。我不确定如何将dyn_bitset_unit_tests5.cpp添加到Jamfile中。我想必须采取其他措施来确保链接到Boost System和Boost Serialization。我使用一个简单的包装器自己运行测试:

     
#include <modular-boost/libs/dynamic_bitset/dyn_bitset_unit_tests5.cpp>

int main() {
    test_main(0, nullptr);
}
     

然后可以编译并运行,例如

g++ main.cpp -lboost_system -lboost_serialization && ./a.out
     

没有输出意味着没有错误。

答案 1 :(得分:4)

dynamic_bitset<>不可序列化,因为您已经发现(std::bitset<N>是不同的类型)。

不过不用担心,你可以毫不费力地添加它:

namespace boost { namespace serialization {

    template <typename Ar, typename Block, typename Alloc>
        void save(Ar& ar, dynamic_bitset<Block, Alloc> const& bs, unsigned) {
            size_t num_bits = bs.size();
            std::vector<Block> blocks(bs.num_blocks());
            to_block_range(bs, blocks.begin());

            ar & num_bits & blocks;
        }

    template <typename Ar, typename Block, typename Alloc>
        void load(Ar& ar, dynamic_bitset<Block, Alloc>& bs, unsigned) {
            size_t num_bits;
            std::vector<Block> blocks;
            ar & num_bits & blocks;

            bs.resize(num_bits);
            from_block_range(blocks.begin(), blocks.end(), bs);
            bs.resize(num_bits);
        }

    template <typename Ar, typename Block, typename Alloc>
        void serialize(Ar& ar, dynamic_bitset<Block, Alloc>& bs, unsigned version) {
            split_free(ar, bs, version);
        }

} }

这有效,例如的 Live On Coliru

int main() {
    A a;
    for (int i=0; i<128; ++i)
        a.x.resize(11*i, i%2);

    std::stringstream ss;
    {
        boost::archive::text_oarchive oa(ss);
        oa << a;
    }
    std::cout << ss.str();
    {
        boost::archive::text_iarchive ia(ss);
        A b;
        ia >> b;

        assert(a.x == b.x);
    }
}

请注意,如果您无法复制块矢量,那么直接在m_bits级别添加序列化同样容易,但这需要进行干扰性更改(需要访问朋友权限)最小值)。

这样的事情很容易被添加到拉取请求中。

更新added that pull request