libpqxx  7.0.1
result_iterator.hxx
1 /* Definitions for the pqxx::result class and support classes.
2  *
3  * pqxx::result represents the set of result rows from a database query.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result instead.
6  *
7  * Copyright (c) 2000-2020, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_RESULT_ITERATOR
14 #define PQXX_H_RESULT_ITERATOR
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include "pqxx/row.hxx"
20 
21 
22 /* Result iterator.
23  *
24  * Don't include this header from your own application; it is included for you
25  * by other libpqxx headers.
26  */
27 
28 namespace pqxx
29 {
31 
35 class PQXX_LIBEXPORT const_result_iterator : public row
36 {
37 public:
38  using iterator_category = std::random_access_iterator_tag;
39  using value_type = row const;
40  using pointer = row const *;
41  using reference = row;
44 
45  const_result_iterator() noexcept : row{result(), 0} {}
46  const_result_iterator(row const &t) noexcept : row{t} {}
47 
63  [[nodiscard]] pointer operator->() const { return this; }
64  [[nodiscard]] reference operator*() const { return row{*this}; }
66 
71  const_result_iterator operator++(int);
73  {
74  ++m_index;
75  return *this;
76  }
77  const_result_iterator operator--(int);
79  {
80  --m_index;
81  return *this;
82  }
83 
85  {
86  m_index += i;
87  return *this;
88  }
90  {
91  m_index -= i;
92  return *this;
93  }
95 
100  [[nodiscard]] bool operator==(const_result_iterator const &i) const
101  {
102  return m_index == i.m_index;
103  }
104  [[nodiscard]] bool operator!=(const_result_iterator const &i) const
105  {
106  return m_index != i.m_index;
107  }
108  [[nodiscard]] bool operator<(const_result_iterator const &i) const
109  {
110  return m_index < i.m_index;
111  }
112  [[nodiscard]] bool operator<=(const_result_iterator const &i) const
113  {
114  return m_index <= i.m_index;
115  }
116  [[nodiscard]] bool operator>(const_result_iterator const &i) const
117  {
118  return m_index > i.m_index;
119  }
120  [[nodiscard]] bool operator>=(const_result_iterator const &i) const
121  {
122  return m_index >= i.m_index;
123  }
125 
130  [[nodiscard]] inline const_result_iterator operator+(difference_type) const;
131  friend const_result_iterator
133  [[nodiscard]] inline const_result_iterator operator-(difference_type) const;
134  [[nodiscard]] inline difference_type
135  operator-(const_result_iterator const &) const;
137 
138 private:
139  friend class pqxx::result;
140  const_result_iterator(pqxx::result const *r, result_size_type i) noexcept :
141  row{*r, i}
142  {}
143 };
144 
145 
147 class PQXX_LIBEXPORT const_reverse_result_iterator
148  : private const_result_iterator
149 {
150 public:
158 
160  default;
163  {
164  super::operator--();
165  }
166 
167  [[nodiscard]] PQXX_PURE const_result_iterator base() const noexcept;
168 
173  using const_result_iterator::operator->;
174  using const_result_iterator::operator*;
176 
183  {
184  iterator_type::operator=(r);
185  return *this;
186  }
188  {
189  iterator_type::operator--();
190  return *this;
191  }
192  const_reverse_result_iterator operator++(int);
194  {
195  iterator_type::operator++();
196  return *this;
197  }
198  const_reverse_result_iterator operator--(int);
200  {
201  iterator_type::operator-=(i);
202  return *this;
203  }
205  {
206  iterator_type::operator+=(i);
207  return *this;
208  }
210 
215  [[nodiscard]] const_reverse_result_iterator
217  {
218  return const_reverse_result_iterator(base() - i);
219  }
221  {
222  return const_reverse_result_iterator(base() + i);
223  }
224  [[nodiscard]] difference_type
226  {
227  return rhs.const_result_iterator::operator-(*this);
228  }
230 
235  [[nodiscard]] bool operator==(const_reverse_result_iterator const &rhs) const
236  noexcept
237  {
238  return iterator_type::operator==(rhs);
239  }
240  [[nodiscard]] bool operator!=(const_reverse_result_iterator const &rhs) const
241  noexcept
242  {
243  return not operator==(rhs);
244  }
245 
246  [[nodiscard]] bool operator<(const_reverse_result_iterator const &rhs) const
247  {
248  return iterator_type::operator>(rhs);
249  }
250  [[nodiscard]] bool operator<=(const_reverse_result_iterator const &rhs) const
251  {
252  return iterator_type::operator>=(rhs);
253  }
254  [[nodiscard]] bool operator>(const_reverse_result_iterator const &rhs) const
255  {
256  return iterator_type::operator<(rhs);
257  }
258  [[nodiscard]] bool operator>=(const_reverse_result_iterator const &rhs) const
259  {
260  return iterator_type::operator<=(rhs);
261  }
263 };
264 
265 
268 {
269  return const_result_iterator{
270  &m_result, size_type(result::difference_type(m_index) + o)};
271 }
272 
275 {
276  return i + o;
277 }
278 
281 {
282  return const_result_iterator{
283  &m_result, result_size_type(result::difference_type(m_index) - o)};
284 }
285 
288 {
289  return result::difference_type(num() - i.num());
290 }
291 
292 inline const_result_iterator result::end() const noexcept
293 {
294  return const_result_iterator{this, size()};
295 }
296 
297 
298 inline const_result_iterator result::cend() const noexcept
299 {
300  return end();
301 }
302 
303 
306 {
307  return const_reverse_result_iterator{i.base() - n};
308 }
309 
310 } // namespace pqxx
311 
312 #include "pqxx/internal/compiler-internal-post.hxx"
313 #endif
const_result_iterator() noexcept
Definition: result_iterator.hxx:45
bool operator==(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:235
bool operator>(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:254
const_reverse_result_iterator operator-(difference_type i)
Definition: result_iterator.hxx:220
bool operator!=(const_result_iterator const &i) const
Definition: result_iterator.hxx:104
bool operator<(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:246
bool operator==(const_result_iterator const &i) const
Definition: result_iterator.hxx:100
bool operator>=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:258
const_result_iterator & operator++()
Definition: result_iterator.hxx:72
int result_difference_type
Difference between result sizes.
Definition: types.hxx:21
bool operator<=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:250
bool operator>=(const_result_iterator const &i) const
Definition: result_iterator.hxx:120
const_reverse_result_iterator & operator++()
Definition: result_iterator.hxx:187
const_result_iterator operator+(difference_type) const
Definition: result_iterator.hxx:267
std::random_access_iterator_tag iterator_category
Definition: result_iterator.hxx:38
result_difference_type difference_type
Definition: result.hxx:74
PQXX_PURE const_result_iterator base() const noexcept
Definition: result.cxx:452
result_difference_type difference_type
Definition: result_iterator.hxx:43
row const * pointer
Definition: result_iterator.hxx:40
row const value_type
Definition: result_iterator.hxx:39
const_result_iterator(row const &t) noexcept
Definition: result_iterator.hxx:46
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:274
const_result_iterator & operator--()
Definition: result_iterator.hxx:78
const_reverse_result_iterator operator+(difference_type i) const
Definition: result_iterator.hxx:216
const_reverse_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:199
bool operator!=(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:240
const_reverse_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:204
result_size_type size_type
Definition: result_iterator.hxx:42
const_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:89
const_iterator end() const noexcept
Definition: result_iterator.hxx:292
result::size_type num() const
Definition: row.hxx:162
const_reverse_result_iterator & operator--()
Definition: result_iterator.hxx:193
Result set containing data returned by a query or command.
Definition: result.hxx:70
bool operator<(const_result_iterator const &i) const
Definition: result_iterator.hxx:108
difference_type operator-(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:225
const_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:84
bool operator>(const_result_iterator const &i) const
Definition: result_iterator.hxx:116
const_reverse_result_iterator & operator=(const_reverse_result_iterator const &r)
Definition: result_iterator.hxx:182
Reference to one row in a result.
Definition: row.hxx:38
const_result_iterator operator-(difference_type) const
Definition: result_iterator.hxx:280
reference operator*() const
Definition: result_iterator.hxx:64
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:147
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
const_iterator cend() const noexcept
Definition: result_iterator.hxx:298
const_reverse_result_iterator(const_result_iterator const &rhs)
Definition: result_iterator.hxx:161
bool operator<=(const_result_iterator const &i) const
Definition: result_iterator.hxx:112
result::size_type m_index
Row number.
Definition: row.hxx:194
pointer operator->() const
Definition: result_iterator.hxx:63
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35