fusion.hpp
Go to the documentation of this file.
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2015-2016 KONDO Takatoshi
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef MSGPACK_V1_TYPE_BOOST_FUSION_HPP
#define MSGPACK_V1_TYPE_BOOST_FUSION_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/adaptor/check_container_size.hpp"
#include "msgpack/meta.hpp"
#include "msgpack/adaptor/pair.hpp"
#if !defined (MSGPACK_USE_CPP03)
#include "msgpack/adaptor/cpp11/tuple.hpp"
#endif // #if !defined (MSGPACK_USE_CPP03)
#include <boost/fusion/support/is_sequence.hpp>
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/sequence/intrinsic/at.hpp>
#include <boost/fusion/include/mpl.hpp>
#include <boost/mpl/size.hpp>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
namespace adaptor {
namespace detail {
template <typename T>
struct is_std_pair {
static bool const value = false;
};
template <typename T, typename U>
struct is_std_pair<std::pair<T, U> > {
static bool const value = true;
};
#if !defined(MSGPACK_USE_CPP03)
template <typename T>
struct is_std_tuple {
static bool const value = false;
};
template <typename... Args>
struct is_std_tuple<std::tuple<Args...>> {
static bool const value = true;
};
#endif // !defined(MSGPACK_USE_CPP03)
template <typename T>
struct is_seq_no_pair_no_tuple {
static bool const value =
boost::fusion::traits::is_sequence<T>::value
&&
!is_std_pair<T>::value
#if !defined (MSGPACK_USE_CPP03)
&&
!is_std_tuple<T>::value
#endif // !defined (MSGPACK_USE_CPP03)
;
};
} // namespace detail
#if !defined (MSGPACK_USE_CPP03)
template <typename T>
struct as<
T,
typename msgpack::enable_if<
detail::is_seq_no_pair_no_tuple<T>::value &&
boost::mpl::fold<
T,
boost::mpl::bool_<true>,
boost::mpl::if_ <
boost::mpl::or_<
boost::mpl::_1,
msgpack::has_as<boost::mpl::_2>
>,
boost::mpl::bool_<true>,
boost::mpl::bool_<false>
>
>::type::value
>::type
> {
T operator()(msgpack::object const& o) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (o.via.array.size != checked_get_container_size(boost::mpl::size<T>::value)) {
throw msgpack::type_error();
}
using tuple_t = decltype(to_tuple(std::declval<T>(), gen_seq<boost::mpl::size<T>::value>()));
return to_t(
o.as<tuple_t>(),
msgpack::gen_seq<boost::mpl::size<T>::value>());
}
template<std::size_t... Is, typename U>
static std::tuple<
typename std::remove_reference<
typename boost::fusion::result_of::at_c<T, Is>::type
>::type...>
to_tuple(U const& u, seq<Is...>) {
return std::make_tuple(boost::fusion::at_c<Is>(u)...);
}
template<std::size_t... Is, typename U>
static T to_t(U const& u, seq<Is...>) {
return T(std::get<Is>(u)...);
}
};
#endif // !defined (MSGPACK_USE_CPP03)
template <typename T>
struct convert<T, typename msgpack::enable_if<detail::is_seq_no_pair_no_tuple<T>::value>::type > {
msgpack::object const& operator()(msgpack::object const& o, T& v) const {
if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
if (o.via.array.size != checked_get_container_size(boost::fusion::size(v))) {
throw msgpack::type_error();
}
uint32_t index = 0;
boost::fusion::for_each(v, convert_imp(o, index));
return o;
}
private:
struct convert_imp {
convert_imp(msgpack::object const& obj, uint32_t& index):obj_(obj), index_(index) {}
template <typename U>
void operator()(U& v) const {
msgpack::adaptor::convert<U>()(obj_.via.array.ptr[index_++], v);
}
private:
msgpack::object const& obj_;
uint32_t& index_;
};
};
template <typename T>
struct pack<T, typename msgpack::enable_if<detail::is_seq_no_pair_no_tuple<T>::value>::type > {
template <typename Stream>
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const T& v) const {
uint32_t size = checked_get_container_size(boost::fusion::size(v));
o.pack_array(size);
boost::fusion::for_each(v, pack_imp<Stream>(o));
return o;
}
private:
template <typename Stream>
struct pack_imp {
pack_imp(msgpack::packer<Stream>& stream):stream_(stream) {}
template <typename U>
void operator()(U const& v) const {
stream_.pack(v);
}
private:
msgpack::packer<Stream>& stream_;
};
};
template <typename T>
struct object_with_zone<T, typename msgpack::enable_if<detail::is_seq_no_pair_no_tuple<T>::value>::type > {
void operator()(msgpack::object::with_zone& o, const T& v) const {
uint32_t size = checked_get_container_size(boost::fusion::size(v));
o.type = msgpack::type::ARRAY;
o.via.array.ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
o.via.array.size = size;
uint32_t count = 0;
boost::fusion::for_each(v, with_zone_imp(o, count));
}
private:
struct with_zone_imp {
with_zone_imp(msgpack::object::with_zone const& obj, uint32_t& count):obj_(obj), count_(count) {}
template <typename U>
void operator()(U const& v) const {
obj_.via.array.ptr[count_++] = msgpack::object(v, obj_.zone);
}
msgpack::object::with_zone const& obj_;
uint32_t& count_;
};
};
} // namespace adaptor
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_V1_TYPE_BOOST_FUSION_HPP
msgpack::object_array::size
uint32_t size
Definition: object_fwd.hpp:23
tuple.hpp
msgpack::zone::allocate_align
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:246
meta.hpp
msgpack::adaptor::detail::is_std_pair
Definition: fusion.hpp:42
msgpack::checked_get_container_size
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
msgpack::adaptor::as
Definition: object_fwd_decl.hpp:62
msgpack::object::via
union_type via
Definition: object_fwd.hpp:93
msgpack::object::as
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:593
adaptor_base.hpp
msgpack::object::with_zone::zone
msgpack::zone & zone
Definition: object.hpp:36
msgpack::adaptor::object_with_zone< T, typename msgpack::enable_if< detail::is_seq_no_pair_no_tuple< T >::value >::type >::operator()
void operator()(msgpack::object::with_zone &o, const T &v) const
Definition: fusion.hpp:175
msgpack::adaptor::detail::is_seq_no_pair_no_tuple
Definition: fusion.hpp:66
msgpack::object_array::ptr
msgpack::object * ptr
Definition: object_fwd.hpp:24
msgpack::packer::pack_array
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1160
msgpack
Definition: adaptor_base.hpp:15
versioning.hpp
msgpack::adaptor::as< T, typename msgpack::enable_if< detail::is_seq_no_pair_no_tuple< T >::value &&boost::mpl::fold< T, boost::mpl::bool_< true >, boost::mpl::if_< boost::mpl::or_< boost::mpl::_1, msgpack::has_as< boost::mpl::_2 > >, boost::mpl::bool_< true >, boost::mpl::bool_< false > > >::type::value >::type >::to_tuple
static std::tuple< typename std::remove_reference< typename boost::fusion::result_of::at_c< T, Is >::type >::type... > to_tuple(U const &u, seq< Is... >)
Definition: fusion.hpp:116
msgpack::object::with_zone
Definition: object.hpp:34
msgpack::seq< Is... >
msgpack::adaptor::pack< T, typename msgpack::enable_if< detail::is_seq_no_pair_no_tuple< T >::value >::type >::operator()
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const T &v) const
Definition: fusion.hpp:154
msgpack::adaptor::object_with_zone
Definition: adaptor_base.hpp:43
pair.hpp
msgpack::type::make_tuple
tuple make_tuple()
Definition: cpp03_msgpack_tuple.hpp:10408
msgpack::type_error
Definition: object_fwd.hpp:236
msgpack::adaptor::pack
Definition: adaptor_base.hpp:32
msgpack::type::size
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
msgpack::object::union_type::array
msgpack::object_array array
Definition: object_fwd.hpp:85
msgpack::enable_if
Definition: cpp_config_decl.hpp:56
msgpack::object
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::adaptor::detail::is_std_tuple
Definition: fusion.hpp:54
msgpack::object::type
msgpack::type::object_type type
Definition: object_fwd.hpp:92
MSGPACK_API_VERSION_NAMESPACE
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
msgpack::type::ARRAY
Definition: object_fwd_decl.hpp:41
msgpack::adaptor::as< T, typename msgpack::enable_if< detail::is_seq_no_pair_no_tuple< T >::value &&boost::mpl::fold< T, boost::mpl::bool_< true >, boost::mpl::if_< boost::mpl::or_< boost::mpl::_1, msgpack::has_as< boost::mpl::_2 > >, boost::mpl::bool_< true >, boost::mpl::bool_< false > > >::type::value >::type >::operator()
T operator()(msgpack::object const &o) const
Definition: fusion.hpp:101
MSGPACK_ZONE_ALIGNOF
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
check_container_size.hpp
msgpack::adaptor::convert< T, typename msgpack::enable_if< detail::is_seq_no_pair_no_tuple< T >::value >::type >::operator()
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: fusion.hpp:129
msgpack::packer
The class template that supports continuous packing.
Definition: adaptor_base_decl.hpp:24
msgpack::adaptor::as< T, typename msgpack::enable_if< detail::is_seq_no_pair_no_tuple< T >::value &&boost::mpl::fold< T, boost::mpl::bool_< true >, boost::mpl::if_< boost::mpl::or_< boost::mpl::_1, msgpack::has_as< boost::mpl::_2 > >, boost::mpl::bool_< true >, boost::mpl::bool_< false > > >::type::value >::type >::to_t
static T to_t(U const &u, seq< Is... >)
Definition: fusion.hpp:120
msgpack::adaptor::convert
Definition: adaptor_base.hpp:27
msgpack::adaptor::detail::is_std_pair::value
static bool const value
Definition: fusion.hpp:43
msgpack::gen_seq
Definition: meta.hpp:40