MessagePack for C++
Loading...
Searching...
No Matches
forward_list.hpp
Go to the documentation of this file.
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
16#include "msgpack/object.hpp"
18
19#include <forward_list>
20
21namespace msgpack {
22
26
27namespace adaptor {
28
29template <typename T, typename Alloc>
30 struct as<std::forward_list<T, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
31 std::forward_list<T, Alloc> operator()(msgpack::object const& o) const {
32 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
33 std::forward_list<T, Alloc> v;
35 msgpack::object* const pend = o.via.array.ptr;
36 while (p != pend) {
37 --p;
38 v.push_front(p->as<T>());
39 }
40 return v;
41 }
42};
43
44template <typename T, typename Alloc>
45struct convert<std::forward_list<T, Alloc>> {
46 msgpack::object const& operator()(msgpack::object const& o, std::forward_list<T, Alloc>& v) const {
47 if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
48 v.resize(o.via.array.size);
50 for (auto &e : v) {
51 p->convert(e);
52 ++p;
53 }
54 return o;
55 }
56};
57
58template <typename T, typename Alloc>
59struct pack<std::forward_list<T, Alloc>> {
60 template <typename Stream>
61 msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::forward_list<T, Alloc>& v) const {
62 uint32_t size = checked_get_container_size(std::distance(v.begin(), v.end()));
63 o.pack_array(size);
64 for(auto const& e : v) o.pack(e);
65 return o;
66 }
67};
68
69template <typename T, typename Alloc>
70struct object_with_zone<std::forward_list<T, Alloc>> {
71 void operator()(msgpack::object::with_zone& o, const std::forward_list<T, Alloc>& v) const {
72 o.type = msgpack::type::ARRAY;
73 if(v.empty()) {
75 o.via.array.size = 0;
76 } else {
77 uint32_t size = checked_get_container_size(std::distance(v.begin(), v.end()));
78 o.via.array.size = size;
79 msgpack::object* p = static_cast<msgpack::object*>(
81 o.via.array.ptr = p;
82 for(auto const& e : v) *p++ = msgpack::object(e, o.zone);
83 }
84 }
85};
86
87} // namespace adaptor
88
90} // MSGPACK_API_VERSION_NAMESPACE(v1)
92
93} // namespace msgpack
94
95#endif // MSGPACK_V1_TYPE_CPP11_FORWARD_LIST_HPP
The class template that supports continuous packing.
Definition pack.hpp:33
packer< Stream > & pack_array(uint32_t n)
Packing array header and size.
Definition pack.hpp:1195
packer< Stream > & pack(const T &v)
Packing function template.
Definition object_fwd.hpp:231
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition cpp03_zone.hpp:255
Definition adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition check_container_size.hpp:55
std::forward_list< T, Alloc > operator()(msgpack::object const &o) const
Definition forward_list.hpp:31
Definition object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::forward_list< T, Alloc > &v) const
Definition forward_list.hpp:46
Definition adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::forward_list< T, Alloc > &v) const
Definition forward_list.hpp:71
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::forward_list< T, Alloc > &v) const
Definition forward_list.hpp:61
Definition adaptor_base.hpp:32
Definition object.hpp:35
msgpack::zone & zone
Definition object.hpp:37
uint32_t size
Definition object_fwd.hpp:23
msgpack::object * ptr
Definition object_fwd.hpp:24
Object class that corresponding to MessagePack format object.
Definition object_fwd.hpp:75
msgpack::enable_if<!msgpack::is_array< T >::value &&!msgpack::is_pointer< T >::value, T & >::type convert(T &v) const
Convert the object.
Definition object.hpp:1076
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition object.hpp:1126
union_type via
Definition object_fwd.hpp:93
msgpack::type::object_type type
Definition object_fwd.hpp:92
msgpack::object_array array
Definition object_fwd.hpp:85
#define MSGPACK_NULLPTR
Definition cpp_config_decl.hpp:85
#define MSGPACK_ZONE_ALIGNOF(type)
Definition cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition versioning.hpp:66