MessagePack for C++
deque.hpp
Go to the documentation of this file.
1 //
2 // MessagePack for C++ static resolution routine
3 //
4 // Copyright (C) 2008-2015 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_DEQUE_HPP
11 #define MSGPACK_V1_TYPE_DEQUE_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/object.hpp"
17 
18 #include <deque>
19 
20 namespace msgpack {
21 
25 
26 namespace adaptor {
27 
28 #if !defined(MSGPACK_USE_CPP03)
29 
30 template <typename T, typename Alloc>
31 struct as<std::deque<T, Alloc>, typename std::enable_if<msgpack::has_as<T>::value>::type> {
32  std::deque<T, Alloc> operator()(const msgpack::object& o) const {
33  if (o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
34  std::deque<T, Alloc> v;
35  if (o.via.array.size > 0) {
37  msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
38  do {
39  v.push_back(p->as<T>());
40  ++p;
41  } while (p < pend);
42  }
43  return v;
44  }
45 };
46 
47 #endif // !defined(MSGPACK_USE_CPP03)
48 
49 template <typename T, typename Alloc>
50 struct convert<std::deque<T, Alloc> > {
51  msgpack::object const& operator()(msgpack::object const& o, std::deque<T, Alloc>& v) const {
52  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
53  v.resize(o.via.array.size);
55  msgpack::object* const pend = o.via.array.ptr + o.via.array.size;
56  typename std::deque<T, Alloc>::iterator it = v.begin();
57  for(; p < pend; ++p, ++it) {
58  p->convert(*it);
59  }
60  return o;
61  }
62 };
63 
64 template <typename T, typename Alloc>
65 struct pack<std::deque<T, Alloc> > {
66  template <typename Stream>
67  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const std::deque<T, Alloc>& v) const {
68  uint32_t size = checked_get_container_size(v.size());
69  o.pack_array(size);
70  for(typename std::deque<T, Alloc>::const_iterator it(v.begin()), it_end(v.end());
71  it != it_end; ++it) {
72  o.pack(*it);
73  }
74  return o;
75  }
76 };
77 
78 template <typename T, typename Alloc>
79 struct object_with_zone<std::deque<T, Alloc> > {
80  void operator()(msgpack::object::with_zone& o, const std::deque<T, Alloc>& v) const {
82  if(v.empty()) {
84  o.via.array.size = 0;
85  } else {
86  uint32_t size = checked_get_container_size(v.size());
88  msgpack::object* const pend = p + size;
89  o.via.array.ptr = p;
90  o.via.array.size = size;
91  typename std::deque<T, Alloc>::const_iterator it(v.begin());
92  do {
93  *p = msgpack::object(*it, o.zone);
94  ++p;
95  ++it;
96  } while(p < pend);
97  }
98  }
99 };
100 
101 } // namespace adaptor
102 
104 } // MSGPACK_API_VERSION_NAMESPACE(v1)
106 
107 } // namespace msgpack
108 
109 #endif // MSGPACK_V1_TYPE_DEQUE_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:1217
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:255
std::size_t size(T const &t)
Definition: size_equal_only.hpp:24
@ ARRAY
Definition: object_fwd_decl.hpp:40
Definition: adaptor_base.hpp:15
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
std::deque< T, Alloc > operator()(const msgpack::object &o) const
Definition: deque.hpp:32
Definition: object_fwd_decl.hpp:61
msgpack::object const & operator()(msgpack::object const &o, std::deque< T, Alloc > &v) const
Definition: deque.hpp:51
Definition: adaptor_base.hpp:27
void operator()(msgpack::object::with_zone &o, const std::deque< T, Alloc > &v) const
Definition: deque.hpp:80
Definition: adaptor_base.hpp:43
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, const std::deque< T, Alloc > &v) const
Definition: deque.hpp:67
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:1126
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:1076
msgpack::type::object_type type
Definition: object_fwd.hpp:92
msgpack::object_array array
Definition: object_fwd.hpp:85
#define MSGPACK_NULLPTR
Definition: cpp_config_decl.hpp:85
#define MSGPACK_ZONE_ALIGNOF(type)
Definition: cpp03_zone_decl.hpp:30
#define MSGPACK_API_VERSION_NAMESPACE(ns)
Definition: versioning.hpp:66