/src/msgpack-c/include/msgpack/v1/adaptor/string.hpp
Line | Count | Source |
1 | | // |
2 | | // MessagePack for C++ static resolution routine |
3 | | // |
4 | | // Copyright (C) 2008-2015 FURUHASHI Sadayuki |
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_STRING_HPP |
11 | | #define MSGPACK_V1_TYPE_STRING_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 <string> |
19 | | #include <cstring> |
20 | | |
21 | | namespace msgpack { |
22 | | |
23 | | /// @cond |
24 | | MSGPACK_API_VERSION_NAMESPACE(v1) { |
25 | | /// @endcond |
26 | | |
27 | | namespace adaptor { |
28 | | |
29 | | template <> |
30 | | struct convert<std::string> { |
31 | 0 | msgpack::object const& operator()(msgpack::object const& o, std::string& v) const { |
32 | 0 | switch (o.type) { |
33 | 0 | case msgpack::type::BIN: |
34 | 0 | v.assign(o.via.bin.ptr, o.via.bin.size); |
35 | 0 | break; |
36 | 0 | case msgpack::type::STR: |
37 | 0 | v.assign(o.via.str.ptr, o.via.str.size); |
38 | 0 | break; |
39 | 0 | default: |
40 | 0 | throw msgpack::type_error(); |
41 | 0 | break; |
42 | 0 | } |
43 | 0 | return o; |
44 | 0 | } |
45 | | }; |
46 | | |
47 | | template <> |
48 | | struct pack<std::string> { |
49 | | template <typename Stream> |
50 | | msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::string& v) const { |
51 | | uint32_t size = checked_get_container_size(v.size()); |
52 | | o.pack_str(size); |
53 | | o.pack_str_body(v.data(), size); |
54 | | return o; |
55 | | } |
56 | | }; |
57 | | |
58 | | template <> |
59 | | struct object<std::string> { |
60 | 0 | void operator()(msgpack::object& o, const std::string& v) const { |
61 | 0 | uint32_t size = checked_get_container_size(v.size()); |
62 | 0 | o.type = msgpack::type::STR; |
63 | 0 | o.via.str.ptr = v.data(); |
64 | 0 | o.via.str.size = size; |
65 | 0 | } |
66 | | }; |
67 | | |
68 | | template <> |
69 | | struct object_with_zone<std::string> { |
70 | 0 | void operator()(msgpack::object::with_zone& o, const std::string& v) const { |
71 | 0 | uint32_t size = checked_get_container_size(v.size()); |
72 | 0 | o.type = msgpack::type::STR; |
73 | 0 | char* ptr = static_cast<char*>(o.zone.allocate_align(size, MSGPACK_ZONE_ALIGNOF(char))); |
74 | 0 | o.via.str.ptr = ptr; |
75 | 0 | o.via.str.size = size; |
76 | 0 | std::memcpy(ptr, v.data(), v.size()); |
77 | 0 | } |
78 | | }; |
79 | | |
80 | | } // namespace adaptor |
81 | | |
82 | | /// @cond |
83 | | } // MSGPACK_API_VERSION_NAMESPACE(v1) |
84 | | /// @endcond |
85 | | |
86 | | } // namespace msgpack |
87 | | |
88 | | #endif // MSGPACK_V1_TYPE_STRING_HPP |