Merge pull request #2990

2d17feb0 factor STL container serialization (moneromooo-monero)
This commit is contained in:
Riccardo Spagni 2018-01-10 11:53:05 +01:00
commit f9c66ba67c
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
15 changed files with 279 additions and 223 deletions

View File

@ -31,7 +31,6 @@
#include "misc_log_ex.h"
#include "enableable.h"
#include "keyvalue_serialization_overloads.h"
#include "serialization/serialization.h"
namespace epee
{

View File

@ -36,7 +36,6 @@
#include <cstring> // memcmp
#include <sstream>
#include <atomic>
#include "serialization/serialization.h"
#include "serialization/variant.h"
#include "serialization/vector.h"
#include "serialization/binary_archive.h"

View File

@ -34,7 +34,7 @@ using namespace epee;
#include "cryptonote_basic_impl.h"
#include "string_tools.h"
#include "serialization/binary_utils.h"
#include "serialization/vector.h"
#include "serialization/container.h"
#include "cryptonote_format_utils.h"
#include "cryptonote_config.h"
#include "misc_language.h"

View File

@ -35,6 +35,7 @@ using namespace epee;
#include <boost/algorithm/string.hpp>
#include "wipeable_string.h"
#include "string_tools.h"
#include "serialization/string.h"
#include "cryptonote_format_utils.h"
#include "cryptonote_config.h"
#include "crypto/crypto.h"

View File

@ -47,7 +47,7 @@ extern "C" {
#include "hex.h"
#include "span.h"
#include "serialization/serialization.h"
#include "serialization/vector.h"
#include "serialization/debug_archive.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"

View File

@ -0,0 +1,113 @@
// Copyright (c) 2014-2017, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
#include "serialization.h"
namespace serialization
{
namespace detail
{
template <typename Archive, class T>
bool serialize_container_element(Archive& ar, T& e)
{
return ::do_serialize(ar, e);
}
template <typename Archive>
bool serialize_container_element(Archive& ar, uint32_t& e)
{
ar.serialize_varint(e);
return true;
}
template <typename Archive>
bool serialize_container_element(Archive& ar, uint64_t& e)
{
ar.serialize_varint(e);
return true;
}
template <typename C>
void do_reserve(C &c, size_t N) {}
}
}
template <template <bool> class Archive, typename C>
bool do_serialize_container(Archive<false> &ar, C &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;
}
::serialization::detail::do_reserve(v, cnt);
for (size_t i = 0; i < cnt; i++) {
if (i > 0)
ar.delimit_array();
typename C::value_type e;
if (!::serialization::detail::serialize_container_element(ar, e))
return false;
::serialization::detail::do_add(v, std::move(e));
if (!ar.stream().good())
return false;
}
ar.end_array();
return true;
}
template <template <bool> class Archive, typename C>
bool do_serialize_container(Archive<true> &ar, C &v)
{
size_t cnt = v.size();
ar.begin_array(cnt);
for (auto i = v.begin(); i != v.end(); ++i)
{
if (!ar.stream().good())
return false;
if (i != v.begin())
ar.delimit_array();
if(!::serialization::detail::serialize_container_element(ar, const_cast<typename C::value_type&>(*i)))
return false;
if (!ar.stream().good())
return false;
}
ar.end_array();
return true;
}

64
src/serialization/deque.h Normal file
View File

@ -0,0 +1,64 @@
// Copyright (c) 2014-2017, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
#include <deque>
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 T>
void do_reserve(std::deque<T> &c, size_t N)
{
c.reserve(N);
}
template <typename T>
void do_add(std::deque<T> &c, T &&e)
{
c.emplace_back(std::move(e));
}
}
}
#include "serialization.h"
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::deque<T> &v) { return do_serialize_container(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::deque<T> &v) { return do_serialize_container(ar, v); }

View File

@ -1,4 +1,4 @@
// Copyright (c) 2014-2015, The Monero Project
// Copyright (c) 2014-2017, The Monero Project
//
// All rights reserved.
//
@ -30,71 +30,29 @@
#pragma once
#include "serialization.h"
#include <list>
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::list<T> &v);
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::list<T> &v);
namespace serialization
{
namespace detail
{
template <typename Archive, class T>
bool serialize_list_element(Archive& ar, T& e)
template <typename T>
void do_add(std::list<T> &c, T &&e)
{
return ::do_serialize(ar, e);
}
template <typename Archive>
bool serialize_list_element(Archive& ar, uint64_t& e)
{
ar.serialize_varint(e);
return true;
c.emplace_back(std::move(e));
}
}
}
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::list<T> &l)
{
size_t cnt;
ar.begin_array(cnt);
if (!ar.stream().good())
return false;
l.clear();
// very basic sanity check
if (ar.remaining_bytes() < cnt) {
ar.stream().setstate(std::ios::failbit);
return false;
}
for (size_t i = 0; i < cnt; i++) {
if (i > 0)
ar.delimit_array();
l.push_back(T());
T &t = l.back();
if (!::serialization::detail::serialize_list_element(ar, t))
return false;
if (!ar.stream().good())
return false;
}
ar.end_array();
return true;
}
#include "serialization.h"
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::list<T> &l)
{
size_t cnt = l.size();
ar.begin_array(cnt);
for (typename std::list<T>::iterator i = l.begin(); i != l.end(); ++i) {
if (!ar.stream().good())
return false;
if (i != l.begin())
ar.delimit_array();
if(!::serialization::detail::serialize_list_element(ar, *i))
return false;
if (!ar.stream().good())
return false;
}
ar.end_array();
return true;
}
bool do_serialize(Archive<false> &ar, std::list<T> &v) { return do_serialize_container(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::list<T> &v) { return do_serialize_container(ar, v); }

View File

@ -63,15 +63,17 @@ struct is_blob_type { typedef boost::false_type type; };
template <class T>
struct has_free_serializer { typedef boost::true_type type; };
/*! \struct is_pair_type
/*! \struct is_basic_type
*
* \brief a descriptor for dispatching serialize
*/
template <class T>
struct is_pair_type { typedef boost::false_type type; };
struct is_basic_type { typedef boost::false_type type; };
template<typename F, typename S>
struct is_pair_type<std::pair<F,S>> { typedef boost::true_type type; };
struct is_basic_type<std::pair<F,S>> { typedef boost::true_type type; };
template<>
struct is_basic_type<std::string> { typedef boost::true_type type; };
/*! \struct serializer
*
@ -89,7 +91,7 @@ struct is_pair_type<std::pair<F,S>> { typedef boost::true_type type; };
template <class Archive, class T>
struct serializer{
static bool serialize(Archive &ar, T &v) {
return serialize(ar, v, typename boost::is_integral<T>::type(), typename is_blob_type<T>::type(), typename is_pair_type<T>::type());
return serialize(ar, v, typename boost::is_integral<T>::type(), typename is_blob_type<T>::type(), typename is_basic_type<T>::type());
}
template<typename A>
static bool serialize(Archive &ar, T &v, boost::false_type, boost::true_type, A a) {
@ -361,9 +363,3 @@ namespace serialization {
return r && check_stream_state(ar);
}
}
#include "string.h"
#include "vector.h"
#include "list.h"
#include "pair.h"
#include "set.h"

View File

@ -30,98 +30,29 @@
#pragma once
#include "serialization.h"
#include <set>
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::set<T> &v);
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::set<T> &v);
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::unordered_set<T> &v);
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::unordered_set<T> &v);
namespace serialization
{
namespace detail
{
template <typename Archive, class T>
bool serialize_set_element(Archive& ar, T& e)
template <typename T>
void do_add(std::set<T> &c, T &&e)
{
return ::do_serialize(ar, e);
}
template <typename Archive>
bool serialize_set_element(Archive& ar, uint32_t& e)
{
ar.serialize_varint(e);
return true;
}
template <typename Archive>
bool serialize_set_element(Archive& ar, uint64_t& e)
{
ar.serialize_varint(e);
return true;
c.insert(std::move(e));
}
}
}
template <template <bool> class Archive, class T>
bool do_serialize_set(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;
}
for (size_t i = 0; i < cnt; i++) {
if (i > 0)
ar.delimit_array();
typename T::key_type k;
if (!::serialization::detail::serialize_set_element(ar, k))
return false;
v.insert(std::move(k));
if (!ar.stream().good())
return false;
}
ar.end_array();
return true;
}
#include "serialization.h"
template <template <bool> class Archive, class T>
bool do_serialize_set(Archive<true> &ar, T &v)
{
size_t cnt = v.size();
ar.begin_array(cnt);
bool first = true;
for (const typename T::key_type &k: v) {
if (!ar.stream().good())
return false;
if (!first)
ar.delimit_array();
if(!::serialization::detail::serialize_set_element(ar, const_cast<typename T::key_type&>(k)))
return false;
if (!ar.stream().good())
return false;
first = false;
}
ar.end_array();
return true;
}
bool do_serialize(Archive<false> &ar, std::set<T> &v) { return do_serialize_container(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::set<T> &v) { return do_serialize_container(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::set<T> &v) { return do_serialize_set(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::set<T> &v) { return do_serialize_set(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::unordered_set<T> &v) { return do_serialize_set(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::unordered_set<T> &v) { return do_serialize_set(ar, v); }

View File

@ -0,0 +1,58 @@
// Copyright (c) 2014-2017, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
#pragma once
#include <set>
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::unordered_set<T> &v);
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::unordered_set<T> &v);
namespace serialization
{
namespace detail
{
template <typename T>
void do_add(std::unordered_set<T> &c, T &&e)
{
c.insert(std::move(e));
}
}
}
#include "serialization.h"
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::unordered_set<T> &v) { return do_serialize_container(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::unordered_set<T> &v) { return do_serialize_container(ar, v); }

View File

@ -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)
template <typename T>
void do_reserve(std::vector<T> &c, size_t N)
{
return ::do_serialize(ar, e);
c.reserve(N);
}
template <typename Archive>
bool serialize_vector_element(Archive& ar, uint32_t& e)
template <typename T>
void do_add(std::vector<T> &c, T &&e)
{
ar.serialize_varint(e);
return true;
}
template <typename Archive>
bool serialize_vector_element(Archive& ar, uint64_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;
}
#include "container.h"
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;
}
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_container(ar, v); }
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::vector<T> &v) { return do_serialize_vd(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); }
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); }

View File

@ -53,6 +53,7 @@ using namespace epee;
#include "profile_tools.h"
#include "crypto/crypto.h"
#include "serialization/binary_utils.h"
#include "serialization/string.h"
#include "cryptonote_basic/blobdatatype.h"
#include "mnemonics/electrum-words.h"
#include "common/i18n.h"

View File

@ -38,7 +38,6 @@
#include "cryptonote_basic/cryptonote_basic.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
#include "ringct/rctSigs.h"
#include "serialization/serialization.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"
#include "serialization/debug_archive.h"

View File

@ -36,7 +36,6 @@
#include <boost/foreach.hpp>
#include "cryptonote_basic/cryptonote_basic.h"
#include "cryptonote_basic/cryptonote_basic_impl.h"
#include "serialization/serialization.h"
#include "serialization/binary_archive.h"
#include "serialization/json_archive.h"
#include "serialization/debug_archive.h"