|
| 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 |
0 commit comments