Added map-based class pack/convert support.

This commit is contained in:
Takatoshi Kondo
2015-07-03 11:44:07 +09:00
parent e0a2c2a4bf
commit 777e5f13f3
14 changed files with 3858 additions and 483 deletions

View File

@@ -15,31 +15,31 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef MSGPACK_CPP03_DEFINE_HPP
#define MSGPACK_CPP03_DEFINE_HPP
#ifndef MSGPACK_CPP03_DEFINE_ARRAY_HPP
#define MSGPACK_CPP03_DEFINE_ARRAY_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/msgpack_tuple.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/object_fwd.hpp"
#define MSGPACK_DEFINE(...) \
#define MSGPACK_DEFINE_ARRAY(...) \
template <typename Packer> \
void msgpack_pack(Packer& pk) const \
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_pack(pk); \
msgpack::type::make_define_array(__VA_ARGS__).msgpack_pack(pk); \
} \
void msgpack_unpack(msgpack::object const& o) \
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_unpack(o); \
msgpack::type::make_define_array(__VA_ARGS__).msgpack_unpack(o); \
}\
template <typename MSGPACK_OBJECT> \
void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone& z) const \
{ \
msgpack::type::make_define(__VA_ARGS__).msgpack_object(o, z); \
msgpack::type::make_define_array(__VA_ARGS__).msgpack_object(o, z); \
}
#define MSGPACK_BASE(base) (*const_cast<base *>(static_cast<base const*>(this)))
#define MSGPACK_BASE_ARRAY(base) (*const_cast<base *>(static_cast<base const*>(this)))
// MSGPACK_ADD_ENUM must be used in the global namespace.
#define MSGPACK_ADD_ENUM(enum_name) \
@@ -91,12 +91,12 @@ namespace type {
/// @cond
<% GENERATION_LIMIT = 31 %>
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
struct define;
struct define_array;
/// @endcond
template <>
struct define<> {
typedef define<> value_type;
struct define_array<> {
typedef define_array<> value_type;
typedef tuple<> tuple_type;
template <typename Packer>
void msgpack_pack(Packer& pk) const
@@ -118,10 +118,10 @@ struct define<> {
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
typedef define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
struct define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
typedef define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> value_type;
typedef tuple<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> tuple_type;
define(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
define_array(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
template <typename Packer>
void msgpack_pack(Packer& pk) const
@@ -156,17 +156,17 @@ struct define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
<%}%>
/// @endcond
inline define<> make_define()
inline define_array<> make_define_array()
{
return define<>();
return define_array<>();
}
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
inline define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_array(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
{
return define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
return define_array<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
}
<%}%>
/// @endcond
@@ -178,4 +178,4 @@ inline define<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define(A0& a0<%1.upto(i)
} // namespace msgpack
#endif // MSGPACK_CPP03_DEFINE_HPP
#endif // MSGPACK_CPP03_DEFINE_ARRAY_HPP

View File

@@ -0,0 +1,178 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef MSGPACK_CPP03_DEFINE_MAP_HPP
#define MSGPACK_CPP03_DEFINE_MAP_HPP
#if defined(MSGPACK_USE_BOOST)
#define BOOST_PP_VARIADICS
#include <boost/preprocessor.hpp>
#include "msgpack/versioning.hpp"
#include "msgpack/adaptor/msgpack_tuple.hpp"
#include "msgpack/adaptor/adaptor_base.hpp"
#include "msgpack/object_fwd.hpp"
#define MSGPACK_DEFINE_MAP_EACH_PROC(r, data, elem) \
BOOST_PP_IF( \
BOOST_PP_IS_BEGIN_PARENS(elem), \
elem, \
(BOOST_PP_STRINGIZE(elem))(elem) \
)
#define MSGPACK_DEFINE_MAP_IMPL(...) \
BOOST_PP_SEQ_TO_TUPLE( \
BOOST_PP_SEQ_FOR_EACH( \
MSGPACK_DEFINE_MAP_EACH_PROC, \
0, \
BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__) \
) \
)
#define MSGPACK_DEFINE_MAP(...) \
template <typename Packer> \
void msgpack_pack(Packer& pk) const \
{ \
msgpack::type::make_define_map \
MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
.msgpack_pack(pk); \
} \
void msgpack_unpack(msgpack::object const& o) \
{ \
msgpack::type::make_define_map \
MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
.msgpack_unpack(o); \
}\
template <typename MSGPACK_OBJECT> \
void msgpack_object(MSGPACK_OBJECT* o, msgpack::zone& z) const \
{ \
msgpack::type::make_define_map \
MSGPACK_DEFINE_MAP_IMPL(__VA_ARGS__) \
.msgpack_object(o, z); \
}
#define MSGPACK_BASE_MAP(base) \
(BOOST_PP_STRINGIZE(base))(*const_cast<base *>(static_cast<base const*>(this)))
namespace msgpack {
/// @cond
MSGPACK_API_VERSION_NAMESPACE(v1) {
/// @endcond
namespace type {
/// @cond
<% GENERATION_LIMIT = 31 %>
template <typename A0 = void<%1.upto(GENERATION_LIMIT+1) {|i|%>, typename A<%=i%> = void<%}%>>
struct define_map;
/// @endcond
template <>
struct define_map<> {
template <typename Packer>
void msgpack_pack(Packer& pk) const
{
pk.pack_map(0);
}
void msgpack_unpack(msgpack::object const& o) const
{
if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
}
void msgpack_object(msgpack::object* o, msgpack::zone&) const
{
o->type = msgpack::type::MAP;
o->via.map.ptr = nullptr;
o->via.map.size = 0;
}
};
/// @cond
<%1.step(GENERATION_LIMIT+1,2) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
struct define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> {
define_map(A0& _a0<%1.upto(i) {|j|%>, A<%=j%>& _a<%=j%><%}%>) :
a0(_a0)<%1.upto(i) {|j|%>, a<%=j%>(_a<%=j%>)<%}%> {}
template <typename Packer>
void msgpack_pack(Packer& pk) const
{
pk.pack_map(<%=(i+1)/2%>);
<%0.upto(i) {|j|%>
pk.pack(a<%=j%>);<%}%>
}
void msgpack_unpack(msgpack::object const& o) const
{
if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
std::map<std::string, msgpack::object const*> kvmap;
for (uint32_t i = 0; i < o.via.map.size; ++i) {
kvmap.insert(
std::map<std::string, msgpack::object const*>::value_type(
std::string(
o.via.map.ptr[i].key.via.str.ptr,
o.via.map.ptr[i].key.via.str.size),
&o.via.map.ptr[i].val
)
);
}
<%0.step(i,2) {|j|%>
{
std::map<std::string, msgpack::object const*>::const_iterator it = kvmap.find(a<%=j%>);
if (it != kvmap.end()) {
it->second->convert(a<%=j+1%>);
}
}
<%}%>
}
void msgpack_object(msgpack::object* o, msgpack::zone& z) const
{
o->type = msgpack::type::MAP;
o->via.map.ptr = static_cast<msgpack::object_kv*>(z.allocate_align(sizeof(msgpack::object_kv)*<%=(i+1)/2%>));
o->via.map.size = <%=(i+1)/2%>;
<%0.step(i,2) {|j|%>
o->via.map.ptr[<%=j/2%>].key = msgpack::object(a<%=j%>, z);
o->via.map.ptr[<%=j/2%>].val = msgpack::object(a<%=j+1%>, z);
<%}%>
}
<%0.upto(i) {|j|%>
A<%=j%>& a<%=j%>;<%}%>
};
<%}%>
/// @endcond
inline define_map<> make_define_map()
{
return define_map<>();
}
/// @cond
<%0.upto(GENERATION_LIMIT) {|i|%>
template <typename A0<%1.upto(i) {|j|%>, typename A<%=j%><%}%>>
inline define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>> make_define_map(A0& a0<%1.upto(i) {|j|%>, A<%=j%>& a<%=j%><%}%>)
{
return define_map<A0<%1.upto(i) {|j|%>, A<%=j%><%}%>>(a0<%1.upto(i) {|j|%>, a<%=j%><%}%>);
}
<%}%>
/// @endcond
} // namespace type
/// @cond
} // MSGPACK_API_VERSION_NAMESPACE(v1)
/// @endcond
} // namespace msgpack
#endif // defined(MSGPACK_USE_BOOST)
#endif // MSGPACK_CPP03_DEFINE_MAP_HPP