MessagePack for C++
tuple.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_CPP11_TUPLE_HPP
11 #define MSGPACK_V1_TYPE_CPP11_TUPLE_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/object.hpp"
17 #include "msgpack/meta.hpp"
18 
19 #include <tuple>
20 
21 namespace msgpack {
22 
26 
27 // --- Pack from tuple to packer stream ---
28 template <typename Stream, typename Tuple, std::size_t N>
30  static void pack(
32  const Tuple& v) {
34  o.pack(std::get<N-1>(v));
35  }
36 };
37 
38 template <typename Stream, typename Tuple>
39 struct StdTuplePacker<Stream, Tuple, 0> {
40  static void pack (
42  const Tuple&) {
43  }
44 };
45 
46 namespace adaptor {
47 
48 template <typename... Args>
49 struct pack<std::tuple<Args...>> {
50  template <typename Stream>
53  const std::tuple<Args...>& v) const {
54  uint32_t size = checked_get_container_size(sizeof...(Args));
55  o.pack_array(size);
56  StdTuplePacker<Stream, decltype(v), sizeof...(Args)>::pack(o, v);
57  return o;
58  }
59 };
60 
61 } // namespace adaptor
62 
63 // --- Convert from tuple to object ---
64 
65 template <typename... Args>
66 struct StdTupleAs;
67 
68 template <typename T, typename... Args>
70  static std::tuple<T, Args...> as(msgpack::object const& o) {
71  return std::tuple_cat(
72  std::make_tuple(o.via.array.ptr[o.via.array.size - sizeof...(Args) - 1].as<T>()),
74  }
75 };
76 
77 template <typename... Args>
78 struct StdTupleAs {
79  static std::tuple<Args...> as(msgpack::object const& o) {
81  }
82 };
83 
84 template <>
85 struct StdTupleAs<> {
86  static std::tuple<> as (msgpack::object const&) {
87  return std::tuple<>();
88  }
89 };
90 
91 template <typename Tuple, std::size_t N>
93  static void convert(
94  msgpack::object const& o,
95  Tuple& v) {
97  if (o.via.array.size >= N)
98  o.via.array.ptr[N-1].convert<typename std::remove_reference<decltype(std::get<N-1>(v))>::type>(std::get<N-1>(v));
99  }
100 };
101 
102 template <typename Tuple>
103 struct StdTupleConverter<Tuple, 0> {
104  static void convert (
105  msgpack::object const&,
106  Tuple&) {
107  }
108 };
109 
110 namespace adaptor {
111 
112 template <typename... Args>
113 struct as<std::tuple<Args...>, typename std::enable_if<msgpack::any_of<msgpack::has_as, Args...>::value>::type> {
114  std::tuple<Args...> operator()(
115  msgpack::object const& o) const {
116  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
117  return StdTupleAs<Args...>::as(o);
118  }
119 };
120 
121 template <typename... Args>
122 struct convert<std::tuple<Args...>> {
124  msgpack::object const& o,
125  std::tuple<Args...>& v) const {
126  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
127  StdTupleConverter<decltype(v), sizeof...(Args)>::convert(o, v);
128  return o;
129  }
130 };
131 
132 } // namespace adaptor
133 
134 // --- Convert from tuple to object with zone ---
135 template <typename Tuple, std::size_t N>
137  static void convert(
139  const Tuple& v) {
141  o.via.array.ptr[N-1] = msgpack::object(std::get<N-1>(v), o.zone);
142  }
143 };
144 
145 template <typename Tuple>
146 struct StdTupleToObjectWithZone<Tuple, 0> {
147  static void convert (
149  const Tuple&) {
150  }
151 };
152 
153 namespace adaptor {
154 
155 template <typename... Args>
156 struct object_with_zone<std::tuple<Args...>> {
159  std::tuple<Args...> const& v) const {
160  uint32_t size = checked_get_container_size(sizeof...(Args));
163  o.via.array.size = size;
164  StdTupleToObjectWithZone<decltype(v), sizeof...(Args)>::convert(o, v);
165  }
166 };
167 
168 } // namespace adaptor
169 
171 } // MSGPACK_API_VERSION_NAMESPACE(v1)
173 
174 } // namespace msgpack
175 
176 #endif // MSGPACK_V1_TYPE_CPP11_TUPLE_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
tuple make_tuple()
Definition: cpp03_msgpack_tuple.hpp:10408
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
auto tuple_cat(Tuples &&... args) -> decltype(std::tuple_cat(std::forward< typename std::remove_reference< Tuples >::type::base >(args)...))
Definition: cpp11_msgpack_tuple.hpp:36
@ ARRAY
Definition: object_fwd_decl.hpp:40
Definition: adaptor_base.hpp:15
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1185
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1178
static std::tuple as(msgpack::object const &)
Definition: tuple.hpp:86
Definition: tuple.hpp:78
static std::tuple< Args... > as(msgpack::object const &o)
Definition: tuple.hpp:79
Definition: tuple.hpp:69
static std::tuple< T, Args... > as(msgpack::object const &o)
Definition: tuple.hpp:70
static void convert(msgpack::object const &, Tuple &)
Definition: tuple.hpp:104
Definition: tuple.hpp:92
static void convert(msgpack::object const &o, Tuple &v)
Definition: tuple.hpp:93
static void pack(msgpack::packer< Stream > &, const Tuple &)
Definition: tuple.hpp:40
Definition: tuple.hpp:29
static void pack(msgpack::packer< Stream > &o, const Tuple &v)
Definition: tuple.hpp:30
static void convert(msgpack::object::with_zone &, const Tuple &)
Definition: tuple.hpp:147
Definition: tuple.hpp:136
static void convert(msgpack::object::with_zone &o, const Tuple &v)
Definition: tuple.hpp:137
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::tuple< Args... > &v) const
Definition: tuple.hpp:123
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, std::tuple< Args... > const &v) const
Definition: tuple.hpp:157
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::tuple< Args... > &v) const
Definition: tuple.hpp:51
Definition: adaptor_base.hpp:32
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
msgpack::object_array array
Definition: object_fwd.hpp:85
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66