Monero
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 
31 namespace serialization
32 {
33  namespace detail
34  {
35  template<typename T>
36  inline constexpr bool use_container_varint() noexcept
37  {
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);
57  return true;
58  }
59 
60  template <typename C>
61  void do_reserve(C &c, size_t N) {}
62  }
63 }
64 
65 template <template <bool> class Archive, typename C>
66 bool 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;
89  if (!ar.good())
90  return false;
91  }
92  ar.end_array();
93  return true;
94 }
95 
96 template <template <bool> class Archive, typename C>
97 bool 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 }
Definition: binary_utils.h:36
void end_array()
Definition: binary_archive.h:160
void serialize_varint(T &v)
Definition: binary_archive.h:138
const uint32_t T[512]
Definition: groestl_tables.h:36
std::enable_if<!use_container_varint< T >), bool >::type serialize_container_element(Archive &ar, T &e)
Definition: container.h:43
constexpr bool use_container_varint() noexcept
Definition: container.h:36
binary_archive< false > ar
Definition: cold-outputs.cpp:54
int i
Definition: pymoduletest.py:23
void begin_array(size_t &s)
Definition: binary_archive.h:153
int type
Definition: superscalar.cpp:50
void set_fail() noexcept
Definition: binary_archive.h:100
e
Definition: pymoduletest.py:79
declaration and default definition for the functions used the API
Definition: expect.cpp:33
void do_serialize(boost::mpl::false_, Archive &a, epee::net_utils::network_address &na)
Definition: net_peerlist_boost_serialization.h:48
void do_reserve(C &c, size_t N)
Definition: container.h:61
void do_add(std::vector< T > &c, T &&e)
Definition: containers.h:91
const T & move(const T &t)
Definition: gtest-port.h:1317
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
size_t remaining_bytes() const noexcept
Definition: binary_archive.h:169
bool do_serialize_container(Archive< false > &ar, C &v)
Definition: container.h:66
void delimit_array()
Definition: binary_archive.h:159
bool varint_bug_backward_compatibility_enabled() const
Definition: binary_archive.h:171
bool good() const noexcept
Definition: binary_archive.h:99
c
Definition: pymoduletest.py:79