// Copyright (c) 2014-2018 The The Louisdor Project // 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. /* serialization.h * * Simple templated serialization API */ #pragma once #include #include #include #include "misc_log_ex.h" #include "binary_archive.h" template struct is_blob_type { typedef boost::false_type type; }; template struct has_free_serializer { typedef boost::true_type type; }; template struct serializer { static bool serialize(Archive &ar, T &v) { return serialize(ar, v, typename boost::is_integral::type(), typename is_blob_type::type()); } static bool serialize(Archive &ar, T &v, boost::false_type, boost::true_type) { ar.serialize_blob(&v, sizeof(v)); return true; } static bool serialize(Archive &ar, T &v, boost::true_type, boost::false_type) { ar.serialize_int(v); return true; } static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type) { //serialize_custom(ar, v, typename has_free_serializer::type()); return v.do_serialize(ar); } static void serialize_custom(Archive &ar, T &v, boost::true_type) { } }; template inline bool do_serialize(Archive &ar, T &v) { return ::serializer::serialize(ar, v); } #if ( !defined(__GNUC__) && !(defined(_MSC_VER) && (_MSC_VER >= 1900))) #ifndef constexpr #define constexpr #endif #endif #define BLOB_SERIALIZER(T) \ template<> struct is_blob_type { typedef boost::true_type type; } #define FREE_SERIALIZER(T) \ template<> struct has_free_serializer { typedef boost::true_type type; } #define VARIANT_TAG(A, T, Tg) \ template struct variant_serialization_traits, T> { static inline typename A::variant_tag_type get_tag() { return Tg; } } #define BEGIN_SERIALIZE() \ template class Archive> bool do_serialize(Archive &ar) { #define BEGIN_SERIALIZE_OBJECT() \ template class Archive> bool do_serialize(Archive &ar) { ar.begin_object(); bool r = do_serialize_object(ar); ar.end_object(); return r; } \ template class Archive> bool do_serialize_object(Archive &ar){ #define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) ::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive::is_saving()) #define END_SERIALIZE() return true;} #define VALUE(f) \ do { \ ar.tag(#f); \ bool r = ::do_serialize(ar, f); \ if (!r || !ar.stream().good()) return false; \ } while (0); #define FIELD_N(t, f) \ do { \ ar.tag(t); \ bool r = ::do_serialize(ar, f); \ if (!r || !ar.stream().good()) return false; \ } while (0); #define FIELDS(f) \ bool r = ::do_serialize(ar, f); \ if (!r || !ar.stream().good()) return false; #define FIELD(f) \ do { \ ar.tag(#f); \ bool r = ::do_serialize(ar, f); \ if (!r || !ar.stream().good()) return false; \ } while (0); #define VARINT_FIELD(f) \ do { \ ar.tag(#f); \ ar.serialize_varint(f); \ if (!ar.stream().good()) return false; \ } while (0); #define DEFINE_SERIALIZATION_VERSION(v) inline static uint32_t get_serialization_veraion(){ return v; } #define VERSION_ENTRY(f) \ do { \ ar.tag(#f); \ if (ar.is_saving_arch()) \ f = this->get_serialization_veraion(); \ bool r = ::do_serialize(ar, f); \ if (!r || !ar.stream().good()) return false; \ } while (0); template class serializable_pair : public std::pair { typedef std::pair base; public: serializable_pair() {} serializable_pair(const first_type& a, const second_type& b) :std::pair(a, b) {} serializable_pair(const serializable_pair& sp) :std::pair(sp.first, sp.second) {} BEGIN_SERIALIZE_OBJECT() FIELD(base::first) FIELD(base::second) END_SERIALIZE() }; template serializable_pair make_serializable_pair(const first_type& first_value, const second_type& second_value) { return serializable_pair(first_value, second_value); } namespace serialization { namespace detail { template void prepare_custom_vector_serialization(size_t size, std::vector& vec, const boost::mpl::bool_& /*is_saving*/) { } template void prepare_custom_vector_serialization(size_t size, std::vector& vec, const boost::mpl::bool_& /*is_saving*/) { vec.resize(size); } template bool do_check_stream_state(Stream& s, boost::mpl::bool_) { return s.good(); } template bool do_check_stream_state(Stream& s, boost::mpl::bool_) { bool result = false; if (s.good()) { std::ios_base::iostate state = s.rdstate(); result = EOF == s.peek(); s.clear(state); } return result; } } template bool check_stream_state(Archive& ar) { return detail::do_check_stream_state(ar.stream(), typename Archive::is_saving()); } template inline bool serialize(Archive &ar, T &v) { bool r = do_serialize(ar, v); return r && check_stream_state(ar); } } //--------------------------------------------------------------- template bool t_serializable_object_to_blob(const t_object& to, std::string& b_blob) { std::stringstream ss; binary_archive ba(ss); bool r = ::serialization::serialize(ba, const_cast(to)); b_blob = ss.str(); return r; } //--------------------------------------------------------------- template bool t_unserializable_object_from_blob(t_object& to, const std::string& blob) { std::stringstream ss; ss << blob; binary_archive ba(ss); bool r = ::serialization::serialize(ba, to); CHECK_AND_ASSERT_MES(r, false, "Failed to parse block from blob"); return true; } //--------------------------------------------------------------- template std::string t_serializable_object_to_blob(const t_object& to) { std::string b; t_serializable_object_to_blob(to, b); return b; } #include "serialize_basic_types.h" #include "string.h" #include "multiprecision.h" #include "stl_containers.h"