| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#include <algorithm> |
| 32 |
#include <cstdint> |
| 33 |
#include <cstddef> |
| 34 |
#include <limits> |
| 35 |
#include <type_traits> |
| 36 |
|
| 37 |
namespace serialization |
| 38 |
{ |
| 39 |
namespace detail |
| 40 |
{ |
| 41 |
template<typename T> |
| 42 |
inline constexpr bool use_container_varint() noexcept |
| 43 |
{ |
| 44 |
return std::is_integral<T>::value && std::is_unsigned<T>::value && sizeof(T) > 1; |
| 45 |
} |
| 46 |
|
| 47 |
template <typename Archive, class T> |
| 48 |
typename std::enable_if<!use_container_varint<T>(), bool>::type |
| 49 |
serialize_container_element(Archive& ar, T& e) |
| 50 |
{ |
| 51 |
return do_serialize(ar, e); |
| 52 |
} |
| 53 |
|
| 54 |
template<typename Archive, typename T> |
| 55 |
typename std::enable_if<use_container_varint<T>(), bool>::type |
| 56 |
serialize_container_element(Archive& ar, T& e) |
| 57 |
{ |
| 58 |
static constexpr const bool previously_varint = std::is_same<uint64_t, T>() || std::is_same<uint32_t, T>(); |
| 59 |
|
| 60 |
if (!previously_varint && ar.varint_bug_backward_compatibility_enabled() && !typename Archive::is_saving()) |
| 61 |
return do_serialize(ar, e); |
| 62 |
ar.serialize_varint(e); |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
template<typename... C> |
| 68 |
void do_reserve(const C&...) {} |
| 69 |
template<typename C> |
| 70 |
auto do_reserve(C &c, std::size_t N, std::size_t B) -> decltype(c.reserve(N)) |
| 71 |
{ |
| 72 |
using T = typename C::value_type; |
| 73 |
|
| 74 |
static constexpr std::size_t max_compression_ratio = |
| 75 |
is_blob_type<T>::type::value ? 1 : |
| 76 |
use_container_varint<T>() ? sizeof(T) : |
| 77 |
(std::is_same<T, char>::value || std::is_same<T, unsigned char>::value) ? 1: |
| 78 |
4; |
| 79 |
|
| 80 |
|
| 81 |
B /= sizeof(T); |
| 82 |
B = std::max(std::size_t(1), B); |
| 83 |
if (std::numeric_limits<std::size_t>::max() / max_compression_ratio <= B) |
| 84 |
B = std::numeric_limits<std::size_t>::max(); |
| 85 |
else |
| 86 |
B *= max_compression_ratio; |
| 87 |
|
| 88 |
return c.reserve(std::min(N, B)); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
template <template <bool> class Archive, typename C> |
| 94 |
bool do_serialize_container(Archive<false> &ar, C &v) |
| 95 |
{ |
| 96 |
size_t cnt; |
| 97 |
ar.begin_array(cnt); |
| 98 |
if (!ar.good()) |
| 99 |
return false; |
| 100 |
v.clear(); |
| 101 |
|
| 102 |
|
| 103 |
if (ar.remaining_bytes() < cnt) { |
| 104 |
ar.set_fail(); |
| 105 |
return false; |
| 106 |
} |
| 107 |
|
| 108 |
::serialization::detail::do_reserve(v, cnt, ar.remaining_bytes()); |
| 109 |
|
| 110 |
for (size_t i = 0; i < cnt; i++) { |
| 111 |
if (i > 0) |
| 112 |
ar.delimit_array(); |
| 113 |
typename C::value_type e; |
| 114 |
if (!::serialization::detail::serialize_container_element(ar, e)) |
| 115 |
return false; |
| 116 |
::serialization::detail::do_add(v, std::move(e)); |
| 117 |
if (!ar.good()) |
| 118 |
return false; |
| 119 |
} |
| 120 |
ar.end_array(); |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
template <template <bool> class Archive, typename C> |
| 125 |
bool do_serialize_container(Archive<true> &ar, C &v) |
| 126 |
{ |
| 127 |
size_t cnt = v.size(); |
| 128 |
ar.begin_array(cnt); |
| 129 |
for (auto i = v.begin(); i != v.end(); ++i) |
| 130 |
{ |
| 131 |
if (!ar.good()) |
| 132 |
return false; |
| 133 |
if (i != v.begin()) |
| 134 |
ar.delimit_array(); |
| 135 |
if(!::serialization::detail::serialize_container_element(ar, (typename C::value_type&)*i)) |
| 136 |
return false; |
| 137 |
if (!ar.good()) |
| 138 |
return false; |
| 139 |
} |
| 140 |
ar.end_array(); |
| 141 |
return true; |
| 142 |
} |