1/* Boost.MultiIndex test for serialization, part 3.
2 *
3 * Copyright 2003-2018 Joaquin M Lopez Munoz.
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE_1_0.txt or copy at
6 * http://www.boost.org/LICENSE_1_0.txt)
7 *
8 * See http://www.boost.org/libs/multi_index for library home page.
9 */
10
11#include "test_serialization3.hpp"
12#include "test_serialization_template.hpp"
13
14#include <boost/move/core.hpp>
15#include <boost/multi_index/hashed_index.hpp>
16#include <boost/multi_index/sequenced_index.hpp>
17#include <boost/multi_index/ordered_index.hpp>
18#include <boost/multi_index/key_extractors.hpp>
19#include "non_std_allocator.hpp"
20
21struct non_default_ctble
22{
23 non_default_ctble(int n_):n(n_){}
24
25 bool operator==(const non_default_ctble& x)const{return n==x.n;}
26
27 int n;
28};
29
30#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
31namespace boost{
32namespace serialization{
33#endif
34
35template<class Archive>
36void save_construct_data(
37 Archive& ar,const non_default_ctble* p,const unsigned int version)
38{
39 if(version<3)return;
40
41 ar<<boost::serialization::make_nvp(n: "n",v: p->n);
42}
43
44template<class Archive>
45void load_construct_data(
46 Archive& ar,non_default_ctble* p,const unsigned int version)
47{
48 if(version<3)return;
49
50 int n=0;
51 ar>>boost::serialization::make_nvp(n: "n",v&: n);
52 ::new(p)non_default_ctble(n);
53}
54
55template<class Archive>
56void serialize(Archive&,non_default_ctble&,const unsigned int)
57{
58}
59
60#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
61} /* namespace serialization */
62} /* namespace boost*/
63#endif
64
65namespace boost{
66namespace serialization{
67template<> struct version<non_default_ctble>
68{
69 BOOST_STATIC_CONSTANT(int,value=3);
70};
71} /* namespace serialization */
72} /* namespace boost*/
73
74struct non_copyable
75{
76 non_copyable(int n_=0):n(n_){}
77 non_copyable(BOOST_RV_REF(non_copyable) x):n(x.n){}
78
79 bool operator==(const non_copyable& x)const{return n==x.n;}
80 bool operator<(const non_copyable& x)const{return n<x.n;}
81
82 int n;
83
84private:
85 BOOST_MOVABLE_BUT_NOT_COPYABLE(non_copyable)
86};
87
88#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
89namespace boost{
90namespace serialization{
91#endif
92
93template<class Archive>
94void serialize(Archive& ar,non_copyable& x,const unsigned int)
95{
96 ar&boost::serialization::make_nvp(n: "n",v&: x.n);
97}
98
99#if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
100} /* namespace serialization */
101} /* namespace boost*/
102#endif
103
104using namespace boost::multi_index;
105
106void test_serialization3()
107{
108 const int N=100;
109 const int SHUFFLE=10232;
110
111 typedef multi_index_container<
112 int,
113 indexed_by<
114 hashed_unique<identity<int> >,
115 sequenced<>
116 >,
117 non_std_allocator<int>
118 > hashed_set;
119
120 typedef hashed_set::iterator iterator;
121 typedef hashed_set::local_iterator local_iterator;
122
123 hashed_set hs;
124
125 for(int i=0;i<N;++i){
126 hs.insert(x: i*SHUFFLE);
127 }
128
129 std::ostringstream oss;
130 {
131 boost::archive::text_oarchive oa(oss);
132 oa<<const_cast<const hashed_set&>(hs);
133
134 std::vector<iterator> its(N);
135 for(int i=0;i<N;++i){
136 iterator it=hs.find(k: i*SHUFFLE);
137 its.push_back(x: it);
138 oa<<const_cast<const iterator&>(its.back());
139 }
140 iterator it=hs.end();
141 oa<<const_cast<const iterator&>(it);
142
143 std::vector<local_iterator> lits(2*N);
144 for(std::size_t buc=0;buc<hs.bucket_count();++buc){
145 for(local_iterator lit=hs.begin(n: buc),lit_end=hs.end(n: buc);
146 lit!=lit_end;++lit){
147 oa<<*lit;
148 lits.push_back(x: lit);
149 oa<<const_cast<const local_iterator&>(lits.back());
150 }
151 local_iterator lit2=hs.end(n: buc);
152 lits.push_back(x: lit2);
153 oa<<const_cast<const local_iterator&>(lits.back());
154 }
155 }
156
157 hashed_set hs2;
158 std::istringstream iss(oss.str());
159 boost::archive::text_iarchive ia(iss);
160 ia>>hs2;
161 BOOST_TEST(boost::multi_index::get<1>(hs)==boost::multi_index::get<1>(hs2));
162
163 for(int j=0;j<N;++j){
164 iterator it;
165 ia>>it;
166 BOOST_TEST(*it==j*SHUFFLE);
167 }
168 iterator it;
169 ia>>it;
170 BOOST_TEST(it==hs2.end());
171
172 for(std::size_t buc=0;buc<hs2.bucket_count();++buc){
173 for(std::size_t k=0;k<hs2.bucket_size(n: buc);++k){
174 int n;
175 local_iterator it_;
176 ia>>n;
177 ia>>it_;
178 BOOST_TEST(*it_==n);
179 }
180 local_iterator it2;
181 ia>>it2;
182 BOOST_TEST(it2==hs2.end(buc));
183 }
184
185 {
186 typedef multi_index_container<
187 non_default_ctble,
188 indexed_by<
189 ordered_unique<
190 BOOST_MULTI_INDEX_MEMBER(non_default_ctble,int,n)
191 >
192 >
193 > multi_index_t;
194
195 multi_index_t m;
196 for(int i=0;i<100;++i)m.insert(x: non_default_ctble(i));
197 test_serialization(m);
198 }
199
200 {
201 /* testcase for https://svn.boost.org/trac10/ticket/13478 */
202
203 typedef multi_index_container<
204 non_copyable,
205 indexed_by<
206 ordered_unique<
207 BOOST_MULTI_INDEX_MEMBER(non_copyable,int,n)
208 >
209 >
210 > multi_index_t;
211
212 multi_index_t m;
213 for(int i=0;i<100;++i)m.emplace(args&: i);
214 test_serialization(m);
215 }
216}
217

source code of boost/libs/multi_index/test/test_serialization3.cpp