diff options
Diffstat (limited to 'src/serialization/vector.h')
-rw-r--r-- | src/serialization/vector.h | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/serialization/vector.h b/src/serialization/vector.h new file mode 100644 index 000000000..9a0c0ee56 --- /dev/null +++ b/src/serialization/vector.h @@ -0,0 +1,74 @@ +// Copyright (c) 2012-2013 The Cryptonote developers +// Distributed under the MIT/X11 software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#pragma once + +#include "serialization.h" + +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, uint64_t& e) + { + ar.serialize_varint(e); + return true; + } + } +} + +template <template <bool> class Archive, class T> +bool do_serialize(Archive<false> &ar, std::vector<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(Archive<true> &ar, std::vector<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; +} |