1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2014-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_VECTOR_CHAR_HPP
19 #define MSGPACK_TYPE_VECTOR_CHAR_HPP
20
21 #include "rpc/msgpack/versioning.hpp"
22 #include "rpc/msgpack/adaptor/adaptor_base.hpp"
23 #include "rpc/msgpack/adaptor/check_container_size.hpp"
24
25 #include <vector>
26
27 namespace clmdep_msgpack {
28
29 /// @cond
MSGPACK_API_VERSION_NAMESPACE(v1)30 MSGPACK_API_VERSION_NAMESPACE(v1) {
31 /// @endcond
32
33 namespace adaptor {
34
35 template <typename Alloc>
36 struct convert<std::vector<char, Alloc> > {
37 clmdep_msgpack::object const& operator()(clmdep_msgpack::object const& o, std::vector<char, Alloc>& v) const {
38 switch (o.type) {
39 case clmdep_msgpack::type::BIN:
40 v.resize(o.via.bin.size);
41 std::memcpy(&v.front(), o.via.bin.ptr, o.via.bin.size);
42 break;
43 case clmdep_msgpack::type::STR:
44 v.resize(o.via.str.size);
45 std::memcpy(&v.front(), o.via.str.ptr, o.via.str.size);
46 break;
47 default:
48 throw clmdep_msgpack::type_error();
49 break;
50 }
51 return o;
52 }
53 };
54
55 template <typename Alloc>
56 struct pack<std::vector<char, Alloc> > {
57 template <typename Stream>
58 clmdep_msgpack::packer<Stream>& operator()(clmdep_msgpack::packer<Stream>& o, const std::vector<char, Alloc>& v) const {
59 uint32_t size = checked_get_container_size(v.size());
60 o.pack_bin(size);
61 o.pack_bin_body(&v.front(), size);
62
63 return o;
64 }
65 };
66
67 template <typename Alloc>
68 struct object<std::vector<char, Alloc> > {
69 void operator()(clmdep_msgpack::object& o, const std::vector<char, Alloc>& v) const {
70 uint32_t size = checked_get_container_size(v.size());
71 o.type = clmdep_msgpack::type::BIN;
72 o.via.bin.ptr = &v.front();
73 o.via.bin.size = size;
74 }
75 };
76
77 template <typename Alloc>
78 struct object_with_zone<std::vector<char, Alloc> > {
79 void operator()(clmdep_msgpack::object::with_zone& o, const std::vector<char, Alloc>& v) const {
80 uint32_t size = checked_get_container_size(v.size());
81 o.type = clmdep_msgpack::type::BIN;
82 char* ptr = static_cast<char*>(o.zone.allocate_align(size));
83 o.via.bin.ptr = ptr;
84 o.via.bin.size = size;
85 std::memcpy(ptr, &v.front(), size);
86 }
87 };
88
89 } // namespace adaptor
90
91 /// @cond
92 } // MSGPACK_API_VERSION_NAMESPACE(v1)
93 /// @endcond
94
95 } // namespace clmdep_msgpack
96
97 #endif // MSGPACK_TYPE_VECTOR_CHAR_HPP
98