MessagePack for C++
Loading...
Searching...
No Matches
vector.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2008-2015 FURUHASHI Sadayuki and KONDO 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#ifndef MSGPACK_V1_TYPE_VECTOR_HPP
11#define MSGPACK_V1_TYPE_VECTOR_HPP
12
15#include "msgpack/object.hpp"
17
18#include <vector>
19
20namespace msgpack {
21
25
26namespace adaptor {
27
28#if !defined(MSGPACK_USE_CPP03)
29
30template <typename T, typename Alloc>
31struct as<std::vector<T, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
32 std::vector<T, Alloc> operator()(const msgpack::object& o) const {
33 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
34 std::vector<T, Alloc> v;
35 v.reserve(o.via.array.size);
36 if (o.via.array.size > 0) {
37 msgpack::object* p = o.via.array.ptr;
38 msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
39 do {
40 v.push_back(p->as<T>());
41 ++p;
42 } while (p < pend);
43 }
44 return v;
45 }
46};
47
48#endif // !defined(MSGPACK_USE_CPP03)
49
50template <typename T, typename Alloc>
51struct convert<std::vector<T, Alloc> > {
52 msgpack::object const& operator()(msgpack::object const& o, std::vector<T, Alloc>& v) const {
53 if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
54 v.resize(o.via.array.size);
55 if (o.via.array.size > 0) {
56 msgpack::object* p = o.via.array.ptr;
57 msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
58 typename std::vector<T, Alloc>::iterator it = v.begin();
59 do {
60 p->convert(*it);
61 ++p;
62 ++it;
63 } while(p < pend);
64 }
65 return o;
66 }
67};
68
69template <typename T, typename Alloc>
70struct pack<std::vector<T, Alloc> > {
71 template <typename Stream>
72 msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::vector<T, Alloc>& v) const {
74 o.pack_array(size);
75 for (typename std::vector<T, Alloc>::const_iterator it(v.begin()), it_end(v.end());
76 it != it_end; ++it) {
77 o.pack(*it);
78 }
79 return o;
80 }
81};
82
83template <typename T, typename Alloc>
84struct object_with_zone<std::vector<T, Alloc> > {
85 void operator()(msgpack::object::with_zone& o, const std::vector<T, Alloc>& v) const {
86 o.type = msgpack::type::ARRAY;
87 if (v.empty()) {
88 o.via.array.ptr = MSGPACK_NULLPTR;
89 o.via.array.size = 0;
90 }
91 else {
93 msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*size, MSGPACK_ZONE_ALIGNOF(msgpack::object)));
94 msgpack::object* const pend = p + size;
95 o.via.array.ptr = p;
96 o.via.array.size = size;
97 typename std::vector<T, Alloc>::const_iterator it(v.begin());
98 do {
99#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
100#pragma GCC diagnostic push
101#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
102#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
103 *p = msgpack::object(*it, o.zone);
104#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
105#pragma GCC diagnostic pop
106#endif // defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7)) && !defined(__clang__)
107 ++p;
108 ++it;
109 } while(p < pend);
110 }
111 }
112};
113
114} // namespace adaptor
115
117} // MSGPACK_API_VERSION_NAMESPACE(v1)
119
120} // namespace msgpack
121
122#endif // MSGPACK_V1_TYPE_VECTOR_HPP
The class template that supports continuous packing.
Definition pack.hpp:33
Definition object_fwd.hpp:231
Definition adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition check_container_size.hpp:55
std::vector< T, Alloc > operator()(const msgpack::object &o) const
Definition vector.hpp:32
Definition object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::vector< T, Alloc > &v) const
Definition vector.hpp:52
Definition adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::vector< T, Alloc > &v) const
Definition vector.hpp:85
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::vector< T, Alloc > &v) const
Definition vector.hpp:72
Definition adaptor_base.hpp:32
Definition object.hpp:35
Object class that corresponding to MessagePack format object.
Definition object_fwd.hpp:75
#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