MessagePack for C++
unordered_set.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_TYPE_TR1_UNORDERED_SET_HPP
11 #define MSGPACK_TYPE_TR1_UNORDERED_SET_HPP
12 
13 #include "msgpack/versioning.hpp"
15 #include "msgpack/object.hpp"
17 
18 #if defined(_LIBCPP_VERSION) || (_MSC_VER >= 1700)
19 
20 #define MSGPACK_HAS_STD_UNORDERED_SET
21 #include <unordered_set>
22 #define MSGPACK_STD_TR1 std
23 
24 #else // defined(_LIBCPP_VERSION) || (_MSC_VER >= 1700)
25 
26 #if __GNUC__ >= 4
27 
28 #define MSGPACK_HAS_STD_TR1_UNORDERED_SET
29 
30 #include <tr1/unordered_set>
31 #define MSGPACK_STD_TR1 std::tr1
32 
33 #endif // __GNUC__ >= 4
34 
35 #endif // defined(_LIBCPP_VERSION) || (_MSC_VER >= 1700)
36 
37 #if defined(MSGPACK_STD_TR1)
38 
39 namespace msgpack {
40 
44 
45 namespace adaptor {
46 
47 template <typename T, typename Hash, typename Compare, typename Alloc>
48 struct convert<MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc> > {
49  msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc>& v) const {
50  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
52  msgpack::object* const pbegin = o.via.array.ptr;
53  MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc> tmp;
54  while(p > pbegin) {
55  --p;
56  tmp.insert(p->as<T>());
57  }
58  tmp.swap(v);
59  return o;
60  }
61 };
62 
63 template <typename T, typename Hash, typename Compare, typename Alloc>
64 struct pack<MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc> > {
65  template <typename Stream>
66  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc>& v) const {
67  uint32_t size = checked_get_container_size(v.size());
68  o.pack_array(size);
69  for(typename MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
70  it != it_end; ++it) {
71  o.pack(*it);
72  }
73  return o;
74  }
75 };
76 
77 template <typename T, typename Hash, typename Compare, typename Alloc>
78 struct object_with_zone<MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc> > {
79  void operator()(msgpack::object::with_zone& o, const MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc>& v) const {
81  if(v.empty()) {
83  o.via.array.size = 0;
84  } else {
85  uint32_t size = checked_get_container_size(v.size());
87  msgpack::object* const pend = p + size;
88  o.via.array.ptr = p;
89  o.via.array.size = size;
90  typename MSGPACK_STD_TR1::unordered_set<T, Hash, Compare, Alloc>::const_iterator it(v.begin());
91  do {
92  *p = msgpack::object(*it, o.zone);
93  ++p;
94  ++it;
95  } while(p < pend);
96  }
97  }
98 };
99 
100 
101 template <typename T, typename Hash, typename Compare, typename Alloc>
102 struct convert<MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc> > {
103  msgpack::object const& operator()(msgpack::object const& o, MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc>& v) const {
104  if(o.type != msgpack::type::ARRAY) { throw msgpack::type_error(); }
106  msgpack::object* const pbegin = o.via.array.ptr;
107  MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc> tmp;
108  while(p > pbegin) {
109  --p;
110  tmp.insert(p->as<T>());
111  }
112  tmp.swap(v);
113  return o;
114  }
115 };
116 
117 template <typename T, typename Hash, typename Compare, typename Alloc>
118 struct pack<MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc> > {
119  template <typename Stream>
120  msgpack::packer<Stream>& operator()(msgpack::packer<Stream>& o, const MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc>& v) const {
121  uint32_t size = checked_get_container_size(v.size());
122  o.pack_array(size);
123  for(typename MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc>::const_iterator it(v.begin()), it_end(v.end());
124  it != it_end; ++it) {
125  o.pack(*it);
126  }
127  return o;
128  }
129 };
130 
131 template <typename T, typename Hash, typename Compare, typename Alloc>
132 struct object_with_zone<MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc> > {
133  void operator()(msgpack::object::with_zone& o, const MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc>& v) const {
135  if(v.empty()) {
137  o.via.array.size = 0;
138  } else {
139  uint32_t size = checked_get_container_size(v.size());
141  msgpack::object* const pend = p + size;
142  o.via.array.ptr = p;
143  o.via.array.size = size;
144  typename MSGPACK_STD_TR1::unordered_multiset<T, Hash, Compare, Alloc>::const_iterator it(v.begin());
145  do {
146  *p = msgpack::object(*it, o.zone);
147  ++p;
148  ++it;
149  } while(p < pend);
150  }
151  }
152 };
153 
154 } // namespace adaptor
155 
157 } // MSGPACK_API_VERSION_NAMESPACE(v1)
159 
160 } // namespace msgpack
161 
162 #undef MSGPACK_STD_TR1
163 
164 #endif // MSGPACK_STD_TR1
165 
166 #endif // MSGPACK_TYPE_TR1_UNORDERED_SET_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
void pack(msgpack::packer< Stream > &o, const T &v)
Definition: object.hpp:1185
uint32_t checked_get_container_size(T size)
Definition: check_container_size.hpp:55
void convert(T &v, msgpack::object const &o)
Definition: object.hpp:1178
msgpack::object const & operator()(msgpack::object const &o, T &v) const
Definition: object.hpp:646
void operator()(msgpack::object::with_zone &o, T const &v) const
Definition: object.hpp:662
msgpack::packer< Stream > & operator()(msgpack::packer< Stream > &o, T const &v) const
Definition: object.hpp:655
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::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