diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-12-22 19:47:12 +0000 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2017-12-22 19:47:12 +0000 |
commit | 2d17feb060275083424d3b5978282b22a5dc88ac (patch) | |
tree | 6e88de54f632117c72e5a8468232dfa1c6afc98b /src/serialization/vector.h | |
parent | Merge pull request #2961 (diff) | |
download | monero-2d17feb060275083424d3b5978282b22a5dc88ac.tar.xz |
factor STL container serialization
Diffstat (limited to 'src/serialization/vector.h')
-rw-r--r-- | src/serialization/vector.h | 82 |
1 files changed, 10 insertions, 72 deletions
diff --git a/src/serialization/vector.h b/src/serialization/vector.h index 12fd59558..9cf3d8272 100644 --- a/src/serialization/vector.h +++ b/src/serialization/vector.h @@ -30,6 +30,7 @@ #pragma once +#include <vector> #include "serialization.h" template <template <bool> class Archive, class T> @@ -37,91 +38,28 @@ bool do_serialize(Archive<false> &ar, std::vector<T> &v); template <template <bool> class Archive, class T> bool do_serialize(Archive<true> &ar, std::vector<T> &v); -template <template <bool> class Archive, class T> -bool do_serialize(Archive<false> &ar, std::deque<T> &v); -template <template <bool> class Archive, class T> -bool do_serialize(Archive<true> &ar, std::deque<T> &v); - namespace serialization { namespace detail { - template <typename Archive, class T> - bool serialize_vector_element(Archive& ar, T& e) - { - return ::do_serialize(ar, e); - } - - template <typename Archive> - bool serialize_vector_element(Archive& ar, uint32_t& e) + template <typename T> + void do_reserve(std::vector<T> &c, size_t N) { - ar.serialize_varint(e); - return true; + c.reserve(N); } - template <typename Archive> - bool serialize_vector_element(Archive& ar, uint64_t& e) + template <typename T> + void do_add(std::vector<T> &c, T &&e) { - ar.serialize_varint(e); - return true; + c.emplace_back(std::move(e)); } } } -template <template <bool> class Archive, class T> -bool do_serialize_vd(Archive<false> &ar, T &v) -{ - size_t cnt; - ar.begin_array(cnt); - if (!ar.stream().good()) - return false; - v.clear(); - - // very basic sanity check - if (ar.remaining_bytes() < cnt) { - ar.stream().setstate(std::ios::failbit); - return false; - } - - v.reserve(cnt); - for (size_t i = 0; i < cnt; i++) { - if (i > 0) - ar.delimit_array(); - v.resize(i+1); - if (!::serialization::detail::serialize_vector_element(ar, v[i])) - return false; - if (!ar.stream().good()) - return false; - } - ar.end_array(); - return true; -} - -template <template <bool> class Archive, class T> -bool do_serialize_vd(Archive<true> &ar, T &v) -{ - size_t cnt = v.size(); - ar.begin_array(cnt); - for (size_t i = 0; i < cnt; i++) { - if (!ar.stream().good()) - return false; - if (i > 0) - ar.delimit_array(); - if(!::serialization::detail::serialize_vector_element(ar, v[i])) - return false; - if (!ar.stream().good()) - return false; - } - ar.end_array(); - return true; -} +#include "container.h" template <template <bool> class Archive, class T> -bool do_serialize(Archive<false> &ar, std::vector<T> &v) { return do_serialize_vd(ar, v); } +bool do_serialize(Archive<false> &ar, std::vector<T> &v) { return do_serialize_container(ar, v); } template <template <bool> class Archive, class T> -bool do_serialize(Archive<true> &ar, std::vector<T> &v) { return do_serialize_vd(ar, v); } +bool do_serialize(Archive<true> &ar, std::vector<T> &v) { return do_serialize_container(ar, v); } -template <template <bool> class Archive, class T> -bool do_serialize(Archive<false> &ar, std::deque<T> &v) { return do_serialize_vd(ar, v); } -template <template <bool> class Archive, class T> -bool do_serialize(Archive<true> &ar, std::deque<T> &v) { return do_serialize_vd(ar, v); } |