|
| 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_TYPE_BOOST_FUSION_HPP |
| 19 | +#define MSGPACK_TYPE_BOOST_FUSION_HPP |
| 20 | + |
| 21 | +#include "msgpack/versioning.hpp" |
| 22 | +#include "msgpack/adaptor/adaptor_base.hpp" |
| 23 | +#include "msgpack/adaptor/check_container_size.hpp" |
| 24 | + |
| 25 | +#include <boost/fusion/support/is_sequence.hpp> |
| 26 | +#include <boost/fusion/sequence/intrinsic/size.hpp> |
| 27 | +#include <boost/fusion/algorithm/iteration/for_each.hpp> |
| 28 | + |
| 29 | +namespace msgpack { |
| 30 | + |
| 31 | +/// @cond |
| 32 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 33 | +/// @endcond |
| 34 | + |
| 35 | +namespace adaptor { |
| 36 | + |
| 37 | +template <typename T> |
| 38 | +struct convert<T, typename msgpack::enable_if<boost::fusion::traits::is_sequence<T>::value>::type > { |
| 39 | + msgpack::object const& operator()(msgpack::object const& o, T& v) const { |
| 40 | + if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
| 41 | + if(o.via.array.size != checked_get_container_size(boost::fusion::size(v))) { |
| 42 | + throw msgpack::type_error(); |
| 43 | + } |
| 44 | + uint32_t index = 0; |
| 45 | + boost::fusion::for_each(v, convert_imp(o, index)); |
| 46 | + return o; |
| 47 | + } |
| 48 | +private: |
| 49 | + struct convert_imp { |
| 50 | + convert_imp(msgpack::object const& obj, uint32_t& index):obj_(obj), index_(index) {} |
| 51 | + template <typename U> |
| 52 | + void operator()(U& v) const { |
| 53 | + msgpack::adaptor::convert<U>()(obj_.via.array.ptr[index_++], v); |
| 54 | + } |
| 55 | + private: |
| 56 | + msgpack::object const& obj_; |
| 57 | + uint32_t& index_; |
| 58 | + }; |
| 59 | +}; |
| 60 | + |
| 61 | +template <typename T> |
| 62 | +struct pack<T, typename msgpack::enable_if<boost::fusion::traits::is_sequence<T>::value>::type > { |
| 63 | + template <typename Stream> |
| 64 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const T& v) const { |
| 65 | + uint32_t size = checked_get_container_size(boost::fusion::size(v)); |
| 66 | + o.pack_array(size); |
| 67 | + boost::fusion::for_each(v, pack_imp<Stream>(o)); |
| 68 | + return o; |
| 69 | + } |
| 70 | +private: |
| 71 | + template <typename Stream> |
| 72 | + struct pack_imp { |
| 73 | + pack_imp(msgpack::packer<Stream>& stream):stream_(stream) {} |
| 74 | + template <typename U> |
| 75 | + void operator()(U const& v) const { |
| 76 | + stream_.pack(v); |
| 77 | + } |
| 78 | + private: |
| 79 | + msgpack::packer<Stream>& stream_; |
| 80 | + }; |
| 81 | +}; |
| 82 | + |
| 83 | +template <typename T> |
| 84 | +struct object_with_zone<T, typename msgpack::enable_if<boost::fusion::traits::is_sequence<T>::value>::type > { |
| 85 | + void operator()(msgpack::object::with_zone& o, const T& v) const { |
| 86 | + uint32_t size = checked_get_container_size(boost::fusion::size(v)); |
| 87 | + o.type = msgpack::type::ARRAY; |
| 88 | + o.via.array.ptr = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size)); |
| 89 | + o.via.array.size = size; |
| 90 | + uint32_t count = 0; |
| 91 | + boost::fusion::for_each(v, with_zone_imp(o, count)); |
| 92 | + } |
| 93 | +private: |
| 94 | + struct with_zone_imp { |
| 95 | + with_zone_imp(msgpack::object::with_zone const& obj, uint32_t& count):obj_(obj), count_(count) {} |
| 96 | + template <typename U> |
| 97 | + void operator()(U const& v) const { |
| 98 | + obj_.via.array.ptr[count_++] = msgpack::object(v, obj_.zone); |
| 99 | + } |
| 100 | + msgpack::object::with_zone const& obj_; |
| 101 | + uint32_t& count_; |
| 102 | + }; |
| 103 | +}; |
| 104 | + |
| 105 | +} // namespace adaptor |
| 106 | + |
| 107 | +/// @cond |
| 108 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 109 | +/// @endcond |
| 110 | + |
| 111 | +} // namespace msgpack |
| 112 | + |
| 113 | +#endif // MSGPACK_TYPE_BOOST_FUSION_HPP |
0 commit comments