From 64ac09c4925719e7f0db7079470b7ceec3234c2c Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sun, 25 Jan 2015 21:59:39 +0900 Subject: [PATCH 1/5] Fixed #198. Added a conversion support from msgpack::object to std::vector. --- CMakeLists.txt | 2 + include/msgpack/adaptor/vector_bool.hpp | 49 +++++++++++++++++++++ include/msgpack/adaptor/vector_bool_fwd.hpp | 34 ++++++++++++++ include/msgpack/object.hpp | 1 + include/msgpack/type.hpp | 1 + src/Makefile.am | 2 + test/msgpack_container.cpp | 16 +++++++ 7 files changed, 105 insertions(+) create mode 100644 include/msgpack/adaptor/vector_bool.hpp create mode 100644 include/msgpack/adaptor/vector_bool_fwd.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5755f368..960a818e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -151,6 +151,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..34950f09 --- /dev/null +++ b/include/msgpack/adaptor/vector_bool.hpp @@ -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 + +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; +} + +} // 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..e5016c1c --- /dev/null +++ b/include/msgpack/adaptor/vector_bool_fwd.hpp @@ -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& 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 7b9280fd..1ce6ec48 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 fe950eaf..9e03ce61 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -107,6 +107,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 6d231c29..b28babdd 100644 --- a/test/msgpack_container.cpp +++ b/test/msgpack_container.cpp @@ -55,6 +55,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); + 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++) { From c3518c06662b49e29fcaa00f3533b7d6af1acc30 Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Sun, 25 Jan 2015 22:25:35 +0900 Subject: [PATCH 2/5] Included vector header. --- include/msgpack/adaptor/vector_bool_fwd.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/msgpack/adaptor/vector_bool_fwd.hpp b/include/msgpack/adaptor/vector_bool_fwd.hpp index e5016c1c..8d477ed9 100644 --- a/include/msgpack/adaptor/vector_bool_fwd.hpp +++ b/include/msgpack/adaptor/vector_bool_fwd.hpp @@ -20,6 +20,7 @@ #include "msgpack/versioning.hpp" #include "msgpack/object_fwd.hpp" +#include namespace msgpack { From 118cf7270cfe709b86afebe0edc65ee076182f9c Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Tue, 10 Mar 2015 18:37:36 +0900 Subject: [PATCH 3/5] Fixed a compile error on clang++ 3.4.1 on FreeBSD and Yosemite. --- include/msgpack/adaptor/vector_bool.hpp | 31 +++++++++++++++++++++ include/msgpack/adaptor/vector_bool_fwd.hpp | 5 ++++ 2 files changed, 36 insertions(+) diff --git a/include/msgpack/adaptor/vector_bool.hpp b/include/msgpack/adaptor/vector_bool.hpp index 34950f09..db5b7e42 100644 --- a/include/msgpack/adaptor/vector_bool.hpp +++ b/include/msgpack/adaptor/vector_bool.hpp @@ -42,6 +42,37 @@ inline object const& operator>> (object const& o, std::vector& v) 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(*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(*it, o.zone); + ++p; + ++it; + } while(p < pend); + } +} + } // MSGPACK_API_VERSION_NAMESPACE(v1) } // namespace msgpack diff --git a/include/msgpack/adaptor/vector_bool_fwd.hpp b/include/msgpack/adaptor/vector_bool_fwd.hpp index 8d477ed9..17433c25 100644 --- a/include/msgpack/adaptor/vector_bool_fwd.hpp +++ b/include/msgpack/adaptor/vector_bool_fwd.hpp @@ -28,6 +28,11 @@ 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 From 2d5c68055cff66ab55df5aa85173d09ac201316a Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Tue, 10 Mar 2015 19:04:23 +0900 Subject: [PATCH 4/5] Added explicit cast to vector internal type. --- include/msgpack/adaptor/vector_bool.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/msgpack/adaptor/vector_bool.hpp b/include/msgpack/adaptor/vector_bool.hpp index db5b7e42..1db8c217 100644 --- a/include/msgpack/adaptor/vector_bool.hpp +++ b/include/msgpack/adaptor/vector_bool.hpp @@ -48,7 +48,7 @@ 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(*it); + o.pack(static_cast(*it)); } return o; } @@ -66,7 +66,7 @@ inline void operator<< (object::with_zone& o, const std::vector& v) o.via.array.size = v.size(); std::vector::const_iterator it(v.begin()); do { - *p = object(*it, o.zone); + *p = object(static_cast(*it), o.zone); ++p; ++it; } while(p < pend); From eb765d21f2ed682ff2f102e7212a83e890f859ad Mon Sep 17 00:00:00 2001 From: Takatoshi Kondo Date: Tue, 10 Mar 2015 19:04:51 +0900 Subject: [PATCH 5/5] Fixed MSVC++ warning about int to bool conversion. --- test/msgpack_container.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/msgpack_container.cpp b/test/msgpack_container.cpp index b28babdd..86bb8150 100644 --- a/test/msgpack_container.cpp +++ b/test/msgpack_container.cpp @@ -59,7 +59,7 @@ TEST(MSGPACK_STL, simple_buffer_vector_bool) { vector val1; for (unsigned int i = 0; i < kElements; i++) - val1.push_back(i % 2); + val1.push_back(i % 2 ? false : true); msgpack::sbuffer sbuf; msgpack::pack(sbuf, val1); msgpack::unpacked ret;