fix doc example typo
[boost.git] / boost / interprocess / containers / container / detail / iterators.hpp
blob64841b14dc7ef8c4dd6dd5ae9b0a376d537456de
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2008.
4 // (C) Copyright Gennaro Prota 2003 - 2004.
5 //
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)
9 //
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)
18 # pragma once
19 #endif
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>
27 #else
28 #include <boost/interprocess/containers/container/detail/preprocessor.hpp>
29 #endif
31 #include <iterator>
33 namespace boost {
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;
43 public:
44 explicit constant_iterator(const T &ref, Difference range_size)
45 : m_ptr(&ref), m_num(range_size){}
47 //Constructors
48 constant_iterator()
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);
57 increment();
58 return result;
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)
71 { return i2 < i; }
73 friend bool operator<= (const constant_iterator& i, const constant_iterator& i2)
74 { return !(i > i2); }
76 friend bool operator>= (const constant_iterator& i, const constant_iterator& i2)
77 { return !(i < i2); }
79 friend Difference operator- (const constant_iterator& i, const constant_iterator& i2)
80 { return i2.distance_to(i); }
82 //Arithmetic
83 constant_iterator& operator+=(Difference off)
84 { this->advance(off); return *this; }
86 constant_iterator operator+(Difference off) const
88 constant_iterator other(*this);
89 other.advance(off);
90 return other;
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()); }
108 private:
109 const T * m_ptr;
110 Difference m_num;
112 void increment()
113 { --m_num; }
115 void decrement()
116 { ++m_num; }
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
125 { return *m_ptr; }
127 void advance(Difference n)
128 { m_num -= 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;
141 public:
142 explicit default_construct_iterator(Difference range_size)
143 : m_num(range_size){}
145 //Constructors
146 default_construct_iterator()
147 : m_num(0){}
149 default_construct_iterator& operator++()
150 { increment(); return *this; }
152 default_construct_iterator operator++(int)
154 default_construct_iterator result (*this);
155 increment();
156 return result;
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)
169 { return i2 < i; }
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); }
180 //Arithmetic
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);
187 other.advance(off);
188 return other;
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()); }
206 private:
207 Difference m_num;
209 void increment()
210 { --m_num; }
212 void decrement()
213 { ++m_num; }
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
223 static T dummy;
224 return dummy;
227 void advance(Difference n)
228 { m_num -= 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;
240 public:
241 explicit repeat_iterator(T &ref, Difference range_size)
242 : m_ptr(&ref), m_num(range_size){}
244 //Constructors
245 repeat_iterator()
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);
254 increment();
255 return result;
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)
268 { return i2 < i; }
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); }
279 //Arithmetic
280 this_type& operator+=(Difference off)
281 { this->advance(off); return *this; }
283 this_type operator+(Difference off) const
285 this_type other(*this);
286 other.advance(off);
287 return other;
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); }
299 T& operator*() const
300 { return dereference(); }
302 T *operator->() const
303 { return &(dereference()); }
305 private:
306 T * m_ptr;
307 Difference m_num;
309 void increment()
310 { --m_num; }
312 void decrement()
313 { ++m_num; }
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
322 { return *m_ptr; }
324 void advance(Difference n)
325 { m_num -= 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;
338 public:
339 explicit emplace_iterator(E&e)
340 : m_num(1), m_pe(&e){}
342 emplace_iterator()
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);
351 increment();
352 return result;
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)
365 { return i2 < i; }
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); }
376 //Arithmetic
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);
383 other.advance(off);
384 return other;
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)
403 { (*m_pe)(ptr); }
405 private:
406 std::ptrdiff_t m_num;
407 E * m_pe;
409 void increment()
410 { --m_num; }
412 void decrement()
413 { ++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
423 static T dummy;
424 return dummy;
427 void advance(std::ptrdiff_t n)
428 { m_num -= 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)
442 : 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_;
455 #else
457 template<class T>
458 struct emplace_functor
460 emplace_functor()
462 void operator()(T *ptr)
463 { new(ptr) T(); }
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, _) \
479 }; \
481 #define BOOST_PP_LOCAL_LIMITS (1, BOOST_CONTAINERS_MAX_CONSTRUCTOR_PARAMETERS)
482 #include BOOST_PP_LOCAL_ITERATE()
484 #endif
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