diff --git a/CMakeLists.txt b/CMakeLists.txt index d1418f04..87906e0f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -81,6 +81,7 @@ IF (MSGPACK_ENABLE_CXX) include/msgpack/adaptor/bool.hpp include/msgpack/adaptor/cpp11/array.hpp include/msgpack/adaptor/cpp11/array_char.hpp + include/msgpack/adaptor/cpp11/forward_list.hpp include/msgpack/adaptor/cpp11/tuple.hpp include/msgpack/adaptor/define.hpp include/msgpack/adaptor/deque.hpp diff --git a/include/msgpack/adaptor/cpp11/forward_list.hpp b/include/msgpack/adaptor/cpp11/forward_list.hpp new file mode 100644 index 00000000..6f9fb575 --- /dev/null +++ b/include/msgpack/adaptor/cpp11/forward_list.hpp @@ -0,0 +1,66 @@ +// +// MessagePack for C++ static resolution routine +// +// Copyright (C) 2014 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_CPP11_FORWARD_LIST_HPP +#define MSGPACK_CPP11_FORWARD_LIST_HPP + +#include + +#include "msgpack/object.hpp" +#include "msgpack/cpp_config.hpp" + +namespace msgpack { + +template +inline std::forward_list& operator>> (object const& o, std::forward_list& v) +{ + if(o.type != type::ARRAY) { throw type_error(); } + v.resize(o.via.array.size); + object* p = o.via.array.ptr; + for (auto &e : v) { + p->convert(e); + ++p; + } + return v; +} + +template +inline packer& operator<< (packer& o, const std::forward_list& v) +{ + o.pack_array(std::distance(v.begin(), v.end())); + for(auto const& e : v) o.pack(e); + return o; +} + +template +inline void operator<< (object::with_zone& o, const std::forward_list& 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())); + for(auto const& e : v) *p++ = object(e, o.zone); + } +} + + +} // namespace msgpack + +#endif // MSGPACK_CPP11_FORWARD_LIST_HPP diff --git a/include/msgpack/type.hpp b/include/msgpack/type.hpp index 0a962667..e1ef05a8 100644 --- a/include/msgpack/type.hpp +++ b/include/msgpack/type.hpp @@ -22,6 +22,7 @@ #include "adaptor/cpp11/array.hpp" #include "adaptor/cpp11/array_char.hpp" +#include "adaptor/cpp11/forward_list.hpp" #include "adaptor/cpp11/tuple.hpp" #endif // !defined(MSGPACK_USE_CPP03) diff --git a/src/Makefile.am b/src/Makefile.am index 2b03b944..37c5f776 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -55,6 +55,7 @@ nobase_include_HEADERS += \ ../include/msgpack/adaptor/bool.hpp \ ../include/msgpack/adaptor/cpp11/array.hpp \ ../include/msgpack/adaptor/cpp11/array_char.hpp \ + ../include/msgpack/adaptor/cpp11/forward_list.hpp \ ../include/msgpack/adaptor/cpp11/tuple.hpp \ ../include/msgpack/adaptor/define.hpp \ ../include/msgpack/adaptor/deque.hpp \ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a6b71df0..07db7f99 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -23,7 +23,7 @@ SET (check_PROGRAMS msgpack_tuple.cc msgpack_test.cpp msgpackc_test.cpp - msgpackc_test_cpp11.cpp + msgpack_test_cpp11.cpp reference.cc ) diff --git a/test/msgpack_test_cpp11.cpp b/test/msgpack_test_cpp11.cpp index 3ebf4611..0d13123b 100644 --- a/test/msgpack_test_cpp11.cpp +++ b/test/msgpack_test_cpp11.cpp @@ -63,6 +63,22 @@ TEST(MSGPACK_CPP11, simple_buffer_array_char) } } +TEST(MSGPACK_STL, simple_buffer_forward_list) +{ + for (unsigned int k = 0; k < kLoop; k++) { + forward_list val1; + for (unsigned int i = 0; i < kElements; i++) + val1.push_front(rand()); + msgpack::sbuffer sbuf; + msgpack::pack(sbuf, val1); + msgpack::unpacked ret; + msgpack::unpack(ret, sbuf.data(), sbuf.size()); + forward_list val2 = ret.get().as >(); + EXPECT_EQ(val1, val2); + EXPECT_FALSE(ret.referenced()); + } +} + class TestEnumClassMemberClass {