MessagePack for C++
cpp11_msgpack_tuple.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2015 FURUHASHI Sadayuki and 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_CPP11_MSGPACK_TUPLE_HPP
11 #define MSGPACK_CPP11_MSGPACK_TUPLE_HPP
12 
13 #include "msgpack/versioning.hpp"
14 #include "msgpack/object_fwd.hpp"
15 #include "msgpack/meta.hpp"
16 
17 #include <tuple>
18 
19 namespace msgpack {
20 
24 
25 namespace type {
26  // tuple
27  using std::get;
28  using std::tuple_size;
29  using std::tuple_element;
30  using std::uses_allocator;
31  using std::ignore;
32  using std::swap;
33 
34  template< class... Types >
35  class tuple : public std::tuple<Types...> {
36  public:
37  using base = std::tuple<Types...>;
38 
39 
40  tuple(tuple const&) = default;
41  tuple(tuple&&) = default;
42 
43  template<typename... OtherTypes>
44  tuple(OtherTypes&&... other):base(std::forward<OtherTypes>(other)...) {}
45 
46  template<typename... OtherTypes>
47  tuple(tuple<OtherTypes...> const& other):base(static_cast<std::tuple<OtherTypes...> const&>(other)) {}
48  template<typename... OtherTypes>
49  tuple(tuple<OtherTypes...> && other):base(static_cast<std::tuple<OtherTypes...> &&>(other)) {}
50 
51  tuple& operator=(tuple const&) = default;
52  tuple& operator=(tuple&&) = default;
53 
54  template<typename... OtherTypes>
56  *static_cast<base*>(this) = static_cast<std::tuple<OtherTypes...> const&>(other);
57  return *this;
58  }
59  template<typename... OtherTypes>
61  *static_cast<base*>(this) = static_cast<std::tuple<OtherTypes...> &&>(other);
62  return *this;
63  }
64 
65  template< std::size_t I>
67  get() & { return std::get<I>(*this); }
68 
69  template< std::size_t I>
70  typename tuple_element<I, base >::type const&
71  get() const& { return std::get<I>(*this); }
72 
73  template< std::size_t I>
75  get() && { return std::get<I>(*this); }
76  };
77 
78  template <class... Args>
79  inline tuple<Args...> make_tuple(Args&&... args) {
80  return tuple<Args...>(args...);
81  }
82 
83  template<class... Args>
84  inline tuple<Args&&...> forward_as_tuple (Args&&... args) noexcept {
85  return tuple<Args&&...>(std::forward<Args>(args)...);
86  }
87 
88  template <class... Tuples>
89  inline auto tuple_cat(Tuples&&... args) ->
90  decltype(
91  std::tuple_cat(std::forward<typename std::remove_reference<Tuples>::type::base>(args)...)
92  ) {
93  return std::tuple_cat(std::forward<typename std::remove_reference<Tuples>::type::base>(args)...);
94  }
95  template <class... Args>
96  inline tuple<Args&...> tie(Args&... args) {
97  return tuple<Args&...>(args...);
98  }
99 } // namespace type
100 
101 // --- Pack from tuple to packer stream ---
102 template <typename Stream, typename Tuple, std::size_t N>
104  static void pack(
106  const Tuple& v) {
108  o.pack(type::get<N-1>(v));
109  }
110 };
111 
112 template <typename Stream, typename Tuple>
113 struct MsgpackTuplePacker<Stream, Tuple, 1> {
114  static void pack (
116  const Tuple& v) {
117  o.pack(type::get<0>(v));
118  }
119 };
120 
121 template <typename Stream, typename Tuple>
122 struct MsgpackTuplePacker<Stream, Tuple, 0> {
123  static void pack (
125  const Tuple&) {
126  }
127 };
128 
129 namespace adaptor {
130 
131 template <typename... Args>
132 struct pack<msgpack::type::tuple<Args...>> {
133  template <typename Stream>
136  const msgpack::type::tuple<Args...>& v) const {
137  o.pack_array(sizeof...(Args));
138  MsgpackTuplePacker<Stream, decltype(v), sizeof...(Args)>::pack(o, v);
139  return o;
140  }
141 };
142 
143 } // namespace adaptor
144 
145 // --- Convert from tuple to object ---
146 
147 template <typename... Args>
149 
150 template <typename T, typename... Args>
152  static msgpack::type::tuple<T, Args...> as(msgpack::object const& o) {
154  msgpack::type::make_tuple(o.via.array.ptr[o.via.array.size - sizeof...(Args) - 1].as<T>()),
156  }
157 };
158 
159 template <typename... Args>
160 struct MsgpackTupleAs {
161  static msgpack::type::tuple<Args...> as(msgpack::object const& o) {
163  }
164 };
165 
166 template <>
167 struct MsgpackTupleAs<> {
169  return msgpack::type::tuple<>();
170  }
171 };
172 
173 template <typename Tuple, std::size_t N>
175  static void convert(
176  msgpack::object const& o,
177  Tuple& v) {
179  o.via.array.ptr[N-1].convert<typename std::remove_reference<decltype(type::get<N-1>(v))>::type>(type::get<N-1>(v));
180  }
181 };
182 
183 template <typename Tuple>
184 struct MsgpackTupleConverter<Tuple, 1> {
185  static void convert (
186  msgpack::object const& o,
187  Tuple& v) {
188  o.via.array.ptr[0].convert<typename std::remove_reference<decltype(type::get<0>(v))>::type>(type::get<0>(v));
189  }
190 };
191 
192 template <typename Tuple>
193 struct MsgpackTupleConverter<Tuple, 0> {
194  static void convert (
195  msgpack::object const&,
196  Tuple&) {
197  }
198 };
199 
200 namespace adaptor {
201 
202 template <typename... Args>
203 struct as<msgpack::type::tuple<Args...>, typename std::enable_if<msgpack::all_of<msgpack::has_as, Args...>::value>::type> {
205  msgpack::object const& o) const {
206  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
207  if (o.via.array.size < sizeof...(Args)) { throw msgpack::type_error(); }
208  return MsgpackTupleAs<Args...>::as(o);
209  }
210 };
211 
212 template <typename... Args>
213 struct convert<msgpack::type::tuple<Args...>> {
215  msgpack::object const& o,
217  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
218  if(o.via.array.size < sizeof...(Args)) { throw msgpack::type_error(); }
219  MsgpackTupleConverter<decltype(v), sizeof...(Args)>::convert(o, v);
220  return o;
221  }
222 };
223 
224 } // namespace adaptor
225 
226 // --- Convert from tuple to object with zone ---
227 template <typename Tuple, std::size_t N>
229  static void convert(
231  const Tuple& v) {
233  o.via.array.ptr[N-1] = msgpack::object(type::get<N-1>(v), o.zone);
234  }
235 };
236 
237 template <typename Tuple>
239  static void convert (
241  const Tuple& v) {
242  o.via.array.ptr[0] = msgpack::object(type::get<0>(v), o.zone);
243  }
244 };
245 
246 template <typename Tuple>
248  static void convert (
250  const Tuple&) {
251  }
252 };
253 
254 namespace adaptor {
255 
256 template <typename... Args>
260  msgpack::type::tuple<Args...> const& v) const {
262  o.via.array.ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*sizeof...(Args)));
263  o.via.array.size = sizeof...(Args);
264  MsgpackTupleToObjectWithZone<decltype(v), sizeof...(Args)>::convert(o, v);
265  }
266 };
267 
268 } // namespace adaptor
269 
271 } // MSGPACK_API_VERSION_NAMESPACE(v1)
273 
274 } // namespace msgpack
275 
276 #endif // MSGPACK_CPP11_MSGPACK_TUPLE_HPP
T & convert(T &v) const
Convert the object.
Definition: object.hpp:520
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::tuple< Args... > &v) const
Definition: cpp11_msgpack_tuple.hpp:134
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
auto tuple_cat(Tuples &&... args) -> decltype(std::tuple_cat(std::forward< typename std::remove_reference< Tuples >::type::base >(args)...))
Definition: cpp11_msgpack_tuple.hpp:89
uint32_t size
Definition: object_fwd.hpp:50
tuple(tuple const &)=default
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:248
tuple< Args &... > tie(Args &... args)
Definition: cpp11_msgpack_tuple.hpp:96
static msgpack::type::tuple< Args... > as(msgpack::object const &o)
Definition: cpp11_msgpack_tuple.hpp:161
static void convert(msgpack::object::with_zone &, const Tuple &)
Definition: cpp11_msgpack_tuple.hpp:248
Definition: object_fwd.hpp:82
Definition: cpp03_msgpack_tuple.hpp:35
union_type via
Definition: object_fwd.hpp:123
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:558
static void convert(msgpack::object::with_zone &o, const Tuple &v)
Definition: cpp11_msgpack_tuple.hpp:229
tuple(tuple< OtherTypes... > const &other)
Definition: cpp11_msgpack_tuple.hpp:47
msgpack::zone & zone
Definition: object_fwd.hpp:262
void operator()(msgpack::object::with_zone &o, msgpack::type::tuple< Args... > const &v) const
Definition: cpp11_msgpack_tuple.hpp:258
tuple & operator=(tuple< OtherTypes... > &&other)
Definition: cpp11_msgpack_tuple.hpp:60
msgpack::object * ptr
Definition: object_fwd.hpp:51
static void convert(msgpack::object const &o, Tuple &v)
Definition: cpp11_msgpack_tuple.hpp:185
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition: pack.hpp:1163
Definition: adaptor_base.hpp:15
static void pack(msgpack::packer< Stream > &o, const Tuple &v)
Definition: cpp11_msgpack_tuple.hpp:104
static void convert(msgpack::object::with_zone &o, const Tuple &v)
Definition: cpp11_msgpack_tuple.hpp:239
tuple & operator=(tuple const &)=default
Definition: cpp11_msgpack_tuple.hpp:228
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:631
Definition: object_fwd.hpp:260
packer< Stream > & pack(const T &v)
Packing function template.
Definition: cpp11_msgpack_tuple.hpp:103
Definition: cpp03_msgpack_tuple.hpp:9178
msgpack::type::tuple< Args... > operator()(msgpack::object const &o) const
Definition: cpp11_msgpack_tuple.hpp:204
std::tuple< Types... > base
Definition: cpp11_msgpack_tuple.hpp:37
Definition: adaptor_base.hpp:45
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::tuple< Args... > &v) const
Definition: cpp11_msgpack_tuple.hpp:214
tuple make_tuple()
Definition: cpp03_msgpack_tuple.hpp:10388
Definition: object_fwd.hpp:253
tuple & operator=(tuple< OtherTypes... > const &other)
Definition: cpp11_msgpack_tuple.hpp:55
static void pack(msgpack::packer< Stream > &, const Tuple &)
Definition: cpp11_msgpack_tuple.hpp:123
Definition: adaptor_base.hpp:34
tuple(tuple< OtherTypes... > &&other)
Definition: cpp11_msgpack_tuple.hpp:49
msgpack::object_array array
Definition: object_fwd.hpp:115
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:638
static msgpack::type::tuple as(msgpack::object const &)
Definition: cpp11_msgpack_tuple.hpp:168
static void pack(msgpack::packer< Stream > &o, const Tuple &v)
Definition: cpp11_msgpack_tuple.hpp:114
tuple(OtherTypes &&... other)
Definition: cpp11_msgpack_tuple.hpp:44
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:106
static void convert(msgpack::object const &o, Tuple &v)
Definition: cpp11_msgpack_tuple.hpp:175
Definition: cpp11_msgpack_tuple.hpp:148
msgpack::type::object_type type
Definition: object_fwd.hpp:122
Definition: object_fwd.hpp:39
static void convert(msgpack::object const &, Tuple &)
Definition: cpp11_msgpack_tuple.hpp:194
tuple< Args &&... > forward_as_tuple(Args &&... args) noexcept
Definition: cpp11_msgpack_tuple.hpp:84
Definition: cpp11_msgpack_tuple.hpp:35
The class template that supports continuous packing.
Definition: adaptor_base.hpp:22
static msgpack::type::tuple< T, Args... > as(msgpack::object const &o)
Definition: cpp11_msgpack_tuple.hpp:152
Definition: cpp11_msgpack_tuple.hpp:174
Definition: cpp11_msgpack_tuple.hpp:151
Definition: adaptor_base.hpp:29