MessagePack for C++
cpp11_define_array.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2016 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_CPP11_DEFINE_ARRAY_HPP
11 #define MSGPACK_V1_CPP11_DEFINE_ARRAY_HPP
12 
15 
16 #include <tuple>
17 
18 namespace msgpack {
22 namespace type {
23 
24 template <typename Tuple, std::size_t N>
26  template <typename Packer>
27  static void pack(Packer& pk, Tuple const& t) {
29  pk.pack(std::get<N-1>(t));
30  }
31  static void unpack(msgpack::object const& o, Tuple& t) {
33  const size_t size = o.via.array.size;
34  if(size <= N-1) { return; }
35  convert_helper(o.via.array.ptr[N-1], std::get<N-1>(t));
36  }
37  static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
39  o->via.array.ptr[N-1] = msgpack::object(std::get<N-1>(t), z);
40  }
41 };
42 
43 template <typename Tuple>
44 struct define_array_imp<Tuple, 1> {
45  template <typename Packer>
46  static void pack(Packer& pk, Tuple const& t) {
47  pk.pack(std::get<0>(t));
48  }
49  static void unpack(msgpack::object const& o, Tuple& t) {
50  const size_t size = o.via.array.size;
51  if(size <= 0) { return; }
52  convert_helper(o.via.array.ptr[0], std::get<0>(t));
53  }
54  static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
55  o->via.array.ptr[0] = msgpack::object(std::get<0>(t), z);
56  }
57 };
58 
59 template <typename... Args>
60 struct define_array {
61  typedef define_array<Args...> value_type;
62  typedef std::tuple<Args...> tuple_type;
63  define_array(Args&... args) :
64  a(args...) {}
65  template <typename Packer>
66  void msgpack_pack(Packer& pk) const
67  {
68  pk.pack_array(sizeof...(Args));
69 
70  define_array_imp<std::tuple<Args&...>, sizeof...(Args)>::pack(pk, a);
71  }
73  {
74  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
75 
76  define_array_imp<std::tuple<Args&...>, sizeof...(Args)>::unpack(o, a);
77  }
79  {
81  o->via.array.ptr = static_cast<msgpack::object*>(z.allocate_align(sizeof(msgpack::object)*sizeof...(Args), MSGPACK_ZONE_ALIGNOF(msgpack::object)));
82  o->via.array.size = sizeof...(Args);
83 
84  define_array_imp<std::tuple<Args&...>, sizeof...(Args)>::object(o, z, a);
85  }
86 
87  std::tuple<Args&...> a;
88 };
89 
90 template <>
91 struct define_array<> {
93  typedef std::tuple<> tuple_type;
94  template <typename Packer>
95  void msgpack_pack(Packer& pk) const
96  {
97  pk.pack_array(0);
98  }
100  {
101  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
102  }
104  {
106  o->via.array.ptr = NULL;
107  o->via.array.size = 0;
108  }
109 };
110 
111 inline define_array<> make_define_array()
112 {
113  return define_array<>();
114 }
115 
116 template <typename... Args>
117 inline define_array<Args...> make_define_array(Args&... args)
118 {
119  return define_array<Args...>(args...);
120 }
121 
122 } // namespace type
124 } // MSGPACK_API_VERSION_NAMESPACE(v1)
126 } // namespace msgpack
127 
128 #endif // MSGPACK_V1_CPP11_DEFINE_ARRAY_HPP
Definition: object_fwd.hpp:231
Definition: cpp03_zone.hpp:31
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:256
std::enable_if< has_as< T >::value >::type convert_helper(msgpack::object const &o, T &t)
Definition: cpp11_convert_helper.hpp:27
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
define_array make_define_array()
Definition: cpp03_define_array.hpp:4274
@ 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
msgpack::object_handle unpack(const char *data, std::size_t len, std::size_t &off, bool &referenced, unpack_reference_func f, void *user_data, unpack_limit const &limit)
Unpack msgpack::object from a buffer.
Definition: unpack.hpp:1397
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
union_type via
Definition: object_fwd.hpp:93
msgpack::type::object_type type
Definition: object_fwd.hpp:92
Definition: cpp03_define_array.hpp:26
std::tuple tuple_type
Definition: cpp11_define_array.hpp:93
void msgpack_pack(Packer &pk) const
Definition: cpp11_define_array.hpp:95
void msgpack_unpack(msgpack::object const &o)
Definition: cpp11_define_array.hpp:99
void msgpack_object(msgpack::object *o, msgpack::zone &) const
Definition: cpp11_define_array.hpp:103
define_array value_type
Definition: cpp11_define_array.hpp:92
static void unpack(msgpack::object const &o, Tuple &t)
Definition: cpp11_define_array.hpp:49
static void object(msgpack::object *o, msgpack::zone &z, Tuple const &t)
Definition: cpp11_define_array.hpp:54
static void pack(Packer &pk, Tuple const &t)
Definition: cpp11_define_array.hpp:46
Definition: cpp11_define_array.hpp:25
static void pack(Packer &pk, Tuple const &t)
Definition: cpp11_define_array.hpp:27
static void object(msgpack::object *o, msgpack::zone &z, Tuple const &t)
Definition: cpp11_define_array.hpp:37
static void unpack(msgpack::object const &o, Tuple &t)
Definition: cpp11_define_array.hpp:31
Definition: cpp11_define_array.hpp:60
void msgpack_object(msgpack::object *o, msgpack::zone &z) const
Definition: cpp11_define_array.hpp:78
define_array(Args &... args)
Definition: cpp11_define_array.hpp:63
void msgpack_pack(Packer &pk) const
Definition: cpp11_define_array.hpp:66
std::tuple< Args... > tuple_type
Definition: cpp11_define_array.hpp:62
void msgpack_unpack(msgpack::object const &o)
Definition: cpp11_define_array.hpp:72
define_array< Args... > value_type
Definition: cpp11_define_array.hpp:61
std::tuple< Args &... > a
Definition: cpp11_define_array.hpp:87
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