|
| 1 | +// |
| 2 | +// MessagePack for C++ static resolution routine |
| 3 | +// |
| 4 | +// Copyright (C) 2008-2009 FURUHASHI Sadayuki |
| 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_ARRAY_REF_HPP |
| 19 | +#define MSGPACK_TYPE_ARRAY_REF_HPP |
| 20 | + |
| 21 | +#include "msgpack/versioning.hpp" |
| 22 | +#include "msgpack/adaptor/adaptor_base.hpp" |
| 23 | +#include "msgpack/adaptor/check_container_size.hpp" |
| 24 | +#include <cstring> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +namespace msgpack { |
| 28 | + |
| 29 | +/// @cond |
| 30 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 31 | +/// @endcond |
| 32 | + |
| 33 | +namespace type { |
| 34 | + |
| 35 | +template <typename T> |
| 36 | +struct array_ref { |
| 37 | + array_ref() : data(nullptr) {} |
| 38 | + array_ref(T& t) : data(&t) {} |
| 39 | + |
| 40 | + T* data; |
| 41 | + |
| 42 | + template <typename U> |
| 43 | + bool operator==(array_ref<U> const& t) const { |
| 44 | + return *data == *t.data; |
| 45 | + } |
| 46 | + template <typename U> |
| 47 | + bool operator!=(array_ref<U> const& t) const { |
| 48 | + return !(*data == *t.data); |
| 49 | + } |
| 50 | + template <typename U> |
| 51 | + bool operator< (array_ref<U> const& t) const |
| 52 | + { |
| 53 | + return *data < *t.data; |
| 54 | + } |
| 55 | + template <typename U> |
| 56 | + bool operator> (array_ref<U> const& t) const |
| 57 | + { |
| 58 | + return *t.data < *data; |
| 59 | + } |
| 60 | + template <typename U> |
| 61 | + bool operator<= (array_ref<U> const& t) const |
| 62 | + { |
| 63 | + return !(*t.data < *data); |
| 64 | + } |
| 65 | + template <typename U> |
| 66 | + bool operator>= (array_ref<U> const& t) const |
| 67 | + { |
| 68 | + return !(*data < *t.data); |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +template <typename T> |
| 73 | +inline array_ref<T const> make_array_ref(T const& t) { |
| 74 | + return array_ref<T const>(t); |
| 75 | +} |
| 76 | + |
| 77 | +template <typename T> |
| 78 | +inline array_ref<T> make_array_ref(T& t) { |
| 79 | + return array_ref<T>(t); |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +} // namespace type |
| 84 | + |
| 85 | +namespace adaptor { |
| 86 | + |
| 87 | +template <typename T> |
| 88 | +struct convert<msgpack::type::array_ref<T> > { |
| 89 | + msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<T>& v) const { |
| 90 | + if (!v.data) { throw msgpack::type_error(); } |
| 91 | + if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
| 92 | + if (v.data->size() < o.via.bin.size) { throw msgpack::type_error(); } |
| 93 | + if (o.via.array.size > 0) { |
| 94 | + msgpack::object* p = o.via.array.ptr; |
| 95 | + msgpack::object* const pend = o.via.array.ptr + o.via.array.size; |
| 96 | + typename T::iterator it = v.data->begin(); |
| 97 | + do { |
| 98 | + p->convert(*it); |
| 99 | + ++p; |
| 100 | + ++it; |
| 101 | + } while(p < pend); |
| 102 | + } |
| 103 | + return o; |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +template <typename T> |
| 108 | +struct convert<msgpack::type::array_ref<std::vector<T> > > { |
| 109 | + msgpack::object const& operator()(msgpack::object const& o, msgpack::type::array_ref<std::vector<T> >& v) const { |
| 110 | + if (!v.data) { throw msgpack::type_error(); } |
| 111 | + if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
| 112 | + v.data->resize(o.via.bin.size); |
| 113 | + if (o.via.array.size > 0) { |
| 114 | + msgpack::object* p = o.via.array.ptr; |
| 115 | + msgpack::object* const pend = o.via.array.ptr + o.via.array.size; |
| 116 | + typename std::vector<T>::iterator it = v.data->begin(); |
| 117 | + do { |
| 118 | + p->convert(*it); |
| 119 | + ++p; |
| 120 | + ++it; |
| 121 | + } while(p < pend); |
| 122 | + } |
| 123 | + return o; |
| 124 | + } |
| 125 | +}; |
| 126 | + |
| 127 | +template <typename T> |
| 128 | +struct pack<msgpack::type::array_ref<T> > { |
| 129 | + template <typename Stream> |
| 130 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const msgpack::type::array_ref<T>& v) const { |
| 131 | + if (!v.data) { throw msgpack::type_error(); } |
| 132 | + uint32_t size = checked_get_container_size(v.data->size()); |
| 133 | + o.pack_array(size); |
| 134 | + for (typename T::const_iterator it(v.data->begin()), it_end(v.data->end()); |
| 135 | + it != it_end; ++it) { |
| 136 | + o.pack(*it); |
| 137 | + } |
| 138 | + return o; |
| 139 | + } |
| 140 | +}; |
| 141 | + |
| 142 | +template <typename T> |
| 143 | +struct object_with_zone<msgpack::type::array_ref<T> > { |
| 144 | + void operator()(msgpack::object::with_zone& o, const msgpack::type::array_ref<T>& v) const { |
| 145 | + if (!v.data) { throw msgpack::type_error(); } |
| 146 | + o.type = msgpack::type::ARRAY; |
| 147 | + if (v.data->empty()) { |
| 148 | + o.via.array.ptr = nullptr; |
| 149 | + o.via.array.size = 0; |
| 150 | + } |
| 151 | + else { |
| 152 | + uint32_t size = checked_get_container_size(v.data->size()); |
| 153 | + msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size)); |
| 154 | + msgpack::object* const pend = p + size; |
| 155 | + o.via.array.ptr = p; |
| 156 | + o.via.array.size = size; |
| 157 | + typename T::const_iterator it(v.data->begin()); |
| 158 | + do { |
| 159 | +#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__) |
| 160 | +#pragma GCC diagnostic push |
| 161 | +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 162 | +#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__) |
| 163 | + *p = msgpack::object(*it, o.zone); |
| 164 | +#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__) |
| 165 | +#pragma GCC diagnostic pop |
| 166 | +#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) && !defined(__clang__) |
| 167 | + ++p; |
| 168 | + ++it; |
| 169 | + } while(p < pend); |
| 170 | + } |
| 171 | + } |
| 172 | +}; |
| 173 | + |
| 174 | +} // namespace adaptor |
| 175 | + |
| 176 | +/// @cond |
| 177 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 178 | +/// @endcond |
| 179 | + |
| 180 | +} // namespace msgpack |
| 181 | + |
| 182 | +#endif // MSGPACK_TYPE_ARRAY_REF_HPP |
0 commit comments