Monero
Loading...
Searching...
No Matches
container.h
Go to the documentation of this file.
1// Copyright (c) 2014-2022, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
31namespace serialization
32{
33 namespace detail
34 {
35 template<typename T>
36 inline constexpr bool use_container_varint() noexcept
37 {
38 return std::is_integral<T>::value && std::is_unsigned<T>::value && sizeof(T) > 1;
39 }
40
41 template <typename Archive, class T>
42 typename std::enable_if<!use_container_varint<T>(), bool>::type
44 {
45 return do_serialize(ar, e);
46 }
47
48 template<typename Archive, typename T>
49 typename std::enable_if<use_container_varint<T>(), bool>::type
51 {
52 static constexpr const bool previously_varint = std::is_same<uint64_t, T>() || std::is_same<uint32_t, T>();
53
54 if (!previously_varint && ar.varint_bug_backward_compatibility_enabled() && !typename Archive::is_saving())
55 return do_serialize(ar, e);
56 ar.serialize_varint(e);
57 return true;
58 }
59
60 template <typename C>
61 void do_reserve(C &c, size_t N) {}
62 }
63}
64
65template <template <bool> class Archive, typename C>
66bool do_serialize_container(Archive<false> &ar, C &v)
67{
68 size_t cnt;
69 ar.begin_array(cnt);
70 if (!ar.good())
71 return false;
72 v.clear();
73
74 // very basic sanity check
75 if (ar.remaining_bytes() < cnt) {
76 ar.set_fail();
77 return false;
78 }
79
81
82 for (size_t i = 0; i < cnt; i++) {
83 if (i > 0)
84 ar.delimit_array();
85 typename C::value_type e;
87 return false;
88 ::serialization::detail::do_add(v, std::move(e));
89 if (!ar.good())
90 return false;
91 }
92 ar.end_array();
93 return true;
94}
95
96template <template <bool> class Archive, typename C>
97bool do_serialize_container(Archive<true> &ar, C &v)
98{
99 size_t cnt = v.size();
100 ar.begin_array(cnt);
101 for (auto i = v.begin(); i != v.end(); ++i)
102 {
103 if (!ar.good())
104 return false;
105 if (i != v.begin())
106 ar.delimit_array();
107 if(!::serialization::detail::serialize_container_element(ar, (typename C::value_type&)*i))
108 return false;
109 if (!ar.good())
110 return false;
111 }
112 ar.end_array();
113 return true;
114}
binary_archive< false > ar
Definition cold-outputs.cpp:54
bool do_serialize_container(Archive< false > &ar, C &v)
Definition container.h:66
bool do_serialize(Archive< false > &ar, std::vector< T > &v)
Definition containers.h:109
declaration and default definition for the functions used the API
Definition expect.cpp:34
void do_add(std::vector< T > &c, T &&e)
Definition containers.h:91
constexpr bool use_container_varint() noexcept
Definition container.h:36
void do_reserve(C &c, size_t N)
Definition container.h:61
std::enable_if<!use_container_varint< T >(), bool >::type serialize_container_element(Archive &ar, T &e)
Definition container.h:43
Definition binary_utils.h:36
#define T(x)