Debian Codegraph @

src/3rdParty/lru-cache/include/lru/internal/base-iterator.hpp

raw
1 /// The MIT License (MIT)
2 /// Copyright (c) 2016 Peter Goldsborough
3 ///
4 /// Permission is hereby granted, free of charge, to any person obtaining a copy
5 /// of this software and associated documentation files (the "Software"), to
6 /// deal in the Software without restriction, including without limitation the
7 /// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 /// sell copies of the Software, and to permit persons to whom the Software is
9 /// furnished to do so, subject to the following conditions:
10 ///
11 /// The above copyright notice and this permission notice shall be included in
12 /// all copies or substantial portions of the Software.
13 ///
14 /// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 /// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 /// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 /// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 /// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 /// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 /// IN THE SOFTWARE.
21
22 #ifndef LRU_INTERNAL_BASE_ITERATOR_HPP
23 #define LRU_INTERNAL_BASE_ITERATOR_HPP
24
25 #include <algorithm>
26 #include <iterator>
27
28 #include <lru/entry.hpp>
29 #include <lru/internal/optional.hpp>
30
31 #define PUBLIC_BASE_ITERATOR_MEMBERS \
32 typename super::Entry; \
33 using typename super::KeyType; \
34 using typename super::ValueType;
35
36 #define PRIVATE_BASE_ITERATOR_MEMBERS \
37 super::_iterator; \
38 using super::_entry; \
39 using super::_cache;
40
41
42 namespace LRU {
43 namespace Internal {
44
45 /// The base class for all (ordered and unordered) iterators.
46 ///
47 /// All iterators over our LRU caches store a reference to the cache they point
48 /// into, an underlying iterator they adapt (e.g. a map iterator or list
49 /// iterator) as well as a entry, a reference to which is returned when
50 /// dereferencing the iterator.
51 ///
52 /// \tparam IteratorTag A standard iterator category tag.
53 /// \tparam Key The key type over which instances of the iterator iterate.
54 /// \tparam Value The value type over which instances of the iterator iterate.
55 /// \tparam Cache The type of the cache instances of the iterator point into.
56 /// \tparam UnderlyingIterator The underlying iterator class used to implement
57 /// the iterator.
58 template <typename IteratorTag,
59 typename Key,
60 typename Value,
61 typename Cache,
62 typename UnderlyingIterator>
63 class BaseIterator : public std::iterator<IteratorTag, LRU::Entry<Key, Value>> {
64 public:
65 using KeyType = Key;
66 using ValueType =
67 std::conditional_t<std::is_const<Cache>::value, const Value, Value>;
68 using Entry = LRU::Entry<KeyType, ValueType>;
69
70 /// Default constructor.
71 BaseIterator() noexcept : _cache(nullptr) {
72 }
73
74 /// Constructor.
75 ///
76 /// \param cache The cache this iterator points into.
77 /// \param iterator The underlying iterator to adapt.
78 BaseIterator(Cache& cache, const UnderlyingIterator& iterator) noexcept
79 : _iterator(iterator), _cache(&cache) {
80 }
81
82 /// Copy constructor.
83 ///
84 /// Differs from the default copy constructor in that it does not copy the
85 /// entry.
86 ///
87 /// \param other The base iterator to copy.
88 BaseIterator(const BaseIterator& other) noexcept
89 : _iterator(other._iterator), _cache(other._cache) {
90 // Note: we do not copy the entry, as it would require a new allocation.
91 // Since iterators are often taken by value, this may incur a high cost.
92 // As such we delay the retrieval of the entry to the first call to entry().
93 }
94
95 /// Copy assignment operator.
96 ///
97 /// Differs from the default copy assignment
98 /// operator in that it does not copy the entry.
99 ///
100 /// \param other The base iterator to copy.
101 /// \return The base iterator instance.
102 BaseIterator& operator=(const BaseIterator& other) noexcept {
103 if (this != &other) {
104 _iterator = other._iterator;
105 _cache = other._cache;
106 _entry.reset();
107 }
108 return *this;
109 }
110
111 /// Move constructor.
112 BaseIterator(BaseIterator&& other) noexcept = default;
113
114 /// Move assignment operator.
115 BaseIterator& operator=(BaseIterator&& other) noexcept = default;
116
117 /// Generalized copy constructor.
118 ///
119 /// Mainly necessary for non-const to const conversion.
120 ///
121 /// \param other The base iterator to copy from.
122 template <typename AnyIteratorTag,
123 typename AnyKeyType,
124 typename AnyValueType,
125 typename AnyCacheType,
126 typename AnyUnderlyingIteratorType>
127 BaseIterator(const BaseIterator<AnyIteratorTag,
128 AnyKeyType,
129 AnyValueType,
130 AnyCacheType,
131 AnyUnderlyingIteratorType>& other)
132 : _iterator(other._iterator), _entry(other._entry), _cache(other._cache) {
133 }
134
135 /// Generalized move constructor.
136 ///
137 /// Mainly necessary for non-const to const conversion.
138 ///
139 /// \param other The base iterator to move into this one.
140 template <typename AnyIteratorTag,
141 typename AnyKeyType,
142 typename AnyValueType,
143 typename AnyCacheType,
144 typename AnyUnderlyingIteratorType>
145 BaseIterator(BaseIterator<AnyIteratorTag,
146 AnyKeyType,
147 AnyValueType,
148 AnyCacheType,
149 AnyUnderlyingIteratorType>&& other) noexcept
150 : _iterator(std::move(other._iterator))
151 , _entry(std::move(other._entry))
152 , _cache(std::move(other._cache)) {
153 }
154
155 /// Destructor.
156 virtual ~BaseIterator() = default;
157
158 /// Swaps this base iterator with another one.
159 ///
160 /// \param other The other iterator to swap with.
161 void swap(BaseIterator& other) noexcept {
162 // Enable ADL
163 using std::swap;
164
165 swap(_iterator, other._iterator);
166 swap(_entry, other._entry);
167 swap(_cache, other._cache);
168 }
169
170 /// Swaps two base iterator.
171 ///
172 /// \param first The first iterator to swap.
173 /// \param second The second iterator to swap.
174 friend void swap(BaseIterator& first, BaseIterator& second) noexcept {
175 first.swap(second);
176 }
177
178 /// \returns A reference to the current entry pointed to by the iterator.
179 virtual Entry& operator*() noexcept = 0;
180
181 /// \returns A pointer to the current entry pointed to by the iterator.
182 Entry* operator->() noexcept {
183 return &(**this);
184 }
185
186 /// \copydoc operator*()
187 virtual Entry& entry() = 0;
188
189 /// \returns A reference to the value of the entry currently pointed to by the
190 /// iterator.
191 virtual ValueType& value() = 0;
192
193 /// \returns A reference to the key of the entry currently pointed to by the
194 /// iterator.
195 virtual const Key& key() = 0;
196
197 protected:
198 template <typename, typename, typename, typename, typename>
199 friend class BaseIterator;
200
201 /// The underlying iterator this iterator class adapts.
202 UnderlyingIterator _iterator;
203
204 /// The entry optionally being stored.
205 Optional<Entry> _entry;
206
207 /// A pointer to the cache this iterator points into.
208 /// Pointer and not reference because it's cheap to copy.
209 /// Pointer and not `std::reference_wrapper` because the class needs to be
210 /// default-constructible.
211 Cache* _cache;
212 };
213 } // namespace Internal
214 } // namespace LRU
215
216 #endif // LRU_INTERNAL_BASE_ITERATOR_HPP