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_CPP11_ARRAY_HPP
12#define MSGPACK_CPP11_ARRAY_HPP
13
14#include "msgpack/versioning.hpp"
15#include "msgpack/adaptor/adaptor_base.hpp"
16#include "msgpack/adaptor/check_container_size.hpp"
17#include "msgpack/meta.hpp"
18
19#include <array>
20
21namespace msgpack {
22
23/// @cond
24MSGPACK_API_VERSION_NAMESPACE(v1) {
25/// @endcond
26
27namespace adaptor {
28
29namespace detail {
30
31namespace array {
32
33template<typename T, std::size_t N1, std::size_t... I1, std::size_t N2, std::size_t... I2>
34inline std::array<T, N1+N2> concat(
35    std::array<T, N1>&& a1,
36    std::array<T, N2>&& a2,
37    msgpack::seq<I1...>,
38    msgpack::seq<I2...>) {
39    return {{ std::move(a1[I1])..., std::move(a2[I2])... }};
40}
41
42template<typename T, std::size_t N1, std::size_t N2>
43inline 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
47template <typename T, std::size_t N>
48struct 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
55template <typename T>
56struct as_impl<T, 1> {
57    static std::array<T, 1> as(msgpack::object const& o) {
58        msgpack::object* p = o.via.array.ptr;
59        return std::array<T, 1>{{p->as<T>()}};
60    }
61};
62
63template <typename T>
64struct 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
74template <typename T, std::size_t N>
75struct 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(); }
79        return detail::array::as_impl<T, N>::as(o);
80    }
81};
82
83template <typename T, std::size_t N>
84struct 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) {
89            msgpack::object* p = o.via.array.ptr;
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
102template <typename T, std::size_t N>
103struct 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
113template <typename T, std::size_t N>
114struct object_with_zone<std::array<T, N>> {
115    void operator()(msgpack::object::with_zone& o, const std::array<T, N>& v) const {
116        o.type = msgpack::type::ARRAY;
117        if(v.empty()) {
118            o.via.array.ptr = nullptr;
119            o.via.array.size = 0;
120        } else {
121            uint32_t size = checked_get_container_size(v.size());
122            msgpack::object* p = static_cast<msgpack::object*>(o.zone.allocate_align(sizeof(msgpack::object)*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
132/// @cond
133} // MSGPACK_API_VERSION_NAMESPACE(v1)
134/// @endcond
135
136} // namespace msgpack
137
138#endif // MSGPACK_CPP11_ARRAY_HPP
139