shared_container_iterator.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 // (C) Copyright Toon Knapen 2001.
8 // (C) Copyright David Abrahams 2003.
9 // (C) Copyright Roland Richter 2003.
10 // (C) Copyright Renaud Detry 2007-2008.
11 // Distributed under the Boost Software License, Version 1.0. (See
12 // accompanying file LICENSE_1_0.txt or copy at
13 // http://www.boost.org/LICENSE_1_0.txt)
14 
15 /** @file */
16 
17 #ifndef NUKLEI_SHARED_CONTAINER_ITERATOR_HPP
18 #define NUKLEI_SHARED_CONTAINER_ITERATOR_HPP
19 
20 #include <iterator>
21 #include <vector>
22 #include <algorithm>
23 #include <boost/iterator.hpp>
24 #include <boost/iterator/iterator_categories.hpp>
25 #include <boost/iterator/iterator_adaptor.hpp>
26 #include <boost/shared_ptr.hpp>
27 
28 namespace nuklei
29 {
30  template<class Container, class ContainerIterator>
32 
33  namespace detail
34  {
35  /** @brief Used internally. */
36  template<class Container, class ContainerIterator>
38  {
39  typedef Container container_t;
40  typedef ContainerIterator container_iterator;
41  typedef boost::shared_ptr<Container> container_ptr;
42 
43  typedef boost::iterator_adaptor<
45  ContainerIterator,
46  typename boost::detail::iterator_traits<ContainerIterator>::value_type,
47  boost::use_default,
48  typename boost::detail::iterator_traits<ContainerIterator>::reference
49  > type;
50  };
51  }
52 
53 
54  template<class Container, class ContainerIterator>
56  : public detail::shared_container_iterator_base<Container, ContainerIterator>::type
57  {
59  typedef typename base_t::type super_t;
60 
61  friend class boost::iterator_core_access;
62 
63  public:
64 
65  typedef typename base_t::container_t container_t;
66  typedef typename base_t::container_ptr container_ptr;
67  typedef typename base_t::container_iterator container_iterator;
68 
70  m_collection(new container_t)
71  {}
72 
73  shared_container_iterator(container_ptr& index_collection)
74  : super_t(index_collection->begin()),
75  m_collection(index_collection)
76  {}
77 
78  /**
79  * @brief Allows conversion from a shared_container_iterator to
80  * a const shared_container_iterator, won't allow conversion
81  * from a const shared_container_iterator to a
82  * shared_container_iterator.
83  *
84  * By &ldquo;const shared_container_iterator&rdquo;, we mean that the @p
85  * ContainerIterator is const, e.g.
86  * <tt>std::vector<Particle>::const_iterator</tt>.
87  */
88  template<class OtherContainerIterator>
89  shared_container_iterator
91  typename boost::enable_if_convertible<OtherContainerIterator, ContainerIterator>::type* = 0) :
92  super_t(r.base()),
93  m_collection(r.m_collection)
94  {}
95 
96  /**
97  * @brief Returns a shared_container_iterator pointing to
98  * the begining of the range.
99  */
101  {
103  iterator.base_reference() =
104  iterator.m_collection->begin();
105  return iterator;
106  }
107 
108  /**
109  * @brief Returns a shared_container_iterator pointing to
110  * the end of the range.
111  */
113  {
115  iterator.base_reference() =
116  iterator.m_collection->end();
117  return iterator;
118  }
119 
120  private:
121 
122 #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
123  template <class, class> friend class shared_container_iterator;
124 #else
125  public:
126 #endif
127  protected:
128  container_ptr m_collection;
129  };
130 
131 } // namespace nuklei
132 
133 #endif // include guard
Public namespace.
Definition: Color.cpp:9
shared_container_iterator< Container, ContainerIterator > end() const
Returns a shared_container_iterator pointing to the end of the range.
shared_container_iterator< Container, ContainerIterator > begin() const
Returns a shared_container_iterator pointing to the begining of the range.
Used internally.
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.