1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2014 KONDO-2015 Takatoshi
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10
11 #ifndef MSGPACK_V1_TYPE_CPP11_FORWARD_LIST_HPP
12 #define MSGPACK_V1_TYPE_CPP11_FORWARD_LIST_HPP
13
14 #include "msgpack/versioning.hpp"
15 #include "msgpack/adaptor/adaptor_base.hpp"
16 #include "msgpack/adaptor/check_container_size.hpp"
17
18 #include <forward_list>
19
20 namespace msgpack {
21
22 /// @cond
MSGPACK_API_VERSION_NAMESPACE(v1)23 MSGPACK_API_VERSION_NAMESPACE(v1) {
24 /// @endcond
25
26 namespace adaptor {
27
28 template <typename T, typename Alloc>
29 struct as<std::forward_list<T, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
30 std::forward_list<T, Alloc> operator()(msgpack::object const& o) const {
31 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
32 std::forward_list<T, Alloc> v;
33 msgpack::object* p = o.via.array.ptr + o.via.array.size;
34 msgpack::object* const pend = o.via.array.ptr;
35 while (p != pend) {
36 --p;
37 v.push_front(p->as<T>());
38 }
39 return v;
40 }
41 };
42
43 template <typename T, typename Alloc>
44 struct convert<std::forward_list<T, Alloc>> {
45 msgpack::object const& operator()(msgpack::object const& o, std::forward_list<T, Alloc>& v) const {
46 if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
47 v.resize(o.via.array.size);
48 msgpack::object* p = o.via.array.ptr;
49 for (auto &e : v) {
50 p->convert(e);
51 ++p;
52 }
53 return o;
54 }
55 };
56
57 template <typename T, typename Alloc>
58 struct pack<std::forward_list<T, Alloc>> {
59 template <typename Stream>
60 msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::forward_list<T, Alloc>& v) const {
61 uint32_t size = checked_get_container_size(std::distance(v.begin(), v.end()));
62 o.pack_array(size);
63 for(auto const& e : v) o.pack(e);
64 return o;
65 }
66 };
67
68 template <typename T, typename Alloc>
69 struct object_with_zone<std::forward_list<T, Alloc>> {
70 void operator()(msgpack::object::with_zone& o, const std::forward_list<T, Alloc>& v) const {
71 o.type = msgpack::type::ARRAY;
72 if(v.empty()) {
73 o.via.array.ptr = MSGPACK_NULLPTR;
74 o.via.array.size = 0;
75 } else {
76 uint32_t size = checked_get_container_size(std::distance(v.begin(), v.end()));
77 o.via.array.size = size;
78 msgpack::object* p = static_cast<msgpack::object*>(
79 o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
80 o.via.array.ptr = p;
81 for(auto const& e : v) *p++ = msgpack::object(e, o.zone);
82 }
83 }
84 };
85
86 } // namespace adaptor
87
88 /// @cond
89 } // MSGPACK_API_VERSION_NAMESPACE(v1)
90 /// @endcond
91
92 } // namespace msgpack
93
94 #endif // MSGPACK_V1_TYPE_CPP11_FORWARD_LIST_HPP
95