]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/container/test/scoped_allocator_usage_test.cpp
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / boost / libs / container / test / scoped_allocator_usage_test.cpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2011-2013. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/container for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 #include <boost/container/detail/config_begin.hpp>
11 #include <memory>
12
13 #include <boost/move/utility_core.hpp>
14 #include <boost/container/vector.hpp>
15 #include <boost/container/deque.hpp>
16 #include <boost/container/list.hpp>
17 #include <boost/container/slist.hpp>
18 #include <boost/container/stable_vector.hpp>
19 #include <boost/container/small_vector.hpp>
20 #include <boost/container/flat_map.hpp>
21 #include <boost/container/flat_set.hpp>
22 #include <boost/container/map.hpp>
23 #include <boost/container/set.hpp>
24 #include <boost/container/detail/mpl.hpp>
25
26 #include <boost/container/scoped_allocator.hpp>
27
28 template <typename Ty>
29 class SimpleAllocator
30 {
31 public:
32 typedef Ty value_type;
33
34 explicit SimpleAllocator(int value)
35 : m_state(value)
36 {}
37
38 template <typename T>
39 SimpleAllocator(const SimpleAllocator<T> &other)
40 : m_state(other.m_state)
41 {}
42
43 Ty* allocate(std::size_t n)
44 {
45 return m_allocator.allocate(n);
46 }
47
48 void deallocate(Ty* p, std::size_t n)
49 {
50 m_allocator.deallocate(p, n);
51 }
52
53 int get_value() const
54 { return m_state; }
55
56 private:
57 int m_state;
58 std::allocator<Ty> m_allocator;
59
60 template <typename T> friend class SimpleAllocator;
61
62 friend bool operator == (const SimpleAllocator &, const SimpleAllocator &)
63 { return true; }
64
65 friend bool operator != (const SimpleAllocator &, const SimpleAllocator &)
66 { return false; }
67 };
68
69 class alloc_int
70 {
71 private: // Not copyable
72
73 BOOST_MOVABLE_BUT_NOT_COPYABLE(alloc_int)
74
75 public:
76 typedef SimpleAllocator<int> allocator_type;
77
78 alloc_int(BOOST_RV_REF(alloc_int)other)
79 : m_value(other.m_value), m_allocator(boost::move(other.m_allocator))
80 {
81 other.m_value = -1;
82 }
83
84 alloc_int(BOOST_RV_REF(alloc_int)other, const allocator_type &allocator)
85 : m_value(other.m_value), m_allocator(allocator)
86 {
87 other.m_value = -1;
88 }
89
90 alloc_int(int value, const allocator_type &allocator)
91 : m_value(value), m_allocator(allocator)
92 {}
93
94 alloc_int & operator=(BOOST_RV_REF(alloc_int)other)
95 {
96 other.m_value = other.m_value;
97 return *this;
98 }
99
100 int get_allocator_state() const
101 { return m_allocator.get_value(); }
102
103 int get_value() const
104 { return m_value; }
105
106 friend bool operator < (const alloc_int &l, const alloc_int &r)
107 { return l.m_value < r.m_value; }
108
109 friend bool operator == (const alloc_int &l, const alloc_int &r)
110 { return l.m_value == r.m_value; }
111
112 private:
113 int m_value;
114 allocator_type m_allocator;
115 };
116
117 using namespace ::boost::container;
118
119 //general allocator
120 typedef scoped_allocator_adaptor<SimpleAllocator<alloc_int> > AllocIntAllocator;
121
122 //[multi]map/set
123 typedef std::pair<const alloc_int, alloc_int> MapNode;
124 typedef scoped_allocator_adaptor<SimpleAllocator<MapNode> > MapAllocator;
125 typedef map<alloc_int, alloc_int, std::less<alloc_int>, MapAllocator> Map;
126 typedef set<alloc_int, std::less<alloc_int>, AllocIntAllocator> Set;
127 typedef multimap<alloc_int, alloc_int, std::less<alloc_int>, MapAllocator> MultiMap;
128 typedef multiset<alloc_int, std::less<alloc_int>, AllocIntAllocator> MultiSet;
129
130 //[multi]flat_map/set
131 typedef std::pair<alloc_int, alloc_int> FlatMapNode;
132 typedef scoped_allocator_adaptor<SimpleAllocator<FlatMapNode> > FlatMapAllocator;
133 typedef flat_map<alloc_int, alloc_int, std::less<alloc_int>, FlatMapAllocator> FlatMap;
134 typedef flat_set<alloc_int, std::less<alloc_int>, AllocIntAllocator> FlatSet;
135 typedef flat_multimap<alloc_int, alloc_int, std::less<alloc_int>, FlatMapAllocator> FlatMultiMap;
136 typedef flat_multiset<alloc_int, std::less<alloc_int>, AllocIntAllocator> FlatMultiSet;
137
138 //vector, deque, list, slist, stable_vector.
139 typedef vector<alloc_int, AllocIntAllocator> Vector;
140 typedef deque<alloc_int, AllocIntAllocator> Deque;
141 typedef list<alloc_int, AllocIntAllocator> List;
142 typedef slist<alloc_int, AllocIntAllocator> Slist;
143 typedef stable_vector<alloc_int, AllocIntAllocator> StableVector;
144 typedef small_vector<alloc_int, 9, AllocIntAllocator> SmallVector;
145
146 /////////
147 //is_unique_assoc
148 /////////
149
150 template<class T>
151 struct is_unique_assoc
152 {
153 static const bool value = false;
154 };
155
156 template<class Key, class T, class Compare, class Allocator>
157 struct is_unique_assoc< map<Key, T, Compare, Allocator> >
158 {
159 static const bool value = true;
160 };
161
162 template<class Key, class T, class Compare, class Allocator>
163 struct is_unique_assoc< flat_map<Key, T, Compare, Allocator> >
164 {
165 static const bool value = true;
166 };
167
168 template<class Key, class Compare, class Allocator>
169 struct is_unique_assoc< set<Key, Compare, Allocator> >
170 {
171 static const bool value = true;
172 };
173
174 template<class Key, class Compare, class Allocator>
175 struct is_unique_assoc< flat_set<Key, Compare, Allocator> >
176 {
177 static const bool value = true;
178 };
179
180
181 /////////
182 //is_map
183 /////////
184
185 template<class T>
186 struct is_map
187 {
188 static const bool value = false;
189 };
190
191 template<class Key, class T, class Compare, class Allocator>
192 struct is_map< map<Key, T, Compare, Allocator> >
193 {
194 static const bool value = true;
195 };
196
197 template<class Key, class T, class Compare, class Allocator>
198 struct is_map< flat_map<Key, T, Compare, Allocator> >
199 {
200 static const bool value = true;
201 };
202
203 template<class Key, class T, class Compare, class Allocator>
204 struct is_map< multimap<Key, T, Compare, Allocator> >
205 {
206 static const bool value = true;
207 };
208
209 template<class Key, class T, class Compare, class Allocator>
210 struct is_map< flat_multimap<Key, T, Compare, Allocator> >
211 {
212 static const bool value = true;
213 };
214
215 template<class T>
216 struct is_set
217 {
218 static const bool value = false;
219 };
220
221 template<class Key, class Compare, class Allocator>
222 struct is_set< set<Key, Compare, Allocator> >
223 {
224 static const bool value = true;
225 };
226
227 template<class Key, class Compare, class Allocator>
228 struct is_set< flat_set<Key, Compare, Allocator> >
229 {
230 static const bool value = true;
231 };
232
233 template<class Key, class Compare, class Allocator>
234 struct is_set< multiset<Key, Compare, Allocator> >
235 {
236 static const bool value = true;
237 };
238
239 template<class Key, class Compare, class Allocator>
240 struct is_set< flat_multiset<Key, Compare, Allocator> >
241 {
242 static const bool value = true;
243 };
244
245 /////////
246 //container_wrapper
247 /////////
248
249 //Try to define-allocator_aware requirements
250 template< class Container
251 , bool Assoc = is_set<Container>::value || is_map<Container>::value
252 , bool UniqueAssoc = is_unique_assoc<Container>::value
253 , bool Map = is_map<Container>::value
254 >
255 struct container_wrapper_inserter
256 {
257 typedef typename Container::const_iterator const_iterator;
258 typedef typename Container::iterator iterator;
259
260 template<class Arg>
261 static iterator emplace(Container &c, const_iterator p, const Arg &arg)
262 { return c.emplace(p, arg); }
263 };
264
265 template<class Container> //map
266 struct container_wrapper_inserter<Container, true, true, true>
267 {
268 typedef typename Container::const_iterator const_iterator;
269 typedef typename Container::iterator iterator;
270
271 template<class Arg>
272 static iterator emplace(Container &c, const_iterator, const Arg &arg)
273 { return c.emplace(arg, arg).first; }
274 };
275
276 template<class Container> //set
277 struct container_wrapper_inserter<Container, true, true, false>
278 {
279 typedef typename Container::const_iterator const_iterator;
280 typedef typename Container::iterator iterator;
281
282 template<class Arg>
283 static iterator emplace(Container &c, const_iterator, const Arg &arg)
284 { return c.emplace(arg).first; }
285 };
286
287 template<class Container> //multimap
288 struct container_wrapper_inserter<Container, true, false, true>
289 {
290 typedef typename Container::const_iterator const_iterator;
291 typedef typename Container::iterator iterator;
292
293 template<class Arg>
294 static iterator emplace(Container &c, const_iterator, const Arg &arg)
295 { return c.emplace(arg, arg); }
296 };
297
298 //multiset
299 template<class Container> //multimap
300 struct container_wrapper_inserter<Container, true, false, false>
301 {
302 typedef typename Container::const_iterator const_iterator;
303 typedef typename Container::iterator iterator;
304
305 template<class Arg>
306 static iterator emplace(Container &c, const_iterator, const Arg &arg)
307 { return c.emplace(arg); }
308 };
309
310 template< class Container>
311 struct container_wrapper
312 : public Container
313 {
314 private:
315 BOOST_COPYABLE_AND_MOVABLE(container_wrapper)
316
317 public:
318 typedef typename Container::allocator_type allocator_type;
319 typedef typename Container::const_iterator const_iterator;
320 typedef typename Container::iterator iterator;
321
322 container_wrapper(const allocator_type &a)
323 : Container(a)
324 {}
325
326 container_wrapper(BOOST_RV_REF(container_wrapper) o, const allocator_type &a)
327 : Container(BOOST_MOVE_BASE(Container, o), a)
328 {}
329
330 container_wrapper(const container_wrapper &o, const allocator_type &a)
331 : Container(o, a)
332 {}
333
334 template<class Arg>
335 iterator emplace(const_iterator p, const Arg &arg)
336 { return container_wrapper_inserter<Container>::emplace(*this, p, arg); }
337 };
338
339
340 bool test_value_and_state_equals(const alloc_int &r, int value, int state)
341 { return r.get_value() == value && r.get_allocator_state() == state; }
342
343 template<class F, class S>
344 bool test_value_and_state_equals(const dtl::pair<F, S> &p, int value, int state)
345 { return test_value_and_state_equals(p.first, value, state) && test_alloc_state_equals(p.second, value, state); }
346
347 template<class F, class S>
348 bool test_value_and_state_equals(const std::pair<F, S> &p, int value, int state)
349 { return test_value_and_state_equals(p.first, value, state) && test_value_and_state_equals(p.second, value, state); }
350
351 template<class Container>
352 bool one_level_allocator_propagation_test()
353 {
354 typedef container_wrapper<Container> ContainerWrapper;
355 typedef typename ContainerWrapper::iterator iterator;
356 typedef typename ContainerWrapper::allocator_type allocator_type;
357 typedef typename ContainerWrapper::value_type value_type;
358 {
359 allocator_type al(SimpleAllocator<value_type>(5));
360 ContainerWrapper c(al);
361
362 c.clear();
363 iterator it = c.emplace(c.cbegin(), 42);
364
365 if(!test_value_and_state_equals(*it, 42, 5))
366 return false;
367 }
368 {
369 allocator_type al(SimpleAllocator<value_type>(4));
370 ContainerWrapper c2(al);
371 ContainerWrapper c(::boost::move(c2), allocator_type(SimpleAllocator<value_type>(5)));
372
373 c.clear();
374 iterator it = c.emplace(c.cbegin(), 42);
375
376 if(!test_value_and_state_equals(*it, 42, 5))
377 return false;
378 }/*
379 {
380 ContainerWrapper c2(allocator_type(SimpleAllocator<value_type>(3)));
381 ContainerWrapper c(c2, allocator_type(SimpleAllocator<value_type>(5)));
382
383 c.clear();
384 iterator it = c.emplace(c.cbegin(), 42);
385
386 if(!test_value_and_state_equals(*it, 42, 5))
387 return false;
388 }*/
389 return true;
390 }
391
392 int main()
393 {
394 //unique assoc
395 if(!one_level_allocator_propagation_test<FlatMap>())
396 return 1;
397 if(!one_level_allocator_propagation_test<Map>())
398 return 1;
399 if(!one_level_allocator_propagation_test<FlatSet>())
400 return 1;
401 if(!one_level_allocator_propagation_test<Set>())
402 return 1;
403 //multi assoc
404 if(!one_level_allocator_propagation_test<FlatMultiMap>())
405 return 1;
406 if(!one_level_allocator_propagation_test<MultiMap>())
407 return 1;
408 if(!one_level_allocator_propagation_test<FlatMultiSet>())
409 return 1;
410 if(!one_level_allocator_propagation_test<MultiSet>())
411 return 1;
412 //sequence containers
413 if(!one_level_allocator_propagation_test<Vector>())
414 return 1;
415 if(!one_level_allocator_propagation_test<Deque>())
416 return 1;
417 if(!one_level_allocator_propagation_test<List>())
418 return 1;
419 if(!one_level_allocator_propagation_test<Slist>())
420 return 1;
421 if(!one_level_allocator_propagation_test<StableVector>())
422 return 1;
423 if(!one_level_allocator_propagation_test<SmallVector>())
424 return 1;
425 return 0;
426 }
427
428 #include <boost/container/detail/config_end.hpp>