MessagePack for C++
Loading...
Searching...
No Matches
fusion.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2015-2016 KONDO Takatoshi
5//
6// Distributed under the Boost Software License, Version 1.0.
7// (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9//
10#ifndef MSGPACK_V1_TYPE_BOOST_FUSION_HPP
11#define MSGPACK_V1_TYPE_BOOST_FUSION_HPP
12
16#include "msgpack/meta.hpp"
17
19
20#if !defined (MSGPACK_USE_CPP03)
22#endif // #if !defined (MSGPACK_USE_CPP03)
23
24#include <boost/fusion/support/is_sequence.hpp>
25#include <boost/fusion/sequence/intrinsic/size.hpp>
26#include <boost/fusion/algorithm/iteration/for_each.hpp>
27#include <boost/fusion/sequence/intrinsic/at.hpp>
28#include <boost/fusion/include/mpl.hpp>
29#include <boost/mpl/size.hpp>
30
31namespace msgpack {
32
36
37namespace adaptor {
38
39namespace detail {
40
41template <typename T>
43 static bool const value = false;
44};
45
46template <typename T, typename U>
47struct is_std_pair<std::pair<T, U> > {
48 static bool const value = true;
49};
50
51#if !defined(MSGPACK_USE_CPP03)
52
53template <typename T>
55 static bool const value = false;
56};
57
58template <typename... Args>
59struct is_std_tuple<std::tuple<Args...>> {
60 static bool const value = true;
61};
62
63#endif // !defined(MSGPACK_USE_CPP03)
64
65template <typename T>
67 static bool const value =
68 boost::fusion::traits::is_sequence<T>::value
69 &&
71#if !defined (MSGPACK_USE_CPP03)
72 &&
74#endif // !defined (MSGPACK_USE_CPP03)
75 ;
76};
77
78} // namespace detail
79
80#if !defined (MSGPACK_USE_CPP03)
81
82template <typename T>
83struct as<
84 T,
85 typename msgpack::enable_if<
86 detail::is_seq_no_pair_no_tuple<T>::value &&
87 boost::mpl::fold<
88 T,
89 boost::mpl::bool_<true>,
90 boost::mpl::if_ <
91 boost::mpl::or_<
92 boost::mpl::_1,
93 msgpack::has_as<boost::mpl::_2>
94 >,
95 boost::mpl::bool_<true>,
96 boost::mpl::bool_<false>
97 >
98 >::type::value
99 >::type
100> {
101 T operator()(msgpack::object const& o) const {
102 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
103 if (o.via.array.size != checked_get_container_size(boost::mpl::size<T>::value)) {
104 throw msgpack::type_error();
105 }
106 using tuple_t = decltype(to_tuple(std::declval<T>(), gen_seq<boost::mpl::size<T>::value>()));
107 return to_t(
108 o.as<tuple_t>(),
109 msgpack::gen_seq<boost::mpl::size<T>::value>());
110 }
111 template<std::size_t... Is, typename U>
112 static std::tuple<
113 typename std::remove_reference<
114 typename boost::fusion::result_of::at_c<T, Is>::type
115 >::type...>
116 to_tuple(U const& u, seq<Is...>) {
117 return std::make_tuple(boost::fusion::at_c<Is>(u)...);
118 }
119 template<std::size_t... Is, typename U>
120 static T to_t(U const& u, seq<Is...>) {
121 return T(std::get<Is>(u)...);
122 }
123};
124
125#endif // !defined (MSGPACK_USE_CPP03)
126
127template <typename T>
128struct convert<T, typename msgpack::enable_if<detail::is_seq_no_pair_no_tuple<T>::value>::type > {
129 msgpack::object const& operator()(msgpack::object const& o, T& v) const {
130 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
131 if (o.via.array.size != checked_get_container_size(boost::fusion::size(v))) {
132 throw msgpack::type_error();
133 }
134 uint32_t index = 0;
135 boost::fusion::for_each(v, convert_imp(o, index));
136 return o;
137 }
138private:
139 struct convert_imp {
140 convert_imp(msgpack::object const& obj, uint32_t& index):obj_(obj), index_(index) {}
141 template <typename U>
142 void operator()(U& v) const {
143 msgpack::adaptor::convert<U>()(obj_.via.array.ptr[index_++], v);
144 }
145 private:
146 msgpack::object const& obj_;
147 uint32_t& index_;
148 };
149};
150
151template <typename T>
152struct pack<T, typename msgpack::enable_if<detail::is_seq_no_pair_no_tuple<T>::value>::type > {
153 template <typename Stream>
155 uint32_t size = checked_get_container_size(boost::fusion::size(v));
156 o.pack_array(size);
157 boost::fusion::for_each(v, pack_imp<Stream>(o));
158 return o;
159 }
160private:
161 template <typename Stream>
162 struct pack_imp {
163 pack_imp(msgpack::packer<Stream>& stream):stream_(stream) {}
164 template <typename U>
165 void operator()(U const& v) const {
166 stream_.pack(v);
167 }
168 private:
170 };
171};
172
173template <typename T>
174struct object_with_zone<T, typename msgpack::enable_if<detail::is_seq_no_pair_no_tuple<T>::value>::type > {
175 void operator()(msgpack::object::with_zone& o, const T& v) const {
176 uint32_t size = checked_get_container_size(boost::fusion::size(v));
177 o.type = msgpack::type::ARRAY;
179 o.via.array.size = size;
180 uint32_t count = 0;
181 boost::fusion::for_each(v, with_zone_imp(o, count));
182 }
183private:
184 struct with_zone_imp {
185 with_zone_imp(msgpack::object::with_zone const& obj, uint32_t& count):obj_(obj), count_(count) {}
186 template <typename U>
187 void operator()(U const& v) const {
188 obj_.via.array.ptr[count_++] = msgpack::object(v, obj_.zone);
189 }
190 msgpack::object::with_zone const& obj_;
191 uint32_t& count_;
192 };
193};
194
195} // namespace adaptor
196
198} // MSGPACK_API_VERSION_NAMESPACE(v1)
200
201} // namespace msgpack
202
203#endif // MSGPACK_V1_TYPE_BOOST_FUSION_HPP
The class template that supports continuous packing.
Definition pack.hpp:33
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition pack.hpp:1195
Definition object_fwd.hpp:236
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:246
Definition adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition check_container_size.hpp:55
Definition object_fwd_decl.hpp:62
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition fusion.hpp:129
Definition adaptor_base.hpp:27
static bool const value
Definition fusion.hpp:67
static bool const value
Definition fusion.hpp:43
static bool const value
Definition fusion.hpp:55
void operator()(msgpack::object::with_zone &o, const T &v) const
Definition fusion.hpp:175
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const T &v) const
Definition fusion.hpp:154
Definition adaptor_base.hpp:32
Definition cpp_config_decl.hpp:56
Definition meta.hpp:40
Definition object.hpp:35
msgpack::zone & zone
Definition object.hpp:37
uint32_t size
Definition object_fwd.hpp:23
msgpack::object * ptr
Definition object_fwd.hpp:24
Object class that corresponding to MessagePack format object.
Definition object_fwd.hpp:75
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition object.hpp:1121
union_type via
Definition object_fwd.hpp:93
msgpack::type::object_type type
Definition object_fwd.hpp:92
Definition meta.hpp:37
msgpack::object_array array
Definition object_fwd.hpp:85
#define MSGPACK_ZONE_ALIGNOF(type)
Definition cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:58