/
cpp03_msgpack_tuple.hpp.erb
227 lines (200 loc) · 7.07 KB
/
cpp03_msgpack_tuple.hpp.erb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2008-2016 FURUHASHI Sadayuki and 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_CPP03_MSGPACK_TUPLE_HPP
#define MSGPACK_V1_CPP03_MSGPACK_TUPLE_HPP
#include "msgpack/v1/adaptor/msgpack_tuple_decl.hpp"
namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace type {
// FIXME operator==
// FIXME operator!=
<% GENERATION_LIMIT = 31 %>
template <typename T>
struct tuple_type {
typedef T type;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef const T& transparent_reference;
};
template <typename T>
struct tuple_type<T&> {
typedef T type;
typedef T& value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T& transparent_reference;
};
template <typename T>
struct tuple_type<const T&> {
typedef T type;
typedef T& value_type;
typedef T& reference;
typedef const T& const_reference;
typedef const T& transparent_reference;
};
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
<%0.upto(i) {|j|%>
template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
struct tuple_element<tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>, <%=j%>> : tuple_type<A<%=j%>> {
tuple_element(tuple<A0<%1.upto(i) {|k|%>, A<%=k%> <%}%>>& x) : m_x(x.a<%=j%>) {}
typename tuple_type<A<%=j%>>::reference get() { return m_x; }
typename tuple_type<A<%=j%>>::const_reference get() const { return m_x; }
private:
typename tuple_type<A<%=j%>>::reference m_x;
};
<%}%>
<%}%>
<%0.upto(GENERATION_LIMIT) {|i|%>
<%0.upto(i) {|j|%>
template <typename A0<%1.upto(i) {|k|%>, typename A<%=k%><%}%>>
struct const_tuple_element<tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>, <%=j%>> : tuple_type<A<%=j%>> {
const_tuple_element(const tuple<A0<%1.upto(i) {|k|%>, A<%=k%><%}%>>& x) : m_x(x.a<%=j%>) {}
typename tuple_type<A<%=j%>>::const_reference get() const { return m_x; }
private:
typename tuple_type<A<%=j%>>::const_reference m_x;
};
<%}%>
<%}%>
/// @endcond
template <>
struct tuple<> {
tuple() {}
tuple(msgpack::object const& o) { o.convert(*this); }
typedef tuple<> value_type;
std::size_t size() const { return 0; }
};
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
std::size_t size() const { return <%=i+1%>; }
tuple() {}
tuple(typename tuple_type<A0>::transparent_reference _a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference _a<%=j%><%}%>) :
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
tuple(msgpack::object const& o) { o.convert(*this); }
template <int N> typename tuple_element<value_type, N>::reference get()
{ return tuple_element<value_type, N>(*this).get(); }
template <int N> typename const_tuple_element<value_type, N>::const_reference get() const
{ return const_tuple_element<value_type, N>(*this).get(); }
<%0.upto(i) {|j|%>
A<%=j%> a<%=j%>;<%}%>
};
template <int N, typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline typename type::tuple_element<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>::reference get(type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& t)
{ return t.template get<N>(); }
template <int N, typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline typename type::const_tuple_element<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>, N>::const_reference get(type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> const& t)
{ return t.template get<N>(); }
<%}%>
/// @endcond
inline tuple<> make_tuple()
{
return tuple<>();
}
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_tuple(typename tuple_type<A0>::transparent_reference a0<%1.upto(i) {|j|%>, typename tuple_type<A<%=j%>>::transparent_reference a<%=j%><%}%>)
{
return tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
}
<%}%>
/// @endcond
} // namespace type
namespace adaptor {
template <>
struct convert<type::tuple<> > {
msgpack::object const& operator()(
msgpack::object const& o,
type::tuple<>&) const {
if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
return o;
}
};
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct convert<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> > {
msgpack::object const& operator()(
msgpack::object const& o,
type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) const {
if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
<%0.upto(i) {|j|%>
// In order to avoid clang++'s invalid warning, msgpack::object:: has been added.
if(o.via.array.size > <%=j%>)
o.via.array.ptr[<%=j%>].msgpack::object::convert<typename type::tuple_type<A<%=j%>>::type>(v.template get<<%=j%>>());<%}%>
return o;
}
};
<%}%>
/// @endcond
template <>
struct pack<type::tuple<> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& o,
const type::tuple<>&) const {
o.pack_array(0);
return o;
}
};
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct pack<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> > {
template <typename Stream>
msgpack::packer<Stream>& operator()(
msgpack::packer<Stream>& o,
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) const {
o.pack_array(<%=i+1%>);
<%0.upto(i) {|j|%>
o.pack(v.template get<<%=j%>>());<%}%>
return o;
}
};
<%}%>
/// @endcond
template <>
struct object_with_zone<type::tuple<> > {
void operator()(
msgpack::object::with_zone& o,
const type::tuple<>&) const {
o.type = msgpack::type::ARRAY;
o.via.array.ptr = MSGPACK_NULLPTR;
o.via.array.size = 0;
}
};
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct object_with_zone<type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> > {
void operator()(
msgpack::object::with_zone& o,
const type::tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>& v) const {
o.type = msgpack::type::ARRAY;
o.via.array.ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*<%=i+1%>, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
o.via.array.size = <%=i+1%>;
<%0.upto(i) {|j|%>
o.via.array.ptr[<%=j%>] = msgpack::object(v.template get<<%=j%>>(), o.zone);<%}%>
}
};
<%}%>
/// @endcond
} // namespace adaptor
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // MSGPACK_V1_CPP03_MSGPACK_TUPLE_HPP