1 ///////////////////////////////////////////////////////////////
2 // Copyright 2013 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
6 #ifndef BOOST_MP_CPP_INT_SERIALIZE_HPP
7 #define BOOST_MP_CPP_INT_SERIALIZE_HPP
13 class binary_oarchive;
14 class binary_iarchive;
18 namespace serialization {
20 namespace mp = boost::multiprecision;
22 namespace cpp_int_detail{
24 using namespace boost::multiprecision;
25 using namespace boost::multiprecision::backends;
28 struct is_binary_archive : public mpl::false_ {};
30 struct is_binary_archive<boost::archive::binary_oarchive> : public mpl::true_ {};
32 struct is_binary_archive<boost::archive::binary_iarchive> : public mpl::true_ {};
35 // We have 8 serialization methods to fill out (and test), they are all permutations of:
37 // Trivial or non-trivial cpp_int type.
38 // Binary or not archive.
40 template <class Archive, class Int>
41 void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::false_ const&, mpl::false_ const&)
49 std::size_t limb_count;
50 std::size_t byte_count;
52 limb_count = byte_count / sizeof(limb_type) + ((byte_count % sizeof(limb_type)) ? 1 : 0);
53 val.resize(limb_count, limb_count);
54 limb_type* pl = val.limbs();
55 for(std::size_t i = 0; i < limb_count; ++i)
58 for(std::size_t j = 0; (j < sizeof(limb_type)) && byte_count; ++j)
62 pl[i] |= static_cast<limb_type>(byte) << (j * CHAR_BIT);
70 template <class Archive, class Int>
71 void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::false_ const&, mpl::false_ const&)
79 limb_type* pl = val.limbs();
80 std::size_t limb_count = val.size();
81 std::size_t byte_count = limb_count * sizeof(limb_type);
84 for(std::size_t i = 0; i < limb_count; ++i)
87 for(std::size_t j = 0; j < sizeof(limb_type); ++j)
89 unsigned char byte = static_cast<unsigned char>((l >> (j * CHAR_BIT)) & ((1u << CHAR_BIT) - 1));
94 template <class Archive, class Int>
95 void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::true_ const&, mpl::false_ const&)
101 typename Int::local_limb_type l = 0;
103 std::size_t byte_count;
105 for(std::size_t i = 0; i < byte_count; ++i)
109 l |= static_cast<typename Int::local_limb_type>(b) << (i * CHAR_BIT);
115 template <class Archive, class Int>
116 void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::true_ const&, mpl::false_ const&)
122 typename Int::local_limb_type l = *val.limbs();
124 std::size_t limb_count = sizeof(l);
126 for(std::size_t i = 0; i < limb_count; ++i)
128 unsigned char b = static_cast<unsigned char>(static_cast<typename Int::local_limb_type>(l >> (i * CHAR_BIT)) & static_cast<typename Int::local_limb_type>((1u << CHAR_BIT) - 1));
132 template <class Archive, class Int>
133 void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::false_ const&, mpl::true_ const&)
143 ar.load_binary(val.limbs(), c * sizeof(limb_type));
148 template <class Archive, class Int>
149 void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::false_ const&, mpl::true_ const&)
155 std::size_t c = val.size();
158 ar.save_binary(val.limbs(), c * sizeof(limb_type));
160 template <class Archive, class Int>
161 void do_serialize(Archive& ar, Int& val, mpl::false_ const&, mpl::true_ const&, mpl::true_ const&)
168 ar.load_binary(val.limbs(), sizeof(*val.limbs()));
172 template <class Archive, class Int>
173 void do_serialize(Archive& ar, Int& val, mpl::true_ const&, mpl::true_ const&, mpl::true_ const&)
180 ar.save_binary(val.limbs(), sizeof(*val.limbs()));
185 template<class Archive, unsigned MinBits, unsigned MaxBits, mp::cpp_integer_type SignType, mp::cpp_int_check_type Checked, class Allocator>
186 void serialize(Archive & ar, mp::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator>& val, const unsigned int /*version*/)
188 typedef typename Archive::is_saving save_tag;
189 typedef mpl::bool_<mp::backends::is_trivial_cpp_int<mp::cpp_int_backend<MinBits, MaxBits, SignType, Checked, Allocator> >::value> trivial_tag;
190 typedef typename cpp_int_detail::is_binary_archive<Archive>::type binary_tag;
192 // Just dispatch to the correct method:
193 cpp_int_detail::do_serialize(ar, val, save_tag(), trivial_tag(), binary_tag());
198 #endif // BOOST_MP_CPP_INT_SERIALIZE_HPP