|
| 1 | +// |
| 2 | +// MessagePack for C++ static resolution routine |
| 3 | +// |
| 4 | +// Copyright (C) 2018 KONDO Takatoshi |
| 5 | +// |
| 6 | +// Distributed under the Boost Software License, Version 1.0. |
| 7 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 8 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 9 | +// |
| 10 | +#ifndef MSGPACK_V1_TYPE_WSTRING_HPP |
| 11 | +#define MSGPACK_V1_TYPE_WSTRING_HPP |
| 12 | + |
| 13 | +#include "msgpack/versioning.hpp" |
| 14 | +#include "msgpack/adaptor/adaptor_base.hpp" |
| 15 | +#include "msgpack/adaptor/check_container_size.hpp" |
| 16 | + |
| 17 | +#include <vector> |
| 18 | + |
| 19 | +namespace msgpack { |
| 20 | + |
| 21 | +/// @cond |
| 22 | +MSGPACK_API_VERSION_NAMESPACE(v1) { |
| 23 | +/// @endcond |
| 24 | + |
| 25 | +namespace adaptor { |
| 26 | + |
| 27 | +#if !defined(MSGPACK_USE_CPP03) |
| 28 | + |
| 29 | +template <> |
| 30 | +struct as<std::wstring> { |
| 31 | + std::wstring operator()(const msgpack::object& o) const { |
| 32 | + if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
| 33 | + std::wstring v; |
| 34 | + v.reserve(o.via.array.size); |
| 35 | + if (o.via.array.size > 0) { |
| 36 | + msgpack::object* p = o.via.array.ptr; |
| 37 | + msgpack::object* const pend = o.via.array.ptr + o.via.array.size; |
| 38 | + do { |
| 39 | + v.push_back(p->as<wchar_t>()); |
| 40 | + ++p; |
| 41 | + } while (p < pend); |
| 42 | + } |
| 43 | + return v; |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +#endif // !defined(MSGPACK_USE_CPP03) |
| 48 | + |
| 49 | +template <> |
| 50 | +struct convert<std::wstring> { |
| 51 | + msgpack::object const& operator()(msgpack::object const& o, std::wstring& v) const { |
| 52 | + if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
| 53 | + v.resize(o.via.array.size); |
| 54 | + if (o.via.array.size > 0) { |
| 55 | + msgpack::object* p = o.via.array.ptr; |
| 56 | + msgpack::object* const pend = o.via.array.ptr + o.via.array.size; |
| 57 | + std::wstring::iterator it = v.begin(); |
| 58 | + do { |
| 59 | + p->convert(*it); |
| 60 | + ++p; |
| 61 | + ++it; |
| 62 | + } while(p < pend); |
| 63 | + } |
| 64 | + return o; |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +template <> |
| 69 | +struct pack<std::wstring> { |
| 70 | + template <typename Stream> |
| 71 | + msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::wstring& v) const { |
| 72 | + uint32_t size = checked_get_container_size(v.size()); |
| 73 | + o.pack_array(size); |
| 74 | + for (std::wstring::const_iterator it(v.begin()), it_end(v.end()); |
| 75 | + it != it_end; ++it) { |
| 76 | + o.pack(*it); |
| 77 | + } |
| 78 | + return o; |
| 79 | + } |
| 80 | +}; |
| 81 | + |
| 82 | +template <> |
| 83 | +struct object_with_zone<std::wstring> { |
| 84 | + void operator()(msgpack::object::with_zone& o, const std::wstring& v) const { |
| 85 | + o.type = msgpack::type::ARRAY; |
| 86 | + if (v.empty()) { |
| 87 | + o.via.array.ptr = MSGPACK_NULLPTR; |
| 88 | + o.via.array.size = 0; |
| 89 | + } |
| 90 | + else { |
| 91 | + uint32_t size = checked_get_container_size(v.size()); |
| 92 | + msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object))); |
| 93 | + msgpack::object* const pend = p + size; |
| 94 | + o.via.array.ptr = p; |
| 95 | + o.via.array.size = size; |
| 96 | + std::wstring::const_iterator it(v.begin()); |
| 97 | + do { |
| 98 | +#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 99 | +#pragma GCC diagnostic push |
| 100 | +#pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 101 | +#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 102 | + *p = msgpack::object(*it, o.zone); |
| 103 | +#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 104 | +#pragma GCC diagnostic pop |
| 105 | +#endif // (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
| 106 | + ++p; |
| 107 | + ++it; |
| 108 | + } while(p < pend); |
| 109 | + } |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +} // namespace adaptor |
| 114 | + |
| 115 | +/// @cond |
| 116 | +} // MSGPACK_API_VERSION_NAMESPACE(v1) |
| 117 | +/// @endcond |
| 118 | + |
| 119 | +} // namespace msgpack |
| 120 | + |
| 121 | +#endif // MSGPACK_V1_TYPE_WSTRING_HPP |
0 commit comments