MessagePack for C++
cpp11_define_map.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_MAP_HPP
11 #define MSGPACK_V1_CPP11_DEFINE_MAP_HPP
12 
15 
16 #include <tuple>
17 #include <map>
18 
19 namespace msgpack {
23 namespace type {
24 
25 template <typename Tuple, std::size_t N>
27  template <typename Packer>
28  static void pack(Packer& pk, Tuple const& t) {
30  pk.pack(std::get<N-1>(t));
31  }
32  static void unpack(
33  msgpack::object const& o, Tuple const& t,
34  std::map<std::string, msgpack::object const*> const& kvmap) {
36  auto it = kvmap.find(std::get<N-2>(t));
37  if (it != kvmap.end()) {
38  convert_helper(*it->second, std::get<N-1>(t));
39  }
40  }
41  static void object(msgpack::object* o, msgpack::zone& z, Tuple const& t) {
43  o->via.map.ptr[(N-1)/2].key = msgpack::object(std::get<N-2>(t), z);
44  o->via.map.ptr[(N-1)/2].val = msgpack::object(std::get<N-1>(t), z);
45  }
46 };
47 
48 template <typename Tuple>
49 struct define_map_imp<Tuple, 0> {
50  template <typename Packer>
51  static void pack(Packer&, Tuple const&) {}
52  static void unpack(
53  msgpack::object const&, Tuple const&,
54  std::map<std::string, msgpack::object const*> const&) {}
55  static void object(msgpack::object*, msgpack::zone&, Tuple const&) {}
56 };
57 
58 template <typename... Args>
59 struct define_map {
60  define_map(Args&... args) :
61  a(args...) {}
62  template <typename Packer>
63  void msgpack_pack(Packer& pk) const
64  {
65  static_assert(sizeof...(Args) % 2 == 0, "");
66  pk.pack_map(sizeof...(Args) / 2);
67 
68  define_map_imp<std::tuple<Args&...>, sizeof...(Args)>::pack(pk, a);
69  }
70  void msgpack_unpack(msgpack::object const& o) const
71  {
72  if(o.type != msgpack::type::MAP) { throw msgpack::type_error(); }
73  std::map<std::string, msgpack::object const*> kvmap;
74  for (uint32_t i = 0; i < o.via.map.size; ++i) {
75  if (o.via.map.ptr[i].key.type != msgpack::type::STR) { throw msgpack::type_error(); }
76  kvmap.emplace(
77  std::string(
78  o.via.map.ptr[i].key.via.str.ptr,
79  o.via.map.ptr[i].key.via.str.size),
80  &o.via.map.ptr[i].val);
81  }
82  define_map_imp<std::tuple<Args&...>, sizeof...(Args)>::unpack(o, a, kvmap);
83  }
85  {
86  static_assert(sizeof...(Args) % 2 == 0, "");
88  o->via.map.ptr = static_cast<msgpack::object_kv*>(z.allocate_align(sizeof(msgpack::object_kv)*sizeof...(Args)/2, MSGPACK_ZONE_ALIGNOF(msgpack::object_kv)));
89  o->via.map.size = sizeof...(Args) / 2;
90 
91  define_map_imp<std::tuple<Args&...>, sizeof...(Args)>::object(o, z, a);
92  }
93 
94  std::tuple<Args&...> a;
95 };
96 
97 
98 template <typename... Args>
99 inline define_map<Args...> make_define_map(Args&... args)
100 {
101  return define_map<Args...>(args...);
102 }
103 
104 } // namespace type
106 } // MSGPACK_API_VERSION_NAMESPACE(v1)
108 } // namespace msgpack
109 
110 #endif // MSGPACK_V1_CPP11_DEFINE_MAP_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
@ MAP
Definition: object_fwd_decl.hpp:41
@ STR
Definition: object_fwd_decl.hpp:38
define_map make_define_map()
Definition: cpp03_define_map.hpp:2546
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
Definition: object.hpp:30
msgpack::object val
Definition: object.hpp:32
msgpack::object key
Definition: object.hpp:31
uint32_t size
Definition: object_fwd.hpp:28
msgpack::object_kv * ptr
Definition: object_fwd.hpp:29
const char * ptr
Definition: object_fwd.hpp:34
uint32_t size
Definition: object_fwd.hpp:33
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
static void unpack(msgpack::object const &, Tuple const &, std::map< std::string, msgpack::object const * > const &)
Definition: cpp11_define_map.hpp:52
static void pack(Packer &, Tuple const &)
Definition: cpp11_define_map.hpp:51
static void object(msgpack::object *, msgpack::zone &, Tuple const &)
Definition: cpp11_define_map.hpp:55
Definition: cpp11_define_map.hpp:26
static void unpack(msgpack::object const &o, Tuple const &t, std::map< std::string, msgpack::object const * > const &kvmap)
Definition: cpp11_define_map.hpp:32
static void pack(Packer &pk, Tuple const &t)
Definition: cpp11_define_map.hpp:28
static void object(msgpack::object *o, msgpack::zone &z, Tuple const &t)
Definition: cpp11_define_map.hpp:41
Definition: cpp11_define_map.hpp:59
void msgpack_unpack(msgpack::object const &o) const
Definition: cpp11_define_map.hpp:70
define_map(Args &... args)
Definition: cpp11_define_map.hpp:60
void msgpack_pack(Packer &pk) const
Definition: cpp11_define_map.hpp:63
std::tuple< Args &... > a
Definition: cpp11_define_map.hpp:94
void msgpack_object(msgpack::object *o, msgpack::zone &z) const
Definition: cpp11_define_map.hpp:84
msgpack::object_str str
Definition: object_fwd.hpp:87
msgpack::object_map map
Definition: object_fwd.hpp:86
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66