]> git.proxmox.com Git - ceph.git/blame - 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
CommitLineData
7c673cae
FG
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
28template <typename Ty>
29class SimpleAllocator
30{
31public:
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
69class 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
117using namespace ::boost::container;
118
119//general allocator
120typedef scoped_allocator_adaptor<SimpleAllocator<alloc_int> > AllocIntAllocator;
121
122//[multi]map/set
123typedef std::pair<const alloc_int, alloc_int> MapNode;
124typedef scoped_allocator_adaptor<SimpleAllocator<MapNode> > MapAllocator;
125typedef map<alloc_int, alloc_int, std::less<alloc_int>, MapAllocator> Map;
126typedef set<alloc_int, std::less<alloc_int>, AllocIntAllocator> Set;
127typedef multimap<alloc_int, alloc_int, std::less<alloc_int>, MapAllocator> MultiMap;
128typedef multiset<alloc_int, std::less<alloc_int>, AllocIntAllocator> MultiSet;
129
130//[multi]flat_map/set
131typedef std::pair<alloc_int, alloc_int> FlatMapNode;
132typedef scoped_allocator_adaptor<SimpleAllocator<FlatMapNode> > FlatMapAllocator;
133typedef flat_map<alloc_int, alloc_int, std::less<alloc_int>, FlatMapAllocator> FlatMap;
134typedef flat_set<alloc_int, std::less<alloc_int>, AllocIntAllocator> FlatSet;
135typedef flat_multimap<alloc_int, alloc_int, std::less<alloc_int>, FlatMapAllocator> FlatMultiMap;
136typedef flat_multiset<alloc_int, std::less<alloc_int>, AllocIntAllocator> FlatMultiSet;
137
138//vector, deque, list, slist, stable_vector.
139typedef vector<alloc_int, AllocIntAllocator> Vector;
140typedef deque<alloc_int, AllocIntAllocator> Deque;
141typedef list<alloc_int, AllocIntAllocator> List;
142typedef slist<alloc_int, AllocIntAllocator> Slist;
143typedef stable_vector<alloc_int, AllocIntAllocator> StableVector;
144typedef small_vector<alloc_int, 9, AllocIntAllocator> SmallVector;
145
146/////////
147//is_unique_assoc
148/////////
149
150template<class T>
151struct is_unique_assoc
152{
153 static const bool value = false;
154};
155
156template<class Key, class T, class Compare, class Allocator>
157struct is_unique_assoc< map<Key, T, Compare, Allocator> >
158{
159 static const bool value = true;
160};
161
162template<class Key, class T, class Compare, class Allocator>
163struct is_unique_assoc< flat_map<Key, T, Compare, Allocator> >
164{
165 static const bool value = true;
166};
167
168template<class Key, class Compare, class Allocator>
169struct is_unique_assoc< set<Key, Compare, Allocator> >
170{
171 static const bool value = true;
172};
173
174template<class Key, class Compare, class Allocator>
175struct is_unique_assoc< flat_set<Key, Compare, Allocator> >
176{
177 static const bool value = true;
178};
179
180
181/////////
182//is_map
183/////////
184
185template<class T>
186struct is_map
187{
188 static const bool value = false;
189};
190
191template<class Key, class T, class Compare, class Allocator>
192struct is_map< map<Key, T, Compare, Allocator> >
193{
194 static const bool value = true;
195};
196
197template<class Key, class T, class Compare, class Allocator>
198struct is_map< flat_map<Key, T, Compare, Allocator> >
199{
200 static const bool value = true;
201};
202
203template<class Key, class T, class Compare, class Allocator>
204struct is_map< multimap<Key, T, Compare, Allocator> >
205{
206 static const bool value = true;
207};
208
209template<class Key, class T, class Compare, class Allocator>
210struct is_map< flat_multimap<Key, T, Compare, Allocator> >
211{
212 static const bool value = true;
213};
214
215template<class T>
216struct is_set
217{
218 static const bool value = false;
219};
220
221template<class Key, class Compare, class Allocator>
222struct is_set< set<Key, Compare, Allocator> >
223{
224 static const bool value = true;
225};
226
227template<class Key, class Compare, class Allocator>
228struct is_set< flat_set<Key, Compare, Allocator> >
229{
230 static const bool value = true;
231};
232
233template<class Key, class Compare, class Allocator>
234struct is_set< multiset<Key, Compare, Allocator> >
235{
236 static const bool value = true;
237};
238
239template<class Key, class Compare, class Allocator>
240struct 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
250template< 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 >
255struct 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
265template<class Container> //map
266struct 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
276template<class Container> //set
277struct 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
287template<class Container> //multimap
288struct 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
299template<class Container> //multimap
300struct 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
310template< class Container>
311struct 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
340bool 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
343template<class F, class S>
11fdf7f2 344bool test_value_and_state_equals(const dtl::pair<F, S> &p, int value, int state)
7c673cae
FG
345{ return test_value_and_state_equals(p.first, value, state) && test_alloc_state_equals(p.second, value, state); }
346
347template<class F, class S>
348bool 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
351template<class Container>
352bool 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
392int 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>