// 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 .
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include "serialization.h"
template
struct variant_serialization_traits
{
};
template
struct variant_reader
{
typedef typename Archive::variant_tag_type variant_tag_type;
typedef typename boost::mpl::next::type TNext;
typedef typename boost::mpl::deref::type current_type;
static inline bool read(Archive &ar, Variant &v, variant_tag_type t)
{
if (variant_serialization_traits::get_tag() == t) {
current_type x;
if(!::do_serialize(ar, x))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
v = x;
} else {
return variant_reader::read(ar, v, t);
}
return true;
}
};
template
struct variant_reader
{
typedef typename Archive::variant_tag_type variant_tag_type;
static inline bool read(Archive &ar, Variant &v, variant_tag_type t)
{
ar.stream().setstate(std::ios::failbit);
return false;
}
};
template class Archive, BOOST_VARIANT_ENUM_PARAMS(typename T)>
struct serializer, boost::variant>
{
typedef boost::variant variant_type;
typedef typename Archive::variant_tag_type variant_tag_type;
typedef typename variant_type::types types;
static bool serialize(Archive &ar, variant_type &v) {
variant_tag_type t;
ar.begin_variant();
ar.read_variant_tag(t);
if(!variant_reader, variant_type, typename boost::mpl::begin::type, typename boost::mpl::end::type>::read(ar, v, t))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
ar.end_variant();
return true;
}
};
template class Archive, BOOST_VARIANT_ENUM_PARAMS(typename T)>
struct serializer, boost::variant>
{
typedef boost::variant variant_type;
//typedef typename Archive::variant_tag_type variant_tag_type;
struct visitor : public boost::static_visitor
{
Archive &ar;
visitor(Archive &a) : ar(a) { }
template
bool operator ()(T &rv) const
{
ar.begin_variant();
ar.write_variant_tag(variant_serialization_traits, T>::get_tag());
if(!::do_serialize(ar, rv))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
ar.end_variant();
return true;
}
};
static bool serialize(Archive &ar, variant_type &v) {
return boost::apply_visitor(visitor(ar), v);
}
};