Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add define map #306

Merged
merged 2 commits into from
Jul 6, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,11 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/cpp11/unordered_set.hpp
include/msgpack/adaptor/define.hpp
include/msgpack/adaptor/deque.hpp
include/msgpack/adaptor/detail/cpp03_define.hpp
include/msgpack/adaptor/detail/cpp03_define_array.hpp
include/msgpack/adaptor/detail/cpp03_define_map.hpp
include/msgpack/adaptor/detail/cpp03_msgpack_tuple.hpp
include/msgpack/adaptor/detail/cpp11_define.hpp
include/msgpack/adaptor/detail/cpp11_define_array.hpp
include/msgpack/adaptor/detail/cpp11_define_map.hpp
include/msgpack/adaptor/detail/cpp11_msgpack_tuple.hpp
include/msgpack/adaptor/fixint.hpp
include/msgpack/adaptor/float.hpp
Expand Down
36 changes: 18 additions & 18 deletions erb/cpp03_define.hpp.erb → erb/cpp03_define_array.hpp.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
182 changes: 182 additions & 0 deletions erb/cpp03_define_map.hpp.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
//
// 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)

// BOOST_PP_VARIADICS is defined in boost/preprocessor/config/config.hpp
// http://www.boost.org/libs/preprocessor/doc/ref/variadics.html
// However, supporting compiler detection is not complete. msgpack-c requires
// variadic macro arguments support. So BOOST_PP_VARIADICS is defined here explicitly.
#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
14 changes: 12 additions & 2 deletions include/msgpack/adaptor/define.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@
#include "msgpack/cpp_config.hpp"

#if defined(MSGPACK_USE_CPP03)
#include "detail/cpp03_define.hpp"
#include "detail/cpp03_define_array.hpp"
#include "detail/cpp03_define_map.hpp"
#else // MSGPACK_USE_CPP03
#include "detail/cpp11_define.hpp"
#include "detail/cpp11_define_array.hpp"
#include "detail/cpp11_define_map.hpp"
#endif // MSGPACK_USE_CPP03

#if defined(MSGPACK_USE_DEFINE_MAP)
#define MSGPACK_DEFINE MSGPACK_DEFINE_MAP
#define MSGPACK_BASE MSGPACK_BASE_MAP
#else // defined(MSGPACK_USE_DEFINE_MAP)
#define MSGPACK_DEFINE MSGPACK_DEFINE_ARRAY
#define MSGPACK_BASE MSGPACK_BASE_ARRAY
#endif // defined(MSGPACK_USE_DEFINE_MAP)

#endif // MSGPACK_DEFINE_HPP