danicoin/src/serialization/serialization.h

169 lines
5 KiB
C
Raw Normal View History

2014-08-13 10:38:35 +00:00
// Copyright (c) 2012-2014, The CryptoNote developers, The Bytecoin developers
//
// This file is part of Bytecoin.
//
// Bytecoin is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Bytecoin is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with Bytecoin. If not, see <http://www.gnu.org/licenses/>.
2014-03-03 22:07:58 +00:00
/* serialization.h
*
* Simple templated serialization API */
#pragma once
#include <vector>
#include <string>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/integral_constant.hpp>
2014-03-03 22:07:58 +00:00
template <class T>
struct is_blob_type { typedef boost::false_type type; };
template <class T>
struct has_free_serializer { 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());
}
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<T>::type());
return v.do_serialize(ar);
}
static void serialize_custom(Archive &ar, T &v, boost::true_type) {
}
};
template <class Archive, class T>
inline bool do_serialize(Archive &ar, T &v)
{
return ::serializer<Archive, T>::serialize(ar, v);
}
#ifndef __GNUC__
#ifndef constexpr
#define constexpr
#endif
#endif
#define BLOB_SERIALIZER(T) \
template<> struct is_blob_type<T> { typedef boost::true_type type; }
#define FREE_SERIALIZER(T) \
template<> struct has_free_serializer<T> { typedef boost::true_type type; }
#define VARIANT_TAG(A, T, Tg) \
template <bool W> struct variant_serialization_traits<A<W>, T> { static inline typename A<W>::variant_tag_type get_tag() { return Tg; } }
#define BEGIN_SERIALIZE() \
template <bool W, template <bool> class Archive> bool do_serialize(Archive<W> &ar) {
#define BEGIN_SERIALIZE_OBJECT() \
template <bool W, template <bool> class Archive> bool do_serialize(Archive<W> &ar) { ar.begin_object(); bool r = do_serialize_object(ar); ar.end_object(); return r; } \
template <bool W, template <bool> class Archive> bool do_serialize_object(Archive<W> &ar){
#define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) ::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive<W>::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) \
2014-05-15 16:40:40 +00:00
do { \
2014-03-03 22:07:58 +00:00
bool r = ::do_serialize(ar, f); \
2014-05-15 16:40:40 +00:00
if (!r || !ar.stream().good()) return false; \
} while(0);
2014-03-03 22:07:58 +00:00
#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);
2014-05-15 16:40:40 +00:00
#define VARINT_FIELD_N(t, f) \
do { \
ar.tag(t); \
ar.serialize_varint(f); \
if (!ar.stream().good()) return false; \
} while(0);
2014-03-03 22:07:58 +00:00
namespace serialization {
namespace detail
{
template <typename T>
void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<true>& /*is_saving*/)
{
}
template <typename T>
void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<false>& /*is_saving*/)
{
vec.resize(size);
}
template<class Stream>
bool do_check_stream_state(Stream& s, boost::mpl::bool_<true>)
{
return s.good();
}
template<class Stream>
bool do_check_stream_state(Stream& s, boost::mpl::bool_<false>)
{
bool result = false;
if (s.good())
{
std::ios_base::iostate state = s.rdstate();
result = EOF == s.peek();
2014-04-29 16:26:45 +00:00
s.clear(state);
2014-03-03 22:07:58 +00:00
}
return result;
}
}
template<class Archive>
bool check_stream_state(Archive& ar)
{
return detail::do_check_stream_state(ar.stream(), typename Archive::is_saving());
}
template <class Archive, class T>
inline bool serialize(Archive &ar, T &v)
{
bool r = do_serialize(ar, v);
return r && check_stream_state(ar);
}
}
#include "string.h"
#include "vector.h"