diff --git a/CMakeLists.txt b/CMakeLists.txt index c064fbad..eca59386 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -152,6 +152,8 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/adaptor/tr1/unordered_set_fwd.hpp include/msgpack/adaptor/vector.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_fwd.hpp include/msgpack/cpp_config.hpp diff --git a/include/msgpack/adaptor/vector_bool.hpp b/include/msgpack/adaptor/vector_bool.hpp new file mode 100644 index 00000000..1db8c217 --- /dev/null +++ b/include/msgpack/adaptor/vector_bool.hpp @@ -0,0 +1,80 @@ +// +// 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 + +namespace msgpack { + +MSGPACK_API_VERSION_NAMESPACE(v1) { + +inline object const& operator>> (object const& o, std::vector& 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::iterator it = v.begin(), end = v.end(); + it != end; + ++it) { + *it = p->as(); + ++p; + } + } + return o; +} + +template +inline packer& operator<< (packer& o, const std::vector& v) +{ + o.pack_array(v.size()); + for(std::vector::const_iterator it(v.begin()), it_end(v.end()); + it != it_end; ++it) { + o.pack(static_cast(*it)); + } + return o; +} + +inline void operator<< (object::with_zone& o, const std::vector& v) +{ + o.type = type::ARRAY; + if(v.empty()) { + o.via.array.ptr = nullptr; + o.via.array.size = 0; + } else { + object* p = static_cast(o.zone.allocate_align(sizeof(object)*v.size())); + object* const pend = p + v.size(); + o.via.array.ptr = p; + o.via.array.size = v.size(); + std::vector::const_iterator it(v.begin()); + do { + *p = object(static_cast(*it), o.zone); + ++p; + ++it; + } while(p < pend); + } +} + +} // MSGPACK_API_VERSION_NAMESPACE(v1) + +} // namespace msgpack + +#endif // MSGPACK_TYPE_VECTOR_BOOL_HPP diff --git a/include/msgpack/adaptor/vector_bool_fwd.hpp b/include/msgpack/adaptor/vector_bool_fwd.hpp new file mode 100644 index 00000000..17433c25 --- /dev/null +++ b/include/msgpack/adaptor/vector_bool_fwd.hpp @@ -0,0 +1,40 @@ +// +// 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" +#include + +namespace msgpack { + +MSGPACK_API_VERSION_NAMESPACE(v1) { + +object const& operator>> (object const& o, std::vector& v); + +template +packer& operator<< (packer& o, const std::vector& v); + +void operator<< (object::with_zone& o, const std::vector& v); + +} // MSGPACK_API_VERSION_NAMESPACE(v1) + +} // namespace msgpack + +#endif // MSGPACK_TYPE_VECTOR_BOOL_FWD_HPP diff --git a/include/msgpack/object.hpp b/include/msgpack/object.hpp index cc3de43f..379be1b0 100644 --- a/include/msgpack/object.hpp +++ b/include/msgpack/object.hpp @@ -38,6 +38,7 @@ #include "msgpack/adaptor/set_fwd.hpp" #include "msgpack/adaptor/string_fwd.hpp" #include "msgpack/adaptor/vector_fwd.hpp" +#include "msgpack/adaptor/vector_bool_fwd.hpp" #include "msgpack/adaptor/vector_char_fwd.hpp" #if defined(MSGPACK_USE_CPP03) diff --git a/include/msgpack/type.hpp b/include/msgpack/type.hpp index e2963fab..73f5f6c8 100644 --- a/include/msgpack/type.hpp +++ b/include/msgpack/type.hpp @@ -13,6 +13,7 @@ #include "adaptor/set.hpp" #include "adaptor/string.hpp" #include "adaptor/vector.hpp" +#include "adaptor/vector_bool.hpp" #include "adaptor/vector_char.hpp" #include "adaptor/msgpack_tuple.hpp" #include "adaptor/define.hpp" diff --git a/src/Makefile.am b/src/Makefile.am index 9e87e0d1..e960b47f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -108,6 +108,8 @@ nobase_include_HEADERS += \ ../include/msgpack/adaptor/tr1/unordered_set_fwd.hpp \ ../include/msgpack/adaptor/vector.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_fwd.hpp \ ../include/msgpack/cpp_config.hpp \ diff --git a/test/msgpack_container.cpp b/test/msgpack_container.cpp index 0b95d032..c9a7ce81 100644 --- a/test/msgpack_container.cpp +++ b/test/msgpack_container.cpp @@ -80,6 +80,22 @@ TEST(MSGPACK_STL, simple_buffer_vector_char) } } +TEST(MSGPACK_STL, simple_buffer_vector_bool) +{ + vector val1; + for (unsigned int i = 0; i < kElements; i++) + val1.push_back(i % 2 ? false : true); + 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 val2 = ret.get().as >(); + EXPECT_EQ(val1.size(), val2.size()); + EXPECT_TRUE(equal(val1.begin(), val1.end(), val2.begin())); +} + + TEST(MSGPACK_STL, simple_buffer_map) { for (unsigned int k = 0; k < kLoop; k++) {