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