/src/msgpack-c/include/msgpack/v1/adaptor/wstring.hpp
Line | Count | Source |
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/object.hpp" |
16 | | #include "msgpack/adaptor/check_container_size.hpp" |
17 | | |
18 | | #include <vector> |
19 | | |
20 | | namespace msgpack { |
21 | | |
22 | | /// @cond |
23 | | MSGPACK_API_VERSION_NAMESPACE(v1) { |
24 | | /// @endcond |
25 | | |
26 | | namespace adaptor { |
27 | | |
28 | | #if !defined(MSGPACK_USE_CPP03) |
29 | | |
30 | | template <> |
31 | | struct as<std::wstring> { |
32 | 0 | std::wstring operator()(const msgpack::object& o) const { |
33 | 0 | if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
34 | 0 | std::wstring v; |
35 | 0 | v.reserve(o.via.array.size); |
36 | 0 | if (o.via.array.size > 0) { |
37 | 0 | msgpack::object* p = o.via.array.ptr; |
38 | 0 | msgpack::object* const pend = o.via.array.ptr + o.via.array.size; |
39 | 0 | do { |
40 | 0 | v.push_back(p->as<wchar_t>()); |
41 | 0 | ++p; |
42 | 0 | } while (p < pend); |
43 | 0 | } |
44 | 0 | return v; |
45 | 0 | } |
46 | | }; |
47 | | |
48 | | #endif // !defined(MSGPACK_USE_CPP03) |
49 | | |
50 | | template <> |
51 | | struct convert<std::wstring> { |
52 | 0 | msgpack::object const& operator()(msgpack::object const& o, std::wstring& v) const { |
53 | 0 | if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); } |
54 | 0 | v.resize(o.via.array.size); |
55 | 0 | if (o.via.array.size > 0) { |
56 | 0 | msgpack::object* p = o.via.array.ptr; |
57 | 0 | msgpack::object* const pend = o.via.array.ptr + o.via.array.size; |
58 | 0 | std::wstring::iterator it = v.begin(); |
59 | 0 | do { |
60 | 0 | p->convert(*it); |
61 | 0 | ++p; |
62 | 0 | ++it; |
63 | 0 | } while(p < pend); |
64 | 0 | } |
65 | 0 | return o; |
66 | 0 | } |
67 | | }; |
68 | | |
69 | | template <> |
70 | | struct pack<std::wstring> { |
71 | | template <typename Stream> |
72 | | msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::wstring& v) const { |
73 | | uint32_t size = checked_get_container_size(v.size()); |
74 | | o.pack_array(size); |
75 | | for (std::wstring::const_iterator it(v.begin()), it_end(v.end()); |
76 | | it != it_end; ++it) { |
77 | | o.pack(*it); |
78 | | } |
79 | | return o; |
80 | | } |
81 | | }; |
82 | | |
83 | | template <> |
84 | | struct object_with_zone<std::wstring> { |
85 | 0 | void operator()(msgpack::object::with_zone& o, const std::wstring& v) const { |
86 | 0 | o.type = msgpack::type::ARRAY; |
87 | 0 | if (v.empty()) { |
88 | 0 | o.via.array.ptr = MSGPACK_NULLPTR; |
89 | 0 | o.via.array.size = 0; |
90 | 0 | } |
91 | 0 | else { |
92 | 0 | uint32_t size = checked_get_container_size(v.size()); |
93 | 0 | msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object))); |
94 | 0 | msgpack::object* const pend = p + size; |
95 | 0 | o.via.array.ptr = p; |
96 | 0 | o.via.array.size = size; |
97 | 0 | std::wstring::const_iterator it(v.begin()); |
98 | 0 | do { |
99 | 0 | #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
100 | 0 | #pragma GCC diagnostic push |
101 | 0 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
102 | 0 | #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
103 | 0 | *p = msgpack::object(*it, o.zone); |
104 | 0 | #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
105 | 0 | #pragma GCC diagnostic pop |
106 | 0 | #endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__) |
107 | 0 | ++p; |
108 | 0 | ++it; |
109 | 0 | } while(p < pend); |
110 | 0 | } |
111 | 0 | } |
112 | | }; |
113 | | |
114 | | } // namespace adaptor |
115 | | |
116 | | /// @cond |
117 | | } // MSGPACK_API_VERSION_NAMESPACE(v1) |
118 | | /// @endcond |
119 | | |
120 | | } // namespace msgpack |
121 | | |
122 | | #endif // MSGPACK_V1_TYPE_WSTRING_HPP |