MessagePack for C++
pair.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2009 FURUHASHI Sadayuki
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_PAIR_HPP
11 #define MSGPACK_V1_TYPE_PAIR_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/meta.hpp"
16 
17 #include <utility>
18 
19 namespace msgpack {
20 
24 
25 namespace adaptor {
26 
27 #if !defined(MSGPACK_USE_CPP03)
28 
29 template <typename T1, typename T2>
30 struct as<std::pair<T1, T2>,
31  typename std::enable_if<msgpack::any_of<msgpack::has_as, T1, T2>::value>::type> {
32  std::pair<T1, T2> operator()(msgpack::object const& o) const {
33  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
34  if (o.via.array.size != 2) { throw msgpack::type_error(); }
35  return std::make_pair(o.via.array.ptr[0].as<T1>(), o.via.array.ptr[1].as<T2>());
36  }
37 };
38 
39 #endif // !defined(MSGPACK_USE_CPP03)
40 
41 template <typename T1, typename T2>
42 struct convert<std::pair<T1, T2> > {
43  msgpack::object const& operator()(msgpack::object const& o, std::pair<T1, T2>& v) const {
44  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
45  if(o.via.array.size != 2) { throw msgpack::type_error(); }
46  o.via.array.ptr[0].convert(v.first);
47  o.via.array.ptr[1].convert(v.second);
48  return o;
49  }
50 };
51 
52 template <typename T1, typename T2>
53 struct pack<std::pair<T1, T2> > {
54  template <typename Stream>
55  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::pair<T1, T2>& v) const {
56  o.pack_array(2);
57  o.pack(v.first);
58  o.pack(v.second);
59  return o;
60  }
61 };
62 
63 template <typename T1, typename T2>
64 struct object_with_zone<std::pair<T1, T2> > {
65  void operator()(msgpack::object::with_zone& o, const std::pair<T1, T2>& v) const {
68  o.via.array.ptr = p;
69  o.via.array.size = 2;
70  p[0] = msgpack::object(v.first, o.zone);
71  p[1] = msgpack::object(v.second, o.zone);
72  }
73 };
74 
75 } // namespace adaptor
76 
78 } // MSGPACK_API_VERSION_NAMESPACE(v1)
80 
81 } // namespace msgpack
82 
83 #endif // MSGPACK_V1_TYPE_PAIR_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
@ ARRAY
Definition: object_fwd_decl.hpp:40
Definition: adaptor_base.hpp:15
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::pair< T1, T2 > &v) const
Definition: pair.hpp:43
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::pair< T1, T2 > &v) const
Definition: pair.hpp:65
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::pair< T1, T2 > &v) const
Definition: pair.hpp:55
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