Added a conversion support from msgpack::object to std::vector<bool>.
This commit is contained in:
Takatoshi Kondo 2015-01-25 21:59:39 +09:00
parent 414da4e8c6
commit 64ac09c492
7 changed files with 105 additions and 0 deletions

View File

@ -151,6 +151,8 @@ IF (MSGPACK_ENABLE_CXX)
include/msgpack/adaptor/tr1/unordered_set_fwd.hpp include/msgpack/adaptor/tr1/unordered_set_fwd.hpp
include/msgpack/adaptor/vector.hpp include/msgpack/adaptor/vector.hpp
include/msgpack/adaptor/vector_fwd.hpp include/msgpack/adaptor/vector_fwd.hpp
include/msgpack/adaptor/vector_bool.hpp
include/msgpack/adaptor/vector_bool_fwd.hpp
include/msgpack/adaptor/vector_char.hpp include/msgpack/adaptor/vector_char.hpp
include/msgpack/adaptor/vector_char_fwd.hpp include/msgpack/adaptor/vector_char_fwd.hpp
include/msgpack/cpp_config.hpp include/msgpack/cpp_config.hpp

View File

@ -0,0 +1,49 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef MSGPACK_TYPE_VECTOR_BOOL_HPP
#define MSGPACK_TYPE_VECTOR_BOOL_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/object_fwd.hpp"
#include <vector>
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
inline object const& operator>> (object const& o, std::vector<bool>& v)
{
if (o.type != type::ARRAY) { throw type_error(); }
if (o.via.array.size > 0) {
v.resize(o.via.array.size);
object* p = o.via.array.ptr;
for (std::vector<bool>::iterator it = v.begin(), end = v.end();
it != end;
++it) {
*it = p->as<bool>();
++p;
}
}
return o;
}
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_TYPE_VECTOR_BOOL_HPP

View File

@ -0,0 +1,34 @@
//
// MessagePack for C++ static resolution routine
//
// Copyright (C) 2015 KONDO Takatoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#ifndef MSGPACK_TYPE_VECTOR_BOOL_FWD_HPP
#define MSGPACK_TYPE_VECTOR_BOOL_FWD_HPP
#include "msgpack/versioning.hpp"
#include "msgpack/object_fwd.hpp"
namespace msgpack {
MSGPACK_API_VERSION_NAMESPACE(v1) {
object const& operator>> (object const& o, std::vector<bool>& v);
} // MSGPACK_API_VERSION_NAMESPACE(v1)
} // namespace msgpack
#endif // MSGPACK_TYPE_VECTOR_BOOL_FWD_HPP

View File

@ -38,6 +38,7 @@
#include "msgpack/adaptor/set_fwd.hpp" #include "msgpack/adaptor/set_fwd.hpp"
#include "msgpack/adaptor/string_fwd.hpp" #include "msgpack/adaptor/string_fwd.hpp"
#include "msgpack/adaptor/vector_fwd.hpp" #include "msgpack/adaptor/vector_fwd.hpp"
#include "msgpack/adaptor/vector_bool_fwd.hpp"
#include "msgpack/adaptor/vector_char_fwd.hpp" #include "msgpack/adaptor/vector_char_fwd.hpp"
#if defined(MSGPACK_USE_CPP03) #if defined(MSGPACK_USE_CPP03)

View File

@ -13,6 +13,7 @@
#include "adaptor/set.hpp" #include "adaptor/set.hpp"
#include "adaptor/string.hpp" #include "adaptor/string.hpp"
#include "adaptor/vector.hpp" #include "adaptor/vector.hpp"
#include "adaptor/vector_bool.hpp"
#include "adaptor/vector_char.hpp" #include "adaptor/vector_char.hpp"
#include "adaptor/msgpack_tuple.hpp" #include "adaptor/msgpack_tuple.hpp"
#include "adaptor/define.hpp" #include "adaptor/define.hpp"

View File

@ -107,6 +107,8 @@ nobase_include_HEADERS += \
../include/msgpack/adaptor/tr1/unordered_set_fwd.hpp \ ../include/msgpack/adaptor/tr1/unordered_set_fwd.hpp \
../include/msgpack/adaptor/vector.hpp \ ../include/msgpack/adaptor/vector.hpp \
../include/msgpack/adaptor/vector_fwd.hpp \ ../include/msgpack/adaptor/vector_fwd.hpp \
../include/msgpack/adaptor/vector_bool.hpp \
../include/msgpack/adaptor/vector_bool_fwd.hpp \
../include/msgpack/adaptor/vector_char.hpp \ ../include/msgpack/adaptor/vector_char.hpp \
../include/msgpack/adaptor/vector_char_fwd.hpp \ ../include/msgpack/adaptor/vector_char_fwd.hpp \
../include/msgpack/cpp_config.hpp \ ../include/msgpack/cpp_config.hpp \

View File

@ -55,6 +55,22 @@ TEST(MSGPACK_STL, simple_buffer_vector_char)
} }
} }
TEST(MSGPACK_STL, simple_buffer_vector_bool)
{
vector<bool> val1;
for (unsigned int i = 0; i < kElements; i++)
val1.push_back(i % 2);
msgpack::sbuffer sbuf;
msgpack::pack(sbuf, val1);
msgpack::unpacked ret;
msgpack::unpack(ret, sbuf.data(), sbuf.size());
EXPECT_EQ(ret.get().type, msgpack::type::ARRAY);
vector<bool> val2 = ret.get().as<vector<bool> >();
EXPECT_EQ(val1.size(), val2.size());
EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin()));
}
TEST(MSGPACK_STL, simple_buffer_map) TEST(MSGPACK_STL, simple_buffer_map)
{ {
for (unsigned int k = 0; k < kLoop; k++) { for (unsigned int k = 0; k < kLoop; k++) {