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 
14 
15 #include <tuple>
16 
17 namespace msgpack {
21 namespace type {
22 
23 template <typename Tuple, std::size_t N>
25  template <typename Packer>
26  static void pack(Packer& pk, Tuple const& t) {
28  pk.pack(std::get<N-1>(t));
29  }
30  static void unpack(msgpack::object const& o, Tuple& t) {
32  const size_t size = o.via.array.size;
33  if(size <= N-1) { return; }
34  o.via.array.ptr[N-1].convert(std::get<N-1>(t));
35  }
36  static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
38  o->via.array.ptr[N-1] = msgpack::object(std::get<N-1>(t), z);
39  }
40 };
41 
42 template <typename Tuple>
43 struct define_array_imp<Tuple, 1> {
44  template <typename Packer>
45  static void pack(Packer& pk, Tuple const& t) {
46  pk.pack(std::get<0>(t));
47  }
48  static void unpack(msgpack::object const& o, Tuple& t) {
49  const size_t size = o.via.array.size;
50  if(size <= 0) { return; }
51  o.via.array.ptr[0].convert(std::get<0>(t));
52  }
53  static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
54  o->via.array.ptr[0] = msgpack::object(std::get<0>(t), z);
55  }
56 };
57 
58 template <typename... Args>
59 struct define_array {
60  typedef define_array<Args...> value_type;
61  typedef std::tuple<Args...> tuple_type;
62  define_array(Args&... args) :
63  a(args...) {}
64  template <typename Packer>
65  void msgpack_pack(Packer& pk) const
66  {
67  pk.pack_array(sizeof...(Args));
68 
69  define_array_imp<std::tuple<Args&...>, sizeof...(Args)>::pack(pk, a);
70  }
72  {
73  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
74 
75  define_array_imp<std::tuple<Args&...>, sizeof...(Args)>::unpack(o, a);
76  }
78  {
80  o->via.array.ptr = static_cast<msgpack::object*>(z.allocate_align(sizeof(msgpack::object)*sizeof...(Args)));
81  o->via.array.size = sizeof...(Args);
82 
83  define_array_imp<std::tuple<Args&...>, sizeof...(Args)>::object(o, z, a);
84  }
85 
86  std::tuple<Args&...> a;
87 };
88 
89 template <>
90 struct define_array<> {
92  typedef std::tuple<> tuple_type;
93  template <typename Packer>
94  void msgpack_pack(Packer& pk) const
95  {
96  pk.pack_array(0);
97  }
99  {
100  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
101  }
103  {
105  o->via.array.ptr = NULL;
106  o->via.array.size = 0;
107  }
108 };
109 
111 {
112  return define_array<>();
113 }
114 
115 template <typename... Args>
116 inline define_array<Args...> make_define_array(Args&... args)
117 {
118  return define_array<Args...>(args...);
119 }
120 
121 } // namespace type
123 } // MSGPACK_API_VERSION_NAMESPACE(v1)
125 } // namespace msgpack
126 
127 #endif // MSGPACK_V1_CPP11_DEFINE_ARRAY_HPP
void msgpack_pack(Packer &pk) const
Definition: cpp11_define_array.hpp:65
define_array(Args &...args)
Definition: cpp11_define_array.hpp:62
void msgpack_pack(Packer &pk) const
Definition: cpp11_define_array.hpp:94
define_array make_define_array()
Definition: cpp03_define_array.hpp:3218
define_array< Args... > value_type
Definition: cpp11_define_array.hpp:60
uint32_t size
Definition: object_fwd.hpp:23
void * allocate_align(size_t size, size_t align=MSGPACK_ZONE_ALIGN)
Definition: cpp03_zone.hpp:236
static void unpack(msgpack::object const &o, Tuple &t)
Definition: cpp11_define_array.hpp:30
std::tuple tuple_type
Definition: cpp11_define_array.hpp:92
union_type via
Definition: object_fwd.hpp:92
Definition: cpp11_define_array.hpp:24
void msgpack_unpack(msgpack::object const &o)
Definition: cpp11_define_array.hpp:98
static void object(msgpack::object *o, msgpack::zone &z, Tuple const &t)
Definition: cpp11_define_array.hpp:36
Definition: cpp11_define_array.hpp:59
static void object(msgpack::object *o, msgpack::zone &z, Tuple const &t)
Definition: cpp11_define_array.hpp:53
msgpack::object * ptr
Definition: object_fwd.hpp:24
static void pack(Packer &pk, Tuple const &t)
Definition: cpp11_define_array.hpp:26
Definition: adaptor_base.hpp:15
void msgpack_object(msgpack::object *o, msgpack::zone &) const
Definition: cpp11_define_array.hpp:102
Definition: cpp03_zone.hpp:22
Definition: object_fwd.hpp:222
static void pack(Packer &pk, Tuple const &t)
Definition: cpp11_define_array.hpp:45
void msgpack_object(msgpack::object *o, msgpack::zone &z) const
Definition: cpp11_define_array.hpp:77
msgpack::object_array array
Definition: object_fwd.hpp:84
std::tuple< Args &... > a
Definition: cpp11_define_array.hpp:86
void msgpack_unpack(msgpack::object const &o)
Definition: cpp11_define_array.hpp:71
T & convert(T &v) const
Convert the object.
Definition: object.hpp:529
Object class that corresponding to MessagePack format object.
Definition: object_fwd.hpp:75
msgpack::type::object_type type
Definition: object_fwd.hpp:91
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:58
Definition: object_fwd_decl.hpp:39
std::tuple< Args... > tuple_type
Definition: cpp11_define_array.hpp:61
static void unpack(msgpack::object const &o, Tuple &t)
Definition: cpp11_define_array.hpp:48
define_array value_type
Definition: cpp11_define_array.hpp:91
Definition: cpp03_define_array.hpp:26