Added std::forward_list support.

Fixed cmake filename typo.
This commit is contained in:
Takatoshi Kondo
2014-08-10 23:45:50 +09:00
parent e217e72c22
commit ea23bf843e
6 changed files with 86 additions and 1 deletions

View File

@@ -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

View File

@@ -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 <forward_list>
#include "msgpack/object.hpp"
#include "msgpack/cpp_config.hpp"
namespace msgpack {
template <typename T>
inline std::forward_list<T>& operator>> (object const& o, std::forward_list<T>& 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 <typename Stream, typename T>
inline packer<Stream>& operator<< (packer<Stream>& o, const std::forward_list<T>& v)
{
o.pack_array(std::distance(v.begin(), v.end()));
for(auto const& e : v) o.pack(e);
return o;
}
template <typename T>
inline void operator<< (object::with_zone& o, const std::forward_list<T>& v)
{
o.type = type::ARRAY;
if(v.empty()) {
o.via.array.ptr = nullptr;
o.via.array.size = 0;
} else {
object* p = static_cast<object*>(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

View File

@@ -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)

View File

@@ -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 \

View File

@@ -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
)

View File

@@ -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<int> 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<int> val2 = ret.get().as<forward_list<int> >();
EXPECT_EQ(val1, val2);
EXPECT_FALSE(ret.referenced());
}
}
class TestEnumClassMemberClass
{