Add optional serialization support

This implemenents non-intrusive serialization support to dynamic_bitset<> based on Boost Serialization

 - it supports archives that require named elements
 - it gets private member access using a nested friend class (to decouple the interface entirely)
 - relies on boost/serialization/vector.hpp for actual implementation
This commit is contained in:
Seth Heeren
2015-06-24 01:28:52 +02:00
committed by sehe
parent 7259281f5b
commit 91895380c6
2 changed files with 50 additions and 0 deletions

View File

@@ -348,6 +348,10 @@ public:
#endif
public:
// forward declaration for optional zero-copy serialization support
class serialize_impl;
friend class serialize_impl;
private:
BOOST_STATIC_CONSTANT(block_width_type, ulong_width = std::numeric_limits<unsigned long>::digits);

View File

@@ -0,0 +1,46 @@
// -----------------------------------------------------------
//
// Copyright (c) 2015 Seth Heeren
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// -----------------------------------------------------------
#ifndef BOOST_DYNAMIC_BITSET_SERIALIZATION_HPP
#define BOOST_DYNAMIC_BITSET_SERIALIZATION_HPP
#include "boost/dynamic_bitset/dynamic_bitset.hpp"
#include <boost/serialization/vector.hpp>
namespace boost {
// implementation for optional zero-copy serialization support
template <typename Block, typename Allocator>
class dynamic_bitset<Block, Allocator>::serialize_impl
{
public:
template <typename Ar>
static void serialize(Ar& ar, dynamic_bitset<Block, Allocator>& bs, unsigned) {
ar & serialization::make_nvp("m_num_bits", bs.m_num_bits)
& serialization::make_nvp("m_bits", bs.m_bits);
}
};
}
// ADL hook to Boost Serialization library
namespace boost {
namespace serialization {
template <typename Ar, typename Block, typename Allocator>
void serialize(Ar& ar, dynamic_bitset<Block, Allocator>& bs, unsigned version) {
dynamic_bitset<Block, Allocator>::serialize_impl::serialize(ar, bs, version);
}
} // namespace serialization
} // namespace boost
#endif // include guard