1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2008.
4 // (C) Copyright Gennaro Prota 2003 - 2004.
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)
10 // See http://www.boost.org/libs/container for documentation.
12 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_CONTAINERS_DETAIL_ITERATORS_HPP
15 #define BOOST_CONTAINERS_DETAIL_ITERATORS_HPP
17 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
21 #include <boost/interprocess/containers/container/detail/config_begin.hpp>
22 #include <boost/interprocess/containers/container/detail/workaround.hpp>
23 #include <boost/interprocess/detail/move.hpp>
25 #ifdef BOOST_CONTAINERS_PERFECT_FORWARDING
26 #include <boost/interprocess/containers/container/detail/variadic_templates_tools.hpp>
28 #include <boost/interprocess/containers/container/detail/preprocessor.hpp>
34 namespace interprocess_container
{
36 template <class T
, class Difference
= std::ptrdiff_t>
37 class constant_iterator
38 : public std::iterator
39 <std::random_access_iterator_tag
, T
, Difference
, const T
*, const T
&>
41 typedef constant_iterator
<T
, Difference
> this_type
;
44 explicit constant_iterator(const T
&ref
, Difference range_size
)
45 : m_ptr(&ref
), m_num(range_size
){}
49 : m_ptr(0), m_num(0){}
51 constant_iterator
& operator++()
52 { increment(); return *this; }
54 constant_iterator
operator++(int)
56 constant_iterator
result (*this);
61 friend bool operator== (const constant_iterator
& i
, const constant_iterator
& i2
)
62 { return i
.equal(i2
); }
64 friend bool operator!= (const constant_iterator
& i
, const constant_iterator
& i2
)
65 { return !(i
== i2
); }
67 friend bool operator< (const constant_iterator
& i
, const constant_iterator
& i2
)
68 { return i
.less(i2
); }
70 friend bool operator> (const constant_iterator
& i
, const constant_iterator
& i2
)
73 friend bool operator<= (const constant_iterator
& i
, const constant_iterator
& i2
)
76 friend bool operator>= (const constant_iterator
& i
, const constant_iterator
& i2
)
79 friend Difference
operator- (const constant_iterator
& i
, const constant_iterator
& i2
)
80 { return i2
.distance_to(i
); }
83 constant_iterator
& operator+=(Difference off
)
84 { this->advance(off
); return *this; }
86 constant_iterator
operator+(Difference off
) const
88 constant_iterator
other(*this);
93 friend constant_iterator
operator+(Difference off
, const constant_iterator
& right
)
94 { return right
+ off
; }
96 constant_iterator
& operator-=(Difference off
)
97 { this->advance(-off
); return *this; }
99 constant_iterator
operator-(Difference off
) const
100 { return *this + (-off
); }
102 const T
& operator*() const
103 { return dereference(); }
105 const T
* operator->() const
106 { return &(dereference()); }
118 bool equal(const this_type
&other
) const
119 { return m_num
== other
.m_num
; }
121 bool less(const this_type
&other
) const
122 { return other
.m_num
< m_num
; }
124 const T
& dereference() const
127 void advance(Difference n
)
130 Difference
distance_to(const this_type
&other
)const
131 { return m_num
- other
.m_num
; }
134 template <class T
, class Difference
= std::ptrdiff_t>
135 class default_construct_iterator
136 : public std::iterator
137 <std::random_access_iterator_tag
, T
, Difference
, const T
*, const T
&>
139 typedef default_construct_iterator
<T
, Difference
> this_type
;
142 explicit default_construct_iterator(Difference range_size
)
143 : m_num(range_size
){}
146 default_construct_iterator()
149 default_construct_iterator
& operator++()
150 { increment(); return *this; }
152 default_construct_iterator
operator++(int)
154 default_construct_iterator
result (*this);
159 friend bool operator== (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
160 { return i
.equal(i2
); }
162 friend bool operator!= (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
163 { return !(i
== i2
); }
165 friend bool operator< (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
166 { return i
.less(i2
); }
168 friend bool operator> (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
171 friend bool operator<= (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
172 { return !(i
> i2
); }
174 friend bool operator>= (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
175 { return !(i
< i2
); }
177 friend Difference
operator- (const default_construct_iterator
& i
, const default_construct_iterator
& i2
)
178 { return i2
.distance_to(i
); }
181 default_construct_iterator
& operator+=(Difference off
)
182 { this->advance(off
); return *this; }
184 default_construct_iterator
operator+(Difference off
) const
186 default_construct_iterator
other(*this);
191 friend default_construct_iterator
operator+(Difference off
, const default_construct_iterator
& right
)
192 { return right
+ off
; }
194 default_construct_iterator
& operator-=(Difference off
)
195 { this->advance(-off
); return *this; }
197 default_construct_iterator
operator-(Difference off
) const
198 { return *this + (-off
); }
200 const T
& operator*() const
201 { return dereference(); }
203 const T
* operator->() const
204 { return &(dereference()); }
215 bool equal(const this_type
&other
) const
216 { return m_num
== other
.m_num
; }
218 bool less(const this_type
&other
) const
219 { return other
.m_num
< m_num
; }
221 const T
& dereference() const
227 void advance(Difference n
)
230 Difference
distance_to(const this_type
&other
)const
231 { return m_num
- other
.m_num
; }
234 template <class T
, class Difference
= std::ptrdiff_t>
235 class repeat_iterator
236 : public std::iterator
237 <std::random_access_iterator_tag
, T
, Difference
>
239 typedef repeat_iterator
<T
, Difference
> this_type
;
241 explicit repeat_iterator(T
&ref
, Difference range_size
)
242 : m_ptr(&ref
), m_num(range_size
){}
246 : m_ptr(0), m_num(0){}
248 this_type
& operator++()
249 { increment(); return *this; }
251 this_type
operator++(int)
253 this_type
result (*this);
258 friend bool operator== (const this_type
& i
, const this_type
& i2
)
259 { return i
.equal(i2
); }
261 friend bool operator!= (const this_type
& i
, const this_type
& i2
)
262 { return !(i
== i2
); }
264 friend bool operator< (const this_type
& i
, const this_type
& i2
)
265 { return i
.less(i2
); }
267 friend bool operator> (const this_type
& i
, const this_type
& i2
)
270 friend bool operator<= (const this_type
& i
, const this_type
& i2
)
271 { return !(i
> i2
); }
273 friend bool operator>= (const this_type
& i
, const this_type
& i2
)
274 { return !(i
< i2
); }
276 friend Difference
operator- (const this_type
& i
, const this_type
& i2
)
277 { return i2
.distance_to(i
); }
280 this_type
& operator+=(Difference off
)
281 { this->advance(off
); return *this; }
283 this_type
operator+(Difference off
) const
285 this_type
other(*this);
290 friend this_type
operator+(Difference off
, const this_type
& right
)
291 { return right
+ off
; }
293 this_type
& operator-=(Difference off
)
294 { this->advance(-off
); return *this; }
296 this_type
operator-(Difference off
) const
297 { return *this + (-off
); }
300 { return dereference(); }
302 T
*operator->() const
303 { return &(dereference()); }
315 bool equal(const this_type
&other
) const
316 { return m_num
== other
.m_num
; }
318 bool less(const this_type
&other
) const
319 { return other
.m_num
< m_num
; }
321 T
& dereference() const
324 void advance(Difference n
)
327 Difference
distance_to(const this_type
&other
)const
328 { return m_num
- other
.m_num
; }
331 template <class T
, class E
>
332 class emplace_iterator
333 : public std::iterator
334 <std::random_access_iterator_tag
, T
, std::ptrdiff_t, const T
*, const T
&>
336 typedef emplace_iterator this_type
;
339 explicit emplace_iterator(E
&e
)
340 : m_num(1), m_pe(&e
){}
343 : m_num(0), m_pe(0){}
345 this_type
& operator++()
346 { increment(); return *this; }
348 this_type
operator++(int)
350 this_type
result (*this);
355 friend bool operator== (const this_type
& i
, const this_type
& i2
)
356 { return i
.equal(i2
); }
358 friend bool operator!= (const this_type
& i
, const this_type
& i2
)
359 { return !(i
== i2
); }
361 friend bool operator< (const this_type
& i
, const this_type
& i2
)
362 { return i
.less(i2
); }
364 friend bool operator> (const this_type
& i
, const this_type
& i2
)
367 friend bool operator<= (const this_type
& i
, const this_type
& i2
)
368 { return !(i
> i2
); }
370 friend bool operator>= (const this_type
& i
, const this_type
& i2
)
371 { return !(i
< i2
); }
373 friend std::ptrdiff_t operator- (const this_type
& i
, const this_type
& i2
)
374 { return i2
.distance_to(i
); }
377 this_type
& operator+=(std::ptrdiff_t off
)
378 { this->advance(off
); return *this; }
380 this_type
operator+(std::ptrdiff_t off
) const
382 this_type
other(*this);
387 friend this_type
operator+(std::ptrdiff_t off
, const this_type
& right
)
388 { return right
+ off
; }
390 this_type
& operator-=(std::ptrdiff_t off
)
391 { this->advance(-off
); return *this; }
393 this_type
operator-(std::ptrdiff_t off
) const
394 { return *this + (-off
); }
396 const T
& operator*() const
397 { return dereference(); }
399 const T
* operator->() const
400 { return &(dereference()); }
402 void construct_in_place(T
* ptr
)
406 std::ptrdiff_t m_num
;
415 bool equal(const this_type
&other
) const
416 { return m_num
== other
.m_num
; }
418 bool less(const this_type
&other
) const
419 { return other
.m_num
< m_num
; }
421 const T
& dereference() const
427 void advance(std::ptrdiff_t n
)
430 std::ptrdiff_t distance_to(const this_type
&other
)const
431 { return m_num
- other
.m_num
; }
434 #ifdef BOOST_CONTAINERS_PERFECT_FORWARDING
436 template<class T
, class ...Args
>
437 struct emplace_functor
439 typedef typename
containers_detail::build_number_seq
<sizeof...(Args
)>::type index_tuple_t
;
441 emplace_functor(Args
&&... args
)
445 void operator()(T
*ptr
)
446 { emplace_functor::inplace_impl(ptr
, index_tuple_t()); }
448 template<int ...IdxPack
>
449 void inplace_impl(T
* ptr
, const containers_detail::index_tuple
<IdxPack
...>&)
450 { ::new(ptr
) T(boost::interprocess::forward
<Args
>(containers_detail::get
<IdxPack
>(args_
))...); }
452 containers_detail::tuple
<Args
&&...> args_
;
458 struct emplace_functor
462 void operator()(T
*ptr
)
466 #define BOOST_PP_LOCAL_MACRO(n) \
467 template <class T, BOOST_PP_ENUM_PARAMS(n, class P) > \
468 struct BOOST_PP_CAT(BOOST_PP_CAT(emplace_functor, n), arg) \
470 BOOST_PP_CAT(BOOST_PP_CAT(emplace_functor, n), arg) \
471 ( BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_PARAM_LIST, _) ) \
472 : BOOST_PP_ENUM(n, BOOST_CONTAINERS_AUX_PARAM_INIT, _) {} \
474 void operator()(T *ptr) \
476 new(ptr)T (BOOST_PP_ENUM(n, BOOST_CONTAINERS_PP_MEMBER_FORWARD, _)); \
478 BOOST_PP_REPEAT(n, BOOST_CONTAINERS_AUX_PARAM_DEFINE, _) \
481 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINERS_MAX_CONSTRUCTOR_PARAMETERS)
482 #include BOOST_PP_LOCAL_ITERATE()
486 } //namespace interprocess_container {
487 } //namespace boost {
489 #include <boost/interprocess/containers/container/detail/config_end.hpp>
491 #endif //#ifndef BOOST_CONTAINERS_DETAIL_ITERATORS_HPP