Skip to content

Commit 8eaa2e9

Browse files
committed
Merge pull request #199 from redboltz/fix_issue_198
Fix issue 198
2 parents ac1eb6e + eb765d2 commit 8eaa2e9

File tree

7 files changed

+142
-0
lines changed

7 files changed

+142
-0
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ IF (MSGPACK_ENABLE_CXX)
152152
include/msgpack/adaptor/tr1/unordered_set_fwd.hpp
153153
include/msgpack/adaptor/vector.hpp
154154
include/msgpack/adaptor/vector_fwd.hpp
155+
include/msgpack/adaptor/vector_bool.hpp
156+
include/msgpack/adaptor/vector_bool_fwd.hpp
155157
include/msgpack/adaptor/vector_char.hpp
156158
include/msgpack/adaptor/vector_char_fwd.hpp
157159
include/msgpack/cpp_config.hpp
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 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_BOOL_HPP
19+
#define MSGPACK_TYPE_VECTOR_BOOL_HPP
20+
21+
#include "msgpack/versioning.hpp"
22+
#include "msgpack/object_fwd.hpp"
23+
#include <vector>
24+
25+
namespace msgpack {
26+
27+
MSGPACK_API_VERSION_NAMESPACE(v1) {
28+
29+
inline object const& operator>> (object const& o, std::vector<bool>& v)
30+
{
31+
if (o.type != type::ARRAY) { throw type_error(); }
32+
if (o.via.array.size > 0) {
33+
v.resize(o.via.array.size);
34+
object* p = o.via.array.ptr;
35+
for (std::vector<bool>::iterator it = v.begin(), end = v.end();
36+
it != end;
37+
++it) {
38+
*it = p->as<bool>();
39+
++p;
40+
}
41+
}
42+
return o;
43+
}
44+
45+
template <typename Stream>
46+
inline packer<Stream>& operator<< (packer<Stream>& o, const std::vector<bool>& v)
47+
{
48+
o.pack_array(v.size());
49+
for(std::vector<bool>::const_iterator it(v.begin()), it_end(v.end());
50+
it != it_end; ++it) {
51+
o.pack(static_cast<bool>(*it));
52+
}
53+
return o;
54+
}
55+
56+
inline void operator<< (object::with_zone& o, const std::vector<bool>& v)
57+
{
58+
o.type = type::ARRAY;
59+
if(v.empty()) {
60+
o.via.array.ptr = nullptr;
61+
o.via.array.size = 0;
62+
} else {
63+
object* p = static_cast<object*>(o.zone.allocate_align(sizeof(object)*v.size()));
64+
object* const pend = p + v.size();
65+
o.via.array.ptr = p;
66+
o.via.array.size = v.size();
67+
std::vector<bool>::const_iterator it(v.begin());
68+
do {
69+
*p = object(static_cast<bool>(*it), o.zone);
70+
++p;
71+
++it;
72+
} while(p < pend);
73+
}
74+
}
75+
76+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
77+
78+
} // namespace msgpack
79+
80+
#endif // MSGPACK_TYPE_VECTOR_BOOL_HPP
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// MessagePack for C++ static resolution routine
3+
//
4+
// Copyright (C) 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_BOOL_FWD_HPP
19+
#define MSGPACK_TYPE_VECTOR_BOOL_FWD_HPP
20+
21+
#include "msgpack/versioning.hpp"
22+
#include "msgpack/object_fwd.hpp"
23+
#include <vector>
24+
25+
namespace msgpack {
26+
27+
MSGPACK_API_VERSION_NAMESPACE(v1) {
28+
29+
object const& operator>> (object const& o, std::vector<bool>& v);
30+
31+
template <typename Stream>
32+
packer<Stream>& operator<< (packer<Stream>& o, const std::vector<bool>& v);
33+
34+
void operator<< (object::with_zone& o, const std::vector<bool>& v);
35+
36+
} // MSGPACK_API_VERSION_NAMESPACE(v1)
37+
38+
} // namespace msgpack
39+
40+
#endif // MSGPACK_TYPE_VECTOR_BOOL_FWD_HPP

include/msgpack/object.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "msgpack/adaptor/set_fwd.hpp"
3939
#include "msgpack/adaptor/string_fwd.hpp"
4040
#include "msgpack/adaptor/vector_fwd.hpp"
41+
#include "msgpack/adaptor/vector_bool_fwd.hpp"
4142
#include "msgpack/adaptor/vector_char_fwd.hpp"
4243

4344
#if defined(MSGPACK_USE_CPP03)

include/msgpack/type.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "adaptor/set.hpp"
1414
#include "adaptor/string.hpp"
1515
#include "adaptor/vector.hpp"
16+
#include "adaptor/vector_bool.hpp"
1617
#include "adaptor/vector_char.hpp"
1718
#include "adaptor/msgpack_tuple.hpp"
1819
#include "adaptor/define.hpp"

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ nobase_include_HEADERS += \
108108
../include/msgpack/adaptor/tr1/unordered_set_fwd.hpp \
109109
../include/msgpack/adaptor/vector.hpp \
110110
../include/msgpack/adaptor/vector_fwd.hpp \
111+
../include/msgpack/adaptor/vector_bool.hpp \
112+
../include/msgpack/adaptor/vector_bool_fwd.hpp \
111113
../include/msgpack/adaptor/vector_char.hpp \
112114
../include/msgpack/adaptor/vector_char_fwd.hpp \
113115
../include/msgpack/cpp_config.hpp \

test/msgpack_container.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ TEST(MSGPACK_STL, simple_buffer_vector_char)
8080
}
8181
}
8282

83+
TEST(MSGPACK_STL, simple_buffer_vector_bool)
84+
{
85+
vector<bool> val1;
86+
for (unsigned int i = 0; i < kElements; i++)
87+
val1.push_back(i % 2 ? false : true);
88+
msgpack::sbuffer sbuf;
89+
msgpack::pack(sbuf, val1);
90+
msgpack::unpacked ret;
91+
msgpack::unpack(ret, sbuf.data(), sbuf.size());
92+
EXPECT_EQ(ret.get().type, msgpack::type::ARRAY);
93+
vector<bool> val2 = ret.get().as<vector<bool> >();
94+
EXPECT_EQ(val1.size(), val2.size());
95+
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
96+
}
97+
98+
8399
TEST(MSGPACK_STL, simple_buffer_map)
84100
{
85101
for (unsigned int k = 0; k < kLoop; k++) {

0 commit comments

Comments
 (0)