MessagePack for C++
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1//
2// MessagePack for C++ static resolution routine
3//
4// Copyright (C) 2014-2015 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
11#ifndef MSGPACK_V1_TYPE_CPP11_ARRAY_HPP
12#define MSGPACK_V1_TYPE_CPP11_ARRAY_HPP
13
16#include "msgpack/object.hpp"
18#include "msgpack/meta.hpp"
19
20#include <array>
21
22namespace msgpack {
23
27
28namespace adaptor {
29
30namespace detail {
31
32namespace array {
33
34template<typename T, std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
35inline std::array<T, N1+N2> concat(
36 std::array<T, N1>&& a1,
37 std::array<T, N2>&& a2,
40 return {{ std::move(a1[I1])..., std::move(a2[I2])... }};
41}
42
43template<typename T, std::size_t N1, std::size_t N2>
44inline std::array<T, N1+N2> concat(std::array<T, N1>&& a1, std::array<T, N2>&& a2) {
45 return concat(std::move(a1), std::move(a2), msgpack::gen_seq<N1>(), msgpack::gen_seq<N2>());
46}
47
48template <typename T, std::size_t N>
49struct as_impl {
50 static std::array<T, N> as(msgpack::object const& o) {
51 msgpack::object* p = o.via.array.ptr + N - 1;
52 return concat(as_impl<T, N-1>::as(o), std::array<T, 1>{{p->as<T>()}});
53 }
54};
55
56template <typename T>
57struct as_impl<T, 1> {
58 static std::array<T, 1> as(msgpack::object const& o) {
60 return std::array<T, 1>{{p->as<T>()}};
61 }
62};
63
64template <typename T>
65struct as_impl<T, 0> {
66 static std::array<T, 0> as(msgpack::object const&) {
67 return std::array<T, 0>();
68 }
69};
70
71} // namespace array
72
73} // namespace detail
74
75template <typename T, std::size_t N>
76struct as<std::array<T, N>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
77 std::array<T, N> operator()(msgpack::object const& o) const {
78 if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
79 if(o.via.array.size > N) { throw msgpack::type_error(); }
81 }
82};
83
84template <typename T, std::size_t N>
85struct convert<std::array<T, N>> {
86 msgpack::object const& operator()(msgpack::object const& o, std::array<T, N>& v) const {
87 if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
88 if(o.via.array.size > N) { throw msgpack::type_error(); }
89 if(o.via.array.size > 0) {
91 msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
92 T* it = &v[0];
93 do {
94 p->convert(*it);
95 ++p;
96 ++it;
97 } while(p < pend);
98 }
99 return o;
100 }
101};
102
103template <typename T, std::size_t N>
104struct pack<std::array<T, N>> {
105 template <typename Stream>
106 msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<T, N>& v) const {
107 uint32_t size = checked_get_container_size(v.size());
108 o.pack_array(size);
109 for(auto const& e : v) o.pack(e);
110 return o;
111 }
112};
113
114template <typename T, std::size_t N>
115struct object_with_zone<std::array<T, N>> {
116 void operator()(msgpack::object::with_zone& o, const std::array<T, N>& v) const {
117 o.type = msgpack::type::ARRAY;
118 if(v.empty()) {
120 o.via.array.size = 0;
121 } else {
122 uint32_t size = checked_get_container_size(v.size());
124 o.via.array.size = size;
125 o.via.array.ptr = p;
126 for (auto const& e : v) *p++ = msgpack::object(e, o.zone);
127 }
128 }
129};
130
131} // namespace adaptor
132
134} // MSGPACK_API_VERSION_NAMESPACE(v1)
136
137} // namespace msgpack
138
139#endif // MSGPACK_V1_TYPE_CPP11_ARRAY_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
std::array< T, N1+N2 > concat(std::array< T, N1 > &&a1, std::array< T, N2 > &&a2, msgpack::seq< I1... >, msgpack::seq< I2... >)
Definition array.hpp:35
Definition adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition check_container_size.hpp:55
std::array< T, N > operator()(msgpack::object const &o) const
Definition array.hpp:77
Definition object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::array< T, N > &v) const
Definition array.hpp:86
Definition adaptor_base.hpp:27
static std::array< T, 0 > as(msgpack::object const &)
Definition array.hpp:66
static std::array< T, 1 > as(msgpack::object const &o)
Definition array.hpp:58
static std::array< T, N > as(msgpack::object const &o)
Definition array.hpp:50
void operator()(msgpack::object::with_zone &o, const std::array< T, N > &v) const
Definition array.hpp:116
Definition adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::array< T, N > &v) const
Definition array.hpp:106
Definition adaptor_base.hpp:32
Definition meta.hpp:40
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
Definition meta.hpp:37
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