MessagePack for C++
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 
14 #include "msgpack/versioning.hpp"
17 #include "msgpack/meta.hpp"
18 
19 #include <array>
20 
21 namespace msgpack {
22 
26 
27 namespace adaptor {
28 
29 namespace detail {
30 
31 namespace array {
32 
33 template<typename T, std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
34 inline std::array<T, N1+N2> concat(
35  std::array<T, N1>&& a1,
36  std::array<T, N2>&& a2,
39  return {{ std::move(a1[I1])..., std::move(a2[I2])... }};
40 }
41 
42 template<typename T, std::size_t N1, std::size_t N2>
43 inline std::array<T, N1+N2> concat(std::array<T, N1>&& a1, std::array<T, N2>&& a2) {
44  return concat(std::move(a1), std::move(a2), msgpack::gen_seq<N1>(), msgpack::gen_seq<N2>());
45 }
46 
47 template <typename T, std::size_t N>
48 struct as_impl {
49  static std::array<T, N> as(msgpack::object const& o) {
50  msgpack::object* p = o.via.array.ptr + N - 1;
51  return concat(as_impl<T, N-1>::as(o), std::array<T, 1>{{p->as<T>()}});
52  }
53 };
54 
55 template <typename T>
56 struct as_impl<T, 1> {
57  static std::array<T, 1> as(msgpack::object const& o) {
59  return std::array<T, 1>{{p->as<T>()}};
60  }
61 };
62 
63 template <typename T>
64 struct as_impl<T, 0> {
65  static std::array<T, 0> as(msgpack::object const&) {
66  return std::array<T, 0>();
67  }
68 };
69 
70 } // namespace array
71 
72 } // namespace detail
73 
74 template <typename T, std::size_t N>
75 struct as<std::array<T, N>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
76  std::array<T, N> operator()(msgpack::object const& o) const {
77  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
78  if(o.via.array.size > N) { throw msgpack::type_error(); }
80  }
81 };
82 
83 template <typename T, std::size_t N>
84 struct convert<std::array<T, N>> {
85  msgpack::object const& operator()(msgpack::object const& o, std::array<T, N>& v) const {
86  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
87  if(o.via.array.size > N) { throw msgpack::type_error(); }
88  if(o.via.array.size > 0) {
90  msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
91  T* it = &v[0];
92  do {
93  p->convert(*it);
94  ++p;
95  ++it;
96  } while(p < pend);
97  }
98  return o;
99  }
100 };
101 
102 template <typename T, std::size_t N>
103 struct pack<std::array<T, N>> {
104  template <typename Stream>
105  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::array<T, N>& v) const {
106  uint32_t size = checked_get_container_size(v.size());
107  o.pack_array(size);
108  for(auto const& e : v) o.pack(e);
109  return o;
110  }
111 };
112 
113 template <typename T, std::size_t N>
114 struct object_with_zone<std::array<T, N>> {
115  void operator()(msgpack::object::with_zone& o, const std::array<T, N>& v) const {
117  if(v.empty()) {
119  o.via.array.size = 0;
120  } else {
121  uint32_t size = checked_get_container_size(v.size());
123  o.via.array.size = size;
124  o.via.array.ptr = p;
125  for (auto const& e : v) *p++ = msgpack::object(e, o.zone);
126  }
127  }
128 };
129 
130 } // namespace adaptor
131 
133 } // MSGPACK_API_VERSION_NAMESPACE(v1)
135 
136 } // namespace msgpack
137 
138 #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:256
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:34
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
@ ARRAY
Definition: object_fwd_decl.hpp:40
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:76
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::array< T, N > &v) const
Definition: array.hpp:85
Definition: adaptor_base.hpp:27
static std::array< T, 0 > as(msgpack::object const &)
Definition: array.hpp:65
static std::array< T, 1 > as(msgpack::object const &o)
Definition: array.hpp:57
static std::array< T, N > as(msgpack::object const &o)
Definition: array.hpp:49
void operator()(msgpack::object::with_zone &o, const std::array< T, N > &v) const
Definition: array.hpp:115
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::array< T, N > &v) const
Definition: array.hpp:105
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
std::enable_if< msgpack::has_as< T >::value, T >::type as() const
Get value as T.
Definition: object.hpp:1121
union_type via
Definition: object_fwd.hpp:93
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:1071
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