Skip to content

Commit 6771302

Browse files
committed
Merge branch 'redboltz-add_define_map'
2 parents 6253796 + 188c0a9 commit 6771302

14 files changed

+3873
-483
lines changed

CMakeLists.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ IF (MSGPACK_ENABLE_CXX)
118118
include/msgpack/adaptor/cpp11/unordered_set.hpp
119119
include/msgpack/adaptor/define.hpp
120120
include/msgpack/adaptor/deque.hpp
121-
include/msgpack/adaptor/detail/cpp03_define.hpp
121+
include/msgpack/adaptor/detail/cpp03_define_array.hpp
122+
include/msgpack/adaptor/detail/cpp03_define_map.hpp
122123
include/msgpack/adaptor/detail/cpp03_msgpack_tuple.hpp
123-
include/msgpack/adaptor/detail/cpp11_define.hpp
124+
include/msgpack/adaptor/detail/cpp11_define_array.hpp
125+
include/msgpack/adaptor/detail/cpp11_define_map.hpp
124126
include/msgpack/adaptor/detail/cpp11_msgpack_tuple.hpp
125127
include/msgpack/adaptor/ext.hpp
126128
include/msgpack/adaptor/fixint.hpp

erb/cpp03_define.hpp.erb renamed to erb/cpp03_define_array.hpp.erb

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@
1515
// See the License for the specific language governing permissions and
1616
// limitations under the License.
1717
//
18-
#ifndef MSGPACK_CPP03_DEFINE_HPP
19-
#define MSGPACK_CPP03_DEFINE_HPP
18+
#ifndef MSGPACK_CPP03_DEFINE_ARRAY_HPP
19+
#define MSGPACK_CPP03_DEFINE_ARRAY_HPP
2020

2121
#include "msgpack/versioning.hpp"
2222
#include "msgpack/adaptor/msgpack_tuple.hpp"
2323
#include "msgpack/adaptor/adaptor_base.hpp"
2424
#include "msgpack/object_fwd.hpp"
2525

26-
#define MSGPACK_DEFINE(...) \
26+
#define MSGPACK_DEFINE_ARRAY(...) \
2727
template <typename Packer> \
2828
void msgpack_pack(Packer& pk) const \
2929
{ \
30-
msgpack::type::make_define(__VA_ARGS__).msgpack_pack(pk); \
30+
msgpack::type::make_define_array(__VA_ARGS__).msgpack_pack(pk); \
3131
} \
3232
void msgpack_unpack(msgpack::object const& o) \
3333
{ \
34-
msgpack::type::make_define(__VA_ARGS__).msgpack_unpack(o); \
34+
msgpack::type::make_define_array(__VA_ARGS__).msgpack_unpack(o); \
3535
}\
3636
template <typename MSGPACK_OBJECT> \
3737
void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone& z) const \
3838
{ \
39-
msgpack::type::make_define(__VA_ARGS__).msgpack_object(o, z); \
39+
msgpack::type::make_define_array(__VA_ARGS__).msgpack_object(o, z); \
4040
}
4141

42-
#define MSGPACK_BASE(base) (*const_cast<base *>(static_cast<base const*>(this)))
42+
#define MSGPACK_BASE_ARRAY(base) (*const_cast<base *>(static_cast<base const*>(this)))
4343

4444
// MSGPACK_ADD_ENUM must be used in the global namespace.
4545
#define MSGPACK_ADD_ENUM(enum_name) \
@@ -91,12 +91,12 @@ namespace type {
9191
/// @cond
9292
<% GENERATION_LIMIT = 31 %>
9393
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
94-
struct define;
94+
struct define_array;
9595
/// @endcond
9696

9797
template <>
98-
struct define<> {
99-
typedef define<> value_type;
98+
struct define_array<> {
99+
typedef define_array<> value_type;
100100
typedef tuple<> tuple_type;
101101
template <typename Packer>
102102
void msgpack_pack(Packer& pk) const
@@ -118,10 +118,10 @@ struct define<> {
118118
/// @cond
119119
<%0.upto(GENERATION_LIMIT) {|i|%>
120120
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
121-
struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
122-
typedef define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
121+
struct define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
122+
typedef define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
123123
typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> tuple_type;
124-
define(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
124+
define_array(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
125125
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
126126
template <typename Packer>
127127
void msgpack_pack(Packer& pk) const
@@ -156,17 +156,17 @@ struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
156156
<%}%>
157157
/// @endcond
158158

159-
inline define<> make_define()
159+
inline define_array<> make_define_array()
160160
{
161-
return define<>();
161+
return define_array<>();
162162
}
163163

164164
/// @cond
165165
<%0.upto(GENERATION_LIMIT) {|i|%>
166166
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
167-
inline define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
167+
inline define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_array(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
168168
{
169-
return define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
169+
return define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
170170
}
171171
<%}%>
172172
/// @endcond
@@ -178,4 +178,4 @@ inline define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i)
178178
} // namespace msgpack
179179

180180

181-
#endif // MSGPACK_CPP03_DEFINE_HPP
181+
#endif // MSGPACK_CPP03_DEFINE_ARRAY_HPP

erb/cpp03_define_map.hpp.erb

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 2015 KONDO Takatoshi
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
//
18+
#ifndef MSGPACK_CPP03_DEFINE_MAP_HPP
19+
#define MSGPACK_CPP03_DEFINE_MAP_HPP
20+
21+
#if defined(MSGPACK_USE_BOOST)
22+
23+
// BOOST_PP_VARIADICS is defined in boost/preprocessor/config/config.hpp
24+
// http://www.boost.org/libs/preprocessor/doc/ref/variadics.html
25+
// However, supporting compiler detection is not complete. msgpack-c requires
26+
// variadic macro arguments support. So BOOST_PP_VARIADICS is defined here explicitly.
27+
#define BOOST_PP_VARIADICS
28+
#include <boost/preprocessor.hpp>
29+
30+
#include "msgpack/versioning.hpp"
31+
#include "msgpack/adaptor/msgpack_tuple.hpp"
32+
#include "msgpack/adaptor/adaptor_base.hpp"
33+
#include "msgpack/object_fwd.hpp"
34+
35+
#define MSGPACK_DEFINE_MAP_EACH_PROC(r, data, elem) \
36+
BOOST_PP_IF( \
37+
BOOST_PP_IS_BEGIN_PARENS(elem), \
38+
elem, \
39+
(BOOST_PP_STRINGIZE(elem))(elem) \
40+
)
41+
42+
#define MSGPACK_DEFINE_MAP_IMPL(...) \
43+
BOOST_PP_SEQ_TO_TUPLE( \
44+
BOOST_PP_SEQ_FOR_EACH( \
45+
MSGPACK_DEFINE_MAP_EACH_PROC, \
46+
0, \
47+
BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) \
48+
) \
49+
)
50+
51+
#define MSGPACK_DEFINE_MAP(...) \
52+
template <typename Packer> \
53+
void msgpack_pack(Packer& pk) const \
54+
{ \
55+
msgpack::type::make_define_map \
56+
MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
57+
.msgpack_pack(pk); \
58+
} \
59+
void msgpack_unpack(msgpack::object const& o) \
60+
{ \
61+
msgpack::type::make_define_map \
62+
MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
63+
.msgpack_unpack(o); \
64+
}\
65+
template <typename MSGPACK_OBJECT> \
66+
void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone& z) const \
67+
{ \
68+
msgpack::type::make_define_map \
69+
MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
70+
.msgpack_object(o, z); \
71+
}
72+
73+
#define MSGPACK_BASE_MAP(base) \
74+
(BOOST_PP_STRINGIZE(base))(*const_cast<base *>(static_cast<base const*>(this)))
75+
76+
namespace msgpack {
77+
/// @cond
78+
MSGPACK_API_VERSION_NAMESPACE(v1) {
79+
/// @endcond
80+
namespace type {
81+
82+
/// @cond
83+
<% GENERATION_LIMIT = 31 %>
84+
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
85+
struct define_map;
86+
/// @endcond
87+
88+
template <>
89+
struct define_map<> {
90+
template <typename Packer>
91+
void msgpack_pack(Packer& pk) const
92+
{
93+
pk.pack_map(0);
94+
}
95+
void msgpack_unpack(msgpack::object const& o) const
96+
{
97+
if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
98+
}
99+
void msgpack_object(msgpack::object* o, msgpack::zone&) const
100+
{
101+
o->type = msgpack::type::MAP;
102+
o->via.map.ptr = nullptr;
103+
o->via.map.size = 0;
104+
}
105+
};
106+
107+
/// @cond
108+
<%1.step(GENERATION_LIMIT+1,2) {|i|%>
109+
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
110+
struct define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
111+
define_map(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
112+
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
113+
template <typename Packer>
114+
void msgpack_pack(Packer& pk) const
115+
{
116+
pk.pack_map(<%=(i+1)/2%>);
117+
<%0.upto(i) {|j|%>
118+
pk.pack(a<%=j%>);<%}%>
119+
}
120+
void msgpack_unpack(msgpack::object const& o) const
121+
{
122+
if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
123+
std::map<std::string, msgpack::object const*> kvmap;
124+
for (uint32_t i = 0; i < o.via.map.size; ++i) {
125+
kvmap.insert(
126+
std::map<std::string, msgpack::object const*>::value_type(
127+
std::string(
128+
o.via.map.ptr[i].key.via.str.ptr,
129+
o.via.map.ptr[i].key.via.str.size),
130+
&o.via.map.ptr[i].val
131+
)
132+
);
133+
}
134+
<%0.step(i,2) {|j|%>
135+
{
136+
std::map<std::string, msgpack::object const*>::const_iterator it = kvmap.find(a<%=j%>);
137+
if (it != kvmap.end()) {
138+
it->second->convert(a<%=j+1%>);
139+
}
140+
}
141+
<%}%>
142+
}
143+
void msgpack_object(msgpack::object* o, msgpack::zone& z) const
144+
{
145+
o->type = msgpack::type::MAP;
146+
o->via.map.ptr = static_cast<msgpack::object_kv*>(z.allocate_align(sizeof(msgpack::object_kv)*<%=(i+1)/2%>));
147+
o->via.map.size = <%=(i+1)/2%>;
148+
<%0.step(i,2) {|j|%>
149+
o->via.map.ptr[<%=j/2%>].key = msgpack::object(a<%=j%>, z);
150+
o->via.map.ptr[<%=j/2%>].val = msgpack::object(a<%=j+1%>, z);
151+
<%}%>
152+
}
153+
<%0.upto(i) {|j|%>
154+
A<%=j%>& a<%=j%>;<%}%>
155+
};
156+
<%}%>
157+
/// @endcond
158+
159+
inline define_map<> make_define_map()
160+
{
161+
return define_map<>();
162+
}
163+
164+
/// @cond
165+
<%0.upto(GENERATION_LIMIT) {|i|%>
166+
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
167+
inline define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_map(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
168+
{
169+
return define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
170+
}
171+
<%}%>
172+
/// @endcond
173+
174+
} // namespace type
175+
/// @cond
176+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
177+
/// @endcond
178+
} // namespace msgpack
179+
180+
#endif // defined(MSGPACK_USE_BOOST)
181+
182+
#endif // MSGPACK_CPP03_DEFINE_MAP_HPP

include/msgpack/adaptor/define.hpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,19 @@
2121
#include "msgpack/cpp_config.hpp"
2222

2323
#if defined(MSGPACK_USE_CPP03)
24-
#include "detail/cpp03_define.hpp"
24+
#include "detail/cpp03_define_array.hpp"
25+
#include "detail/cpp03_define_map.hpp"
2526
#else // MSGPACK_USE_CPP03
26-
#include "detail/cpp11_define.hpp"
27+
#include "detail/cpp11_define_array.hpp"
28+
#include "detail/cpp11_define_map.hpp"
2729
#endif // MSGPACK_USE_CPP03
2830

31+
#if defined(MSGPACK_USE_DEFINE_MAP)
32+
#define MSGPACK_DEFINE MSGPACK_DEFINE_MAP
33+
#define MSGPACK_BASE MSGPACK_BASE_MAP
34+
#else // defined(MSGPACK_USE_DEFINE_MAP)
35+
#define MSGPACK_DEFINE MSGPACK_DEFINE_ARRAY
36+
#define MSGPACK_BASE MSGPACK_BASE_ARRAY
37+
#endif // defined(MSGPACK_USE_DEFINE_MAP)
38+
2939
#endif // MSGPACK_DEFINE_HPP

0 commit comments

Comments
 (0)