Skip to content

Commit 3352b2f

Browse files
committed
Merge pull request #359 from redboltz/fix_243
Fixed #243.
2 parents 364658e + 96f1458 commit 3352b2f

File tree

13 files changed

+833
-0
lines changed

13 files changed

+833
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ IF (MSGPACK_ENABLE_CXX)
164164
LIST (APPEND msgpack_HEADERS
165165
include/msgpack.hpp
166166
include/msgpack/adaptor/adaptor_base.hpp
167+
include/msgpack/adaptor/array_ref.hpp
167168
include/msgpack/adaptor/bool.hpp
168169
include/msgpack/adaptor/boost/fusion.hpp
169170
include/msgpack/adaptor/boost/optional.hpp
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_ARRAY_UNSIGNED_CHAR_HPP
19+
#define MSGPACK_TYPE_ARRAY_UNSIGNED_CHAR_HPP
20+
21+
#include "msgpack/versioning.hpp"
22+
#include "msgpack/adaptor/adaptor_base.hpp"
23+
#include "msgpack/adaptor/check_container_size.hpp"
24+
25+
#include <array>
26+
27+
namespace msgpack {
28+
29+
/// @cond
30+
MSGPACK_API_VERSION_NAMESPACE(v1) {
31+
/// @endcond
32+
33+
namespace adaptor {
34+
35+
template <std::size_t N>
36+
struct convert<std::array<unsigned char, N>> {
37+
msgpack::object const& operator()(msgpack::object const& o, std::array<unsigned char, N>& v) const {
38+
switch (o.type) {
39+
case msgpack::type::BIN:
40+
if(o.via.bin.size != N) { throw msgpack::type_error(); }
41+
std::memcpy(v.data(), o.via.bin.ptr, o.via.bin.size);
42+
break;
43+
case msgpack::type::STR:
44+
if(o.via.str.size != N) { throw msgpack::type_error(); }
45+
std::memcpy(v.data(), o.via.str.ptr, N);
46+
break;
47+
default:
48+
throw msgpack::type_error();
49+
break;
50+
}
51+
return o;
52+
}
53+
};
54+
55+
template <std::size_t N>
56+
struct pack<std::array<unsigned char, N>> {
57+
template <typename Stream>
58+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<unsigned char, N>& v) const {
59+
uint32_t size = checked_get_container_size(v.size());
60+
o.pack_bin(size);
61+
o.pack_bin_body(reinterpret_cast<char const*>(v.data()), size);
62+
63+
return o;
64+
}
65+
};
66+
67+
template <std::size_t N>
68+
struct object<std::array<unsigned char, N>> {
69+
void operator()(msgpack::object& o, const std::array<unsigned char, N>& v) const {
70+
uint32_t size = checked_get_container_size(v.size());
71+
o.type = msgpack::type::BIN;
72+
o.via.bin.ptr = reinterpret_cast<char const*>(v.data());
73+
o.via.bin.size = size;
74+
}
75+
};
76+
77+
template <std::size_t N>
78+
struct object_with_zone<std::array<unsigned char, N>> {
79+
void operator()(msgpack::object::with_zone& o, const std::array<unsigned char, N>& v) const {
80+
uint32_t size = checked_get_container_size(v.size());
81+
o.type = 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.data(), size);
86+
}
87+
};
88+
89+
} // namespace adaptor
90+
91+
/// @cond
92+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
93+
/// @endcond
94+
95+
} // namespace msgpack
96+
97+
#endif // MSGPACK_TYPE_ARRAY_UNSIGNED_CHAR_HPP
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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_UNSIGNED_CHAR_HPP
19+
#define MSGPACK_TYPE_VECTOR_UNSIGNED_CHAR_HPP
20+
21+
#include "msgpack/versioning.hpp"
22+
#include "msgpack/adaptor/adaptor_base.hpp"
23+
#include "msgpack/adaptor/check_container_size.hpp"
24+
25+
#include <vector>
26+
27+
namespace msgpack {
28+
29+
/// @cond
30+
MSGPACK_API_VERSION_NAMESPACE(v1) {
31+
/// @endcond
32+
33+
namespace adaptor {
34+
35+
template <typename Alloc>
36+
struct convert<std::vector<unsigned char, Alloc> > {
37+
msgpack::object const& operator()(msgpack::object const& o, std::vector<unsigned char, Alloc>& v) const {
38+
switch (o.type) {
39+
case 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 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 msgpack::type_error();
49+
break;
50+
}
51+
return o;
52+
}
53+
};
54+
55+
template <typename Alloc>
56+
struct pack<std::vector<unsigned char, Alloc> > {
57+
template <typename Stream>
58+
msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<unsigned char, Alloc>& v) const {
59+
uint32_t size = checked_get_container_size(v.size());
60+
o.pack_bin(size);
61+
o.pack_bin_body(reinterpret_cast<char const*>(&v.front()), size);
62+
63+
return o;
64+
}
65+
};
66+
67+
template <typename Alloc>
68+
struct object<std::vector<unsigned char, Alloc> > {
69+
void operator()(msgpack::object& o, const std::vector<unsigned char, Alloc>& v) const {
70+
uint32_t size = checked_get_container_size(v.size());
71+
o.type = msgpack::type::BIN;
72+
o.via.bin.ptr = reinterpret_cast<char const*>(&v.front());
73+
o.via.bin.size = size;
74+
}
75+
};
76+
77+
template <typename Alloc>
78+
struct object_with_zone<std::vector<unsigned char, Alloc> > {
79+
void operator()(msgpack::object::with_zone& o, const std::vector<unsigned char, Alloc>& v) const {
80+
uint32_t size = checked_get_container_size(v.size());
81+
o.type = 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 msgpack
96+
97+
#endif // MSGPACK_TYPE_VECTOR_UNSIGNED_CHAR_HPP

0 commit comments

Comments
 (0)