MessagePack for C++
Loading...
Searching...
No Matches
cpp11_msgpack_tuple.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2008-2016 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_V1_CPP11_MSGPACK_TUPLE_HPP
11#define MSGPACK_V1_CPP11_MSGPACK_TUPLE_HPP
12
15#include "msgpack/pack.hpp"
16
17namespace msgpack {
18
22
23namespace type {
24
25template <class... Args>
26inline tuple<Args...> make_tuple(Args&&... args) {
27 return tuple<Args...>(std::forward<Args>(args)...);
28}
29
30template<class... Args>
31inline tuple<Args&&...> forward_as_tuple (Args&&... args) noexcept {
32 return tuple<Args&&...>(std::forward<Args>(args)...);
33}
34
35template <class... Tuples>
36inline auto tuple_cat(Tuples&&... args) ->
37 decltype(
38 std::tuple_cat(std::forward<typename std::remove_reference<Tuples>::type::base>(args)...)
39 ) {
40 return std::tuple_cat(std::forward<typename std::remove_reference<Tuples>::type::base>(args)...);
41}
42
43template <class... Args>
44inline tuple<Args&...> tie(Args&... args) {
45 return tuple<Args&...>(args...);
46}
47} // namespace type
48
49// --- Pack from tuple to packer stream ---
50template <typename Stream, typename Tuple, std::size_t N>
52 static void pack(
54 const Tuple& v) {
56 o.pack(v.template get<N-1>());
57 }
58};
59
60template <typename Stream, typename Tuple>
61struct MsgpackTuplePacker<Stream, Tuple, 1> {
62 static void pack (
64 const Tuple& v) {
65 o.pack(v.template get<0>());
66 }
67};
68
69template <typename Stream, typename Tuple>
70struct MsgpackTuplePacker<Stream, Tuple, 0> {
71 static void pack (
73 const Tuple&) {
74 }
75};
76
77namespace adaptor {
78
79template <typename... Args>
80struct pack<msgpack::type::tuple<Args...>> {
81 template <typename Stream>
84 const msgpack::type::tuple<Args...>& v) const {
85 o.pack_array(sizeof...(Args));
86 MsgpackTuplePacker<Stream, decltype(v), sizeof...(Args)>::pack(o, v);
87 return o;
88 }
89};
90
91} // namespace adaptor
92
93// --- Convert from tuple to object ---
94
95template <typename T, typename... Args>
97 static msgpack::type::tuple<T, Args...> as(msgpack::object const& o) {
98 return msgpack::type::tuple_cat(
99 msgpack::type::make_tuple(o.via.array.ptr[o.via.array.size - sizeof...(Args) - 1].as<T>()),
101 }
102};
103
104template <typename... Args>
106 static msgpack::type::tuple<Args...> as(msgpack::object const& o) {
108 }
109};
110
111template <>
116};
117
118template <typename Tuple, std::size_t N>
120 static void convert(
121 msgpack::object const& o,
122 Tuple& v) {
124 if (o.via.array.size >= N)
125 o.via.array.ptr[N-1].convert<typename std::remove_reference<decltype(v.template get<N-1>())>::type>(v.template get<N-1>());
126 }
127};
128
129template <typename Tuple>
130struct MsgpackTupleConverter<Tuple, 1> {
131 static void convert (
132 msgpack::object const& o,
133 Tuple& v) {
134 o.via.array.ptr[0].convert<typename std::remove_reference<decltype(v.template get<0>())>::type>(v.template get<0>());
135 }
136};
137
138template <typename Tuple>
139struct MsgpackTupleConverter<Tuple, 0> {
140 static void convert (
141 msgpack::object const&,
142 Tuple&) {
143 }
144};
145
146namespace adaptor {
147
148template <typename... Args>
149struct as<msgpack::type::tuple<Args...>, typename std::enable_if<msgpack::any_of<msgpack::has_as, Args...>::value>::type> {
151 msgpack::object const& o) const {
152 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
154 }
155};
156
157template <typename... Args>
158struct convert<msgpack::type::tuple<Args...>> {
160 msgpack::object const& o,
162 if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
163 MsgpackTupleConverter<decltype(v), sizeof...(Args)>::convert(o, v);
164 return o;
165 }
166};
167
168} // namespace adaptor
169
170// --- Convert from tuple to object with zone ---
171template <typename Tuple, std::size_t N>
173 static void convert(
175 const Tuple& v) {
177 o.via.array.ptr[N-1] = msgpack::object(v.template get<N-1>(), o.zone);
178 }
179};
180
181template <typename Tuple>
183 static void convert (
185 const Tuple& v) {
186 o.via.array.ptr[0] = msgpack::object(v.template get<0>(), o.zone);
187 }
188};
189
190template <typename Tuple>
192 static void convert (
194 const Tuple&) {
195 }
196};
197
198namespace adaptor {
199
200template <typename... Args>
204 msgpack::type::tuple<Args...> const& v) const {
205 o.type = msgpack::type::ARRAY;
207 o.via.array.size = sizeof...(Args);
208 MsgpackTupleToObjectWithZone<decltype(v), sizeof...(Args)>::convert(o, v);
209 }
210};
211
212} // namespace adaptor
213
215} // MSGPACK_API_VERSION_NAMESPACE(v1)
217
218} // namespace msgpack
219
220#endif // MSGPACK_CPP11_MSGPACK_TUPLE_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
packer< Stream > & pack(const T &v)
Packing function template.
Definition cpp11_msgpack_tuple_decl.hpp:35
Definition object_fwd.hpp:231
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:255
tuple< Args &... > tie(Args &... args)
Definition cpp11_msgpack_tuple.hpp:44
tuple make_tuple()
Definition cpp03_msgpack_tuple.hpp:10408
auto tuple_cat(Tuples &&... args) -> decltype(std::tuple_cat(std::forward< typename std::remove_reference< Tuples >::type::base >(args)...))
Definition cpp11_msgpack_tuple.hpp:36
tuple< Args &&... > forward_as_tuple(Args &&... args) noexcept
Definition cpp11_msgpack_tuple.hpp:31
Definition adaptor_base.hpp:15
static msgpack::type::tuple as(msgpack::object const &)
Definition cpp11_msgpack_tuple.hpp:113
Definition cpp11_msgpack_tuple.hpp:105
static msgpack::type::tuple< Args... > as(msgpack::object const &o)
Definition cpp11_msgpack_tuple.hpp:106
Definition cpp11_msgpack_tuple.hpp:96
static msgpack::type::tuple< T, Args... > as(msgpack::object const &o)
Definition cpp11_msgpack_tuple.hpp:97
static void convert(msgpack::object const &, Tuple &)
Definition cpp11_msgpack_tuple.hpp:140
static void convert(msgpack::object const &o, Tuple &v)
Definition cpp11_msgpack_tuple.hpp:131
Definition cpp11_msgpack_tuple.hpp:119
static void convert(msgpack::object const &o, Tuple &v)
Definition cpp11_msgpack_tuple.hpp:120
static void pack(msgpack::packer< Stream > &, const Tuple &)
Definition cpp11_msgpack_tuple.hpp:71
static void pack(msgpack::packer< Stream > &o, const Tuple &v)
Definition cpp11_msgpack_tuple.hpp:62
Definition cpp11_msgpack_tuple.hpp:51
static void pack(msgpack::packer< Stream > &o, const Tuple &v)
Definition cpp11_msgpack_tuple.hpp:52
static void convert(msgpack::object::with_zone &, const Tuple &)
Definition cpp11_msgpack_tuple.hpp:192
static void convert(msgpack::object::with_zone &o, const Tuple &v)
Definition cpp11_msgpack_tuple.hpp:183
Definition cpp11_msgpack_tuple.hpp:172
static void convert(msgpack::object::with_zone &o, const Tuple &v)
Definition cpp11_msgpack_tuple.hpp:173
msgpack::type::tuple< Args... > operator()(msgpack::object const &o) const
Definition cpp11_msgpack_tuple.hpp:150
Definition object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, msgpack::type::tuple< Args... > &v) const
Definition cpp11_msgpack_tuple.hpp:159
Definition adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, msgpack::type::tuple< Args... > const &v) const
Definition cpp11_msgpack_tuple.hpp:202
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const msgpack::type::tuple< Args... > &v) const
Definition cpp11_msgpack_tuple.hpp:82
Definition adaptor_base.hpp:32
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
msgpack::enable_if<!msgpack::is_array< T >::value &&!msgpack::is_pointer< T >::value, T & >::type convert(T &v) const
Convert the object.
Definition object.hpp:1076
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition object.hpp:1126
union_type via
Definition object_fwd.hpp:93
msgpack::type::object_type type
Definition object_fwd.hpp:92
Definition cpp03_msgpack_tuple.hpp:9165
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:66