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"
16 #include "msgpack/meta.hpp"
17 
18 #include <tuple>
19 
20 namespace msgpack {
21 
25 
26 // --- Pack from tuple to packer stream ---
27 template <typename Stream, typename Tuple, std::size_t N>
29  static void pack(
31  const Tuple& v) {
33  o.pack(std::get<N-1>(v));
34  }
35 };
36 
37 template <typename Stream, typename Tuple>
38 struct StdTuplePacker<Stream, Tuple, 0> {
39  static void pack (
41  const Tuple&) {
42  }
43 };
44 
45 namespace adaptor {
46 
47 template <typename... Args>
48 struct pack<std::tuple<Args...>> {
49  template <typename Stream>
52  const std::tuple<Args...>& v) const {
53  uint32_t size = checked_get_container_size(sizeof...(Args));
54  o.pack_array(size);
55  StdTuplePacker<Stream, decltype(v), sizeof...(Args)>::pack(o, v);
56  return o;
57  }
58 };
59 
60 } // namespace adaptor
61 
62 // --- Convert from tuple to object ---
63 
64 template <typename... Args>
65 struct StdTupleAs;
66 
67 template <typename T, typename... Args>
69  static std::tuple<T, Args...> as(msgpack::object const& o) {
70  return std::tuple_cat(
71  std::make_tuple(o.via.array.ptr[o.via.array.size - sizeof...(Args) - 1].as<T>()),
73  }
74 };
75 
76 template <typename... Args>
77 struct StdTupleAs {
78  static std::tuple<Args...> as(msgpack::object const& o) {
80  }
81 };
82 
83 template <>
84 struct StdTupleAs<> {
85  static std::tuple<> as (msgpack::object const&) {
86  return std::tuple<>();
87  }
88 };
89 
90 template <typename Tuple, std::size_t N>
92  static void convert(
93  msgpack::object const& o,
94  Tuple& v) {
96  if (o.via.array.size >= N)
97  o.via.array.ptr[N-1].convert<typename std::remove_reference<decltype(std::get<N-1>(v))>::type>(std::get<N-1>(v));
98  }
99 };
100 
101 template <typename Tuple>
102 struct StdTupleConverter<Tuple, 0> {
103  static void convert (
104  msgpack::object const&,
105  Tuple&) {
106  }
107 };
108 
109 namespace adaptor {
110 
111 template <typename... Args>
112 struct as<std::tuple<Args...>, typename std::enable_if<msgpack::any_of<msgpack::has_as, Args...>::value>::type> {
113  std::tuple<Args...> operator()(
114  msgpack::object const& o) const {
115  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
116  return StdTupleAs<Args...>::as(o);
117  }
118 };
119 
120 template <typename... Args>
121 struct convert<std::tuple<Args...>> {
123  msgpack::object const& o,
124  std::tuple<Args...>& v) const {
125  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
126  StdTupleConverter<decltype(v), sizeof...(Args)>::convert(o, v);
127  return o;
128  }
129 };
130 
131 } // namespace adaptor
132 
133 // --- Convert from tuple to object with zone ---
134 template <typename Tuple, std::size_t N>
136  static void convert(
138  const Tuple& v) {
140  o.via.array.ptr[N-1] = msgpack::object(std::get<N-1>(v), o.zone);
141  }
142 };
143 
144 template <typename Tuple>
145 struct StdTupleToObjectWithZone<Tuple, 0> {
146  static void convert (
148  const Tuple&) {
149  }
150 };
151 
152 namespace adaptor {
153 
154 template <typename... Args>
155 struct object_with_zone<std::tuple<Args...>> {
158  std::tuple<Args...> const& v) const {
159  uint32_t size = checked_get_container_size(sizeof...(Args));
162  o.via.array.size = size;
163  StdTupleToObjectWithZone<decltype(v), sizeof...(Args)>::convert(o, v);
164  }
165 };
166 
167 } // namespace adaptor
168 
170 } // MSGPACK_API_VERSION_NAMESPACE(v1)
172 
173 } // namespace msgpack
174 
175 #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: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
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:1180
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:1173
static std::tuple as(msgpack::object const &)
Definition: tuple.hpp:85
Definition: tuple.hpp:77
static std::tuple< Args... > as(msgpack::object const &o)
Definition: tuple.hpp:78
Definition: tuple.hpp:68
static std::tuple< T, Args... > as(msgpack::object const &o)
Definition: tuple.hpp:69
static void convert(msgpack::object const &, Tuple &)
Definition: tuple.hpp:103
Definition: tuple.hpp:91
static void convert(msgpack::object const &o, Tuple &v)
Definition: tuple.hpp:92
static void pack(msgpack::packer< Stream > &, const Tuple &)
Definition: tuple.hpp:39
Definition: tuple.hpp:28
static void pack(msgpack::packer< Stream > &o, const Tuple &v)
Definition: tuple.hpp:29
static void convert(msgpack::object::with_zone &, const Tuple &)
Definition: tuple.hpp:146
Definition: tuple.hpp:135
static void convert(msgpack::object::with_zone &o, const Tuple &v)
Definition: tuple.hpp:136
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::tuple< Args... > &v) const
Definition: tuple.hpp:122
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, std::tuple< Args... > const &v) const
Definition: tuple.hpp:156
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::tuple< Args... > &v) const
Definition: tuple.hpp:50
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: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
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