Nugget
generic_iterator.h
1 // Copyright (c) Electronic Arts Inc. All rights reserved.
4 
6 // Implements a generic iterator from a given iteratable type, such as a pointer.
7 // We cannot put this file into our own iterator.h file because we need to
8 // still be able to use this file when we have our iterator.h disabled.
9 //
11 
12 
13 #ifndef EASTL_INTERNAL_GENERIC_ITERATOR_H
14 #define EASTL_INTERNAL_GENERIC_ITERATOR_H
15 
16 
17 #include <EABase/eabase.h>
18 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
19  #pragma once
20 #endif
21 
22 #include <EASTL/internal/config.h>
23 #include <EASTL/iterator.h>
24 #include <EASTL/type_traits.h>
25 
26 // There is no warning number 'number'.
27 // Member template functions cannot be used for copy-assignment or copy-construction.
28 EA_DISABLE_VC_WARNING(4619 4217);
29 
30 
31 namespace eastl
32 {
33 
46  template <typename Iterator, typename Container = void>
48  {
49  protected:
50  Iterator mIterator;
51 
52  public:
53  typedef typename eastl::iterator_traits<Iterator>::iterator_category iterator_category;
54  typedef typename eastl::iterator_traits<Iterator>::value_type value_type;
55  typedef typename eastl::iterator_traits<Iterator>::difference_type difference_type;
56  typedef typename eastl::iterator_traits<Iterator>::reference reference;
57  typedef typename eastl::iterator_traits<Iterator>::pointer pointer;
58  typedef Iterator iterator_type;
59  typedef iterator_type wrapped_iterator_type; // This is not in the C++ Standard; it's used by use to identify it as a wrapping iterator type.
60  typedef Container container_type;
62 
64  : mIterator(iterator_type()) { }
65 
66  explicit generic_iterator(const iterator_type& x)
67  : mIterator(x) { }
68 
69  this_type& operator=(const iterator_type& x)
70  { mIterator = x; return *this; }
71 
72  template <typename Iterator2>
74  : mIterator(x.base()) { }
75 
76  reference operator*() const
77  { return *mIterator; }
78 
79  pointer operator->() const
80  { return mIterator; }
81 
82  this_type& operator++()
83  { ++mIterator; return *this; }
84 
85  this_type operator++(int)
86  { return this_type(mIterator++); }
87 
88  this_type& operator--()
89  { --mIterator; return *this; }
90 
91  this_type operator--(int)
92  { return this_type(mIterator--); }
93 
94  reference operator[](const difference_type& n) const
95  { return mIterator[n]; }
96 
97  this_type& operator+=(const difference_type& n)
98  { mIterator += n; return *this; }
99 
100  this_type operator+(const difference_type& n) const
101  { return this_type(mIterator + n); }
102 
103  this_type& operator-=(const difference_type& n)
104  { mIterator -= n; return *this; }
105 
106  this_type operator-(const difference_type& n) const
107  { return this_type(mIterator - n); }
108 
109  const iterator_type& base() const
110  { return mIterator; }
111 
112  }; // class generic_iterator
113 
114 
115  template <typename IteratorL, typename IteratorR, typename Container>
116  inline bool operator==(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
117  { return lhs.base() == rhs.base(); }
118 
119  template <typename Iterator, typename Container>
120  inline bool operator==(const generic_iterator<Iterator, Container>& lhs, const generic_iterator<Iterator, Container>& rhs)
121  { return lhs.base() == rhs.base(); }
122 
123  template <typename IteratorL, typename IteratorR, typename Container>
124  inline bool operator!=(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
125  { return lhs.base() != rhs.base(); }
126 
127  template <typename Iterator, typename Container>
128  inline bool operator!=(const generic_iterator<Iterator, Container>& lhs, const generic_iterator<Iterator, Container>& rhs)
129  { return lhs.base() != rhs.base(); }
130 
131  template <typename IteratorL, typename IteratorR, typename Container>
132  inline bool operator<(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
133  { return lhs.base() < rhs.base(); }
134 
135  template <typename Iterator, typename Container>
136  inline bool operator<(const generic_iterator<Iterator, Container>& lhs, const generic_iterator<Iterator, Container>& rhs)
137  { return lhs.base() < rhs.base(); }
138 
139  template <typename IteratorL, typename IteratorR, typename Container>
140  inline bool operator>(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
141  { return lhs.base() > rhs.base(); }
142 
143  template <typename Iterator, typename Container>
144  inline bool operator>(const generic_iterator<Iterator, Container>& lhs, const generic_iterator<Iterator, Container>& rhs)
145  { return lhs.base() > rhs.base(); }
146 
147  template <typename IteratorL, typename IteratorR, typename Container>
148  inline bool operator<=(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
149  { return lhs.base() <= rhs.base(); }
150 
151  template <typename Iterator, typename Container>
152  inline bool operator<=(const generic_iterator<Iterator, Container>& lhs, const generic_iterator<Iterator, Container>& rhs)
153  { return lhs.base() <= rhs.base(); }
154 
155  template <typename IteratorL, typename IteratorR, typename Container>
156  inline bool operator>=(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
157  { return lhs.base() >= rhs.base(); }
158 
159  template <typename Iterator, typename Container>
160  inline bool operator>=(const generic_iterator<Iterator, Container>& lhs, const generic_iterator<Iterator, Container>& rhs)
161  { return lhs.base() >= rhs.base(); }
162 
163  template <typename IteratorL, typename IteratorR, typename Container>
164  inline typename generic_iterator<IteratorL, Container>::difference_type
165  operator-(const generic_iterator<IteratorL, Container>& lhs, const generic_iterator<IteratorR, Container>& rhs)
166  { return lhs.base() - rhs.base(); }
167 
168  template <typename Iterator, typename Container>
169  inline generic_iterator<Iterator, Container>
170  operator+(typename generic_iterator<Iterator, Container>::difference_type n, const generic_iterator<Iterator, Container>& x)
171  { return generic_iterator<Iterator, Container>(x.base() + n); }
172 
173 
174 
181  template <typename Iterator>
182  struct is_generic_iterator : public false_type { };
183 
184  template <typename Iterator, typename Container>
185  struct is_generic_iterator<generic_iterator<Iterator, Container> > : public true_type { };
186 
187 
197  template <typename Iterator>
200 
201 
202 } // namespace eastl
203 
204 
205 EA_RESTORE_VC_WARNING();
206 
207 
208 #endif // Header include guard
Definition: generic_iterator.h:48
EA Standard Template Library.
Definition: algorithm.h:288
eastl::is_iterator_wrapper_helper< Iterator, eastl::is_generic_iterator< Iterator >::value >::iterator_type unwrap_generic_iterator(Iterator it)
Definition: generic_iterator.h:198
Definition: type_traits.h:263
Definition: generic_iterator.h:182
Definition: iterator.h:182