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"
16 #include "msgpack/object.hpp"
18 #include "msgpack/meta.hpp"
19 
20 #include <array>
21 
22 namespace msgpack {
23 
27 
28 namespace adaptor {
29 
30 namespace detail {
31 
32 namespace array {
33 
34 template<typename T, std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
35 inline 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 
43 template<typename T, std::size_t N1, std::size_t N2>
44 inline 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 
48 template <typename T, std::size_t N>
49 struct 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 
56 template <typename T>
57 struct 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 
64 template <typename T>
65 struct 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 
75 template <typename T, std::size_t N>
76 struct 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 
84 template <typename T, std::size_t N>
85 struct 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 
103 template <typename T, std::size_t N>
104 struct 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 
114 template <typename T, std::size_t N>
115 struct object_with_zone<std::array<T, N>> {
116  void operator()(msgpack::object::with_zone& o, const std::array<T, N>& v) const {
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:1217
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
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: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
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::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
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