1///////////////////////////////////////////////////////////////////////////////
2//
3// (C) Copyright Ion Gaztanaga 2005-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
11#ifndef BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP
12#define BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP
13
14#ifndef BOOST_CONFIG_HPP
15# include <boost/config.hpp>
16#endif
17
18#if defined(BOOST_HAS_PRAGMA_ONCE)
19# pragma once
20#endif
21
22#include <boost/container/detail/config_begin.hpp>
23#include <boost/container/detail/workaround.hpp>
24#include <boost/container/container_fwd.hpp>
25
26#include <boost/container/throw_exception.hpp>
27
28#include <boost/container/detail/addressof.hpp>
29#include <boost/container/detail/allocation_type.hpp>
30#include <boost/container/detail/mpl.hpp>
31#include <boost/container/detail/multiallocation_chain.hpp>
32#include <boost/container/detail/type_traits.hpp>
33#include <boost/container/detail/version_type.hpp>
34
35#include <boost/move/utility_core.hpp>
36#include <boost/move/adl_move_swap.hpp>
37#include <boost/move/detail/force_ptr.hpp>
38#include <boost/assert.hpp>
39
40#include <memory>
41#include <algorithm>
42#include <cstddef>
43#include <cassert>
44
45namespace boost {
46namespace container {
47namespace test {
48
49//Very simple version 1 allocator
50template<class T>
51class simple_allocator
52{
53 public:
54 typedef T value_type;
55
56 simple_allocator()
57 {}
58
59 template<class U>
60 simple_allocator(const simple_allocator<U> &)
61 {}
62
63 T* allocate(std::size_t n)
64 { return move_detail::force_ptr<T*>(::new char[sizeof(T)*n]); }
65
66 void deallocate(T*p, std::size_t)
67 { delete[] ((char*)p);}
68
69 friend bool operator==(const simple_allocator &, const simple_allocator &)
70 { return true; }
71
72 friend bool operator!=(const simple_allocator &, const simple_allocator &)
73 { return false; }
74};
75
76template< class T
77 , bool PropagateOnContCopyAssign
78 , bool PropagateOnContMoveAssign
79 , bool PropagateOnContSwap
80 , bool CopyOnPropagateOnContSwap
81 , bool EqualIfEqualIds
82 >
83class propagation_test_allocator
84{
85 BOOST_COPYABLE_AND_MOVABLE(propagation_test_allocator)
86
87 public:
88 typedef T value_type;
89 typedef boost::container::dtl::bool_<PropagateOnContCopyAssign>
90 propagate_on_container_copy_assignment;
91 typedef boost::container::dtl::bool_<PropagateOnContMoveAssign>
92 propagate_on_container_move_assignment;
93 typedef boost::container::dtl::bool_<PropagateOnContSwap>
94 propagate_on_container_swap;
95
96 template<class T2>
97 struct rebind
98 { typedef propagation_test_allocator
99 < T2
100 , PropagateOnContCopyAssign
101 , PropagateOnContMoveAssign
102 , PropagateOnContSwap
103 , CopyOnPropagateOnContSwap
104 , EqualIfEqualIds> other;
105 };
106
107 propagation_test_allocator select_on_container_copy_construction() const
108 { return CopyOnPropagateOnContSwap ? propagation_test_allocator(*this) : propagation_test_allocator(); }
109
110 explicit propagation_test_allocator()
111 : id_(++unique_id_)
112 , ctr_copies_(0)
113 , ctr_moves_(0)
114 , assign_copies_(0)
115 , assign_moves_(0)
116 , swaps_(0)
117 {}
118
119 propagation_test_allocator(const propagation_test_allocator &x)
120 : id_(x.id_)
121 , ctr_copies_(x.ctr_copies_+1)
122 , ctr_moves_(x.ctr_moves_)
123 , assign_copies_(x.assign_copies_)
124 , assign_moves_(x.assign_moves_)
125 , swaps_(x.swaps_)
126 {}
127
128 template<class U>
129 propagation_test_allocator(const propagation_test_allocator
130 < U
131 , PropagateOnContCopyAssign
132 , PropagateOnContMoveAssign
133 , PropagateOnContSwap
134 , CopyOnPropagateOnContSwap
135 , EqualIfEqualIds> &x)
136 : id_(x.id_)
137 , ctr_copies_(x.ctr_copies_+1)
138 , ctr_moves_(0)
139 , assign_copies_(0)
140 , assign_moves_(0)
141 , swaps_(0)
142 {}
143
144 propagation_test_allocator(BOOST_RV_REF(propagation_test_allocator) x)
145 : id_(x.id_)
146 , ctr_copies_(x.ctr_copies_)
147 , ctr_moves_(x.ctr_moves_ + 1)
148 , assign_copies_(x.assign_copies_)
149 , assign_moves_(x.assign_moves_)
150 , swaps_(x.swaps_)
151 {}
152
153 propagation_test_allocator &operator=(BOOST_COPY_ASSIGN_REF(propagation_test_allocator) x)
154 {
155 id_ = x.id_;
156 ctr_copies_ = x.ctr_copies_;
157 ctr_moves_ = x.ctr_moves_;
158 assign_copies_ = x.assign_copies_+1;
159 assign_moves_ = x.assign_moves_;
160 swaps_ = x.swaps_;
161 return *this;
162 }
163
164 propagation_test_allocator &operator=(BOOST_RV_REF(propagation_test_allocator) x)
165 {
166 id_ = x.id_;
167 ctr_copies_ = x.ctr_copies_;
168 ctr_moves_ = x.ctr_moves_;
169 assign_copies_ = x.assign_copies_;
170 assign_moves_ = x.assign_moves_+1;
171 swaps_ = x.swaps_;
172 return *this;
173 }
174
175 static void reset_unique_id(unsigned id = 0)
176 { unique_id_ = id; }
177
178 T* allocate(std::size_t n)
179 { return move_detail::force_ptr<T*>(::new char[sizeof(T)*n]); }
180
181 void deallocate(T*p, std::size_t)
182 { delete[] ((char*)p);}
183
184 friend bool operator==(const propagation_test_allocator &a, const propagation_test_allocator &b)
185 { return EqualIfEqualIds ? a.id_ == b.id_ : true; }
186
187 friend bool operator!=(const propagation_test_allocator &a, const propagation_test_allocator &b)
188 { return EqualIfEqualIds ? a.id_ != b.id_ : false; }
189
190 void swap(propagation_test_allocator &r)
191 {
192 ++this->swaps_; ++r.swaps_;
193 boost::adl_move_swap(this->id_, r.id_);
194 boost::adl_move_swap(this->ctr_copies_, r.ctr_copies_);
195 boost::adl_move_swap(this->ctr_moves_, r.ctr_moves_);
196 boost::adl_move_swap(this->assign_copies_, r.assign_copies_);
197 boost::adl_move_swap(this->assign_moves_, r.assign_moves_);
198 boost::adl_move_swap(this->swaps_, r.swaps_);
199 }
200
201 friend void swap(propagation_test_allocator &l, propagation_test_allocator &r)
202 {
203 l.swap(r);
204 }
205
206 unsigned int id_;
207 unsigned int ctr_copies_;
208 unsigned int ctr_moves_;
209 unsigned int assign_copies_;
210 unsigned int assign_moves_;
211 unsigned int swaps_;
212 static unsigned unique_id_;
213};
214
215template< class T
216 , bool PropagateOnContCopyAssign
217 , bool PropagateOnContMoveAssign
218 , bool PropagateOnContSwap
219 , bool CopyOnPropagateOnContSwap
220 , bool EqualIfEqualIds
221 >
222unsigned int propagation_test_allocator< T
223 , PropagateOnContCopyAssign
224 , PropagateOnContMoveAssign
225 , PropagateOnContSwap
226 , CopyOnPropagateOnContSwap
227 , EqualIfEqualIds
228 >::unique_id_ = 0;
229
230
231} //namespace test {
232} //namespace container {
233} //namespace boost {
234
235#include <boost/container/detail/config_end.hpp>
236
237#endif //BOOST_CONTAINER_DUMMY_TEST_ALLOCATOR_HPP
238
239

source code of boost/libs/container/test/dummy_test_allocator.hpp