libpqxx  7.6.0
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-2021, 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 #include "pqxx/internal/ignore-deprecated-pre.hxx"
47  const_result_iterator() noexcept = default;
49  const_result_iterator(const_result_iterator const &) noexcept = default;
51  const_result_iterator(const_result_iterator &&) noexcept = default;
52 
54  const_result_iterator(row const &t) noexcept : row{t} {}
55 #include "pqxx/internal/ignore-deprecated-post.hxx"
56 
71  [[nodiscard]] pointer operator->() const { return this; }
73 
74 #include "pqxx/internal/ignore-deprecated-pre.hxx"
76  [[nodiscard]] reference operator*() const { return row{*this}; }
77 #include "pqxx/internal/ignore-deprecated-post.hxx"
79 
84  using row::back;
85  using row::front;
86  using row::operator[];
87  using row::at;
88  using row::rownumber;
90 
96  {
97 #include "pqxx/internal/ignore-deprecated-pre.hxx"
98  row::operator=(rhs);
99 #include "pqxx/internal/ignore-deprecated-post.hxx"
100  return *this;
101  }
102 
104  {
105 #include "pqxx/internal/ignore-deprecated-pre.hxx"
106  row::operator=(std::move(rhs));
107 #include "pqxx/internal/ignore-deprecated-post.hxx"
108  return *this;
109  }
110 
111  const_result_iterator operator++(int);
113  {
114  ++m_index;
115  return *this;
116  }
117  const_result_iterator operator--(int);
119  {
120  --m_index;
121  return *this;
122  }
123 
125  {
126  m_index += i;
127  return *this;
128  }
130  {
131  m_index -= i;
132  return *this;
133  }
134 
136  void swap(const_result_iterator &other) noexcept
137  {
138 #include "pqxx/internal/ignore-deprecated-pre.hxx"
139  row::swap(other);
140 #include "pqxx/internal/ignore-deprecated-post.hxx"
141  }
143 
148  [[nodiscard]] bool operator==(const_result_iterator const &i) const
149  {
150  return m_index == i.m_index;
151  }
152  [[nodiscard]] bool operator!=(const_result_iterator const &i) const
153  {
154  return m_index != i.m_index;
155  }
156  [[nodiscard]] bool operator<(const_result_iterator const &i) const
157  {
158  return m_index < i.m_index;
159  }
160  [[nodiscard]] bool operator<=(const_result_iterator const &i) const
161  {
162  return m_index <= i.m_index;
163  }
164  [[nodiscard]] bool operator>(const_result_iterator const &i) const
165  {
166  return m_index > i.m_index;
167  }
168  [[nodiscard]] bool operator>=(const_result_iterator const &i) const
169  {
170  return m_index >= i.m_index;
171  }
173 
178  [[nodiscard]] inline const_result_iterator operator+(difference_type) const;
179  friend const_result_iterator
181  [[nodiscard]] inline const_result_iterator operator-(difference_type) const;
182  [[nodiscard]] inline difference_type
183  operator-(const_result_iterator const &) const;
185 
186 private:
187  friend class pqxx::result;
188  const_result_iterator(pqxx::result const *r, result_size_type i) noexcept :
189  row{*r, i}
190  {}
191 };
192 
193 
195 class PQXX_LIBEXPORT const_reverse_result_iterator
196  : private const_result_iterator
197 {
198 public:
206 
208  const_reverse_result_iterator() = default;
211  default;
215  {
216  super::operator--();
217  }
218 
221  const_result_iterator{std::move(rhs)}
222  {
223  super::operator--();
224  }
225 
227  [[nodiscard]] PQXX_PURE const_result_iterator base() const noexcept;
228 
233  using const_result_iterator::operator->;
236  using const_result_iterator::operator*;
238 
245  using const_result_iterator::operator[];
249 
256  {
257  iterator_type::operator=(r);
258  return *this;
259  }
261  {
262  iterator_type::operator=(std::move(r));
263  return *this;
264  }
266  {
267  iterator_type::operator--();
268  return *this;
269  }
270  const_reverse_result_iterator operator++(int);
272  {
273  iterator_type::operator++();
274  return *this;
275  }
276  const_reverse_result_iterator operator--(int);
278  {
279  iterator_type::operator-=(i);
280  return *this;
281  }
283  {
284  iterator_type::operator+=(i);
285  return *this;
286  }
287 
288  void swap(const_reverse_result_iterator &other) noexcept
289  {
291  }
293 
298  [[nodiscard]] const_reverse_result_iterator
300  {
301  return const_reverse_result_iterator(base() - i);
302  }
304  {
305  return const_reverse_result_iterator(base() + i);
306  }
307  [[nodiscard]] difference_type
309  {
310  return rhs.const_result_iterator::operator-(*this);
311  }
313 
318  [[nodiscard]] bool
319  operator==(const_reverse_result_iterator const &rhs) const noexcept
320  {
321  return iterator_type::operator==(rhs);
322  }
323  [[nodiscard]] bool
324  operator!=(const_reverse_result_iterator const &rhs) const noexcept
325  {
326  return not operator==(rhs);
327  }
328 
329  [[nodiscard]] bool operator<(const_reverse_result_iterator const &rhs) const
330  {
331  return iterator_type::operator>(rhs);
332  }
333  [[nodiscard]] bool operator<=(const_reverse_result_iterator const &rhs) const
334  {
335  return iterator_type::operator>=(rhs);
336  }
337  [[nodiscard]] bool operator>(const_reverse_result_iterator const &rhs) const
338  {
339  return iterator_type::operator<(rhs);
340  }
341  [[nodiscard]] bool operator>=(const_reverse_result_iterator const &rhs) const
342  {
343  return iterator_type::operator<=(rhs);
344  }
346 };
347 
348 
351 {
352  return const_result_iterator{
353  &m_result, size_type(result::difference_type(m_index) + o)};
354 }
355 
358 {
359  return i + o;
360 }
361 
364 {
365  return const_result_iterator{
366  &m_result, result_size_type(result::difference_type(m_index) - o)};
367 }
368 
371 {
372  return result::difference_type(num() - i.num());
373 }
374 
375 inline const_result_iterator result::end() const noexcept
376 {
377  return const_result_iterator{this, size()};
378 }
379 
380 
381 inline const_result_iterator result::cend() const noexcept
382 {
383  return end();
384 }
385 
386 
389 {
390  return const_reverse_result_iterator{i.base() - n};
391 }
392 
393 } // namespace pqxx
394 
395 #include "pqxx/internal/compiler-internal-post.hxx"
396 #endif
bool operator>(const_result_iterator const &i) const
Definition: result_iterator.hxx:164
reference operator*() const
Dereference the iterator.
Definition: result_iterator.hxx:76
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:195
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:357
const_iterator cend() const noexcept
Definition: result_iterator.hxx:381
const_iterator end() const noexcept
Definition: result_iterator.hxx:375
const_reverse_result_iterator(const_result_iterator const &rhs)
Copy a reverse iterator from a regular iterator.
Definition: result_iterator.hxx:213
bool operator>=(const_result_iterator const &i) const
Definition: result_iterator.hxx:168
const_reverse_result_iterator & operator++()
Definition: result_iterator.hxx:265
const_result_iterator operator+(difference_type) const
Definition: result_iterator.hxx:350
PQXX_PURE const_result_iterator base() const noexcept
Return the underlying "regular" iterator (as per standard library).
Definition: result.cxx:482
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
result_difference_type difference_type
Definition: result_iterator.hxx:43
row const * pointer
Definition: result_iterator.hxx:40
bool operator==(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:319
row const value_type
Definition: result_iterator.hxx:39
const_reverse_result_iterator operator-(difference_type i)
Definition: result_iterator.hxx:303
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
const_result_iterator & operator--()
Definition: result_iterator.hxx:118
const_reverse_result_iterator operator+(difference_type i) const
Definition: result_iterator.hxx:299
bool operator>=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:341
const_result_iterator & operator++()
Definition: result_iterator.hxx:112
const_reverse_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:277
reference at(size_type) const
Definition: row.cxx:145
int result_difference_type
Difference between result sizes.
Definition: types.hxx:27
const_reverse_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:282
result_size_type size_type
Definition: result_iterator.hxx:42
const_result_iterator & operator=(const_result_iterator const &rhs)
Definition: result_iterator.hxx:95
void swap(const_result_iterator &other) noexcept
Interchange two iterators in an exception-safe manner.
Definition: result_iterator.hxx:136
const_reverse_result_iterator & operator=(const_reverse_result_iterator &&r)
Definition: result_iterator.hxx:260
reference back() const noexcept
Definition: row.cxx:62
bool operator<=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:333
int result_size_type
Number of rows in a result set.
Definition: types.hxx:24
std::random_access_iterator_tag iterator_category
Definition: result_iterator.hxx:38
row & operator=(row const &)=default
result_difference_type difference_type
Definition: result.hxx:74
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:108
difference_type operator-(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:308
reference front() const noexcept
Definition: row.cxx:54
const_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:124
const_reverse_result_iterator & operator=(const_reverse_result_iterator const &r)
Definition: result_iterator.hxx:255
result::size_type m_index
Row number.
Definition: row.hxx:221
const_result_iterator operator-(difference_type) const
Definition: result_iterator.hxx:363
const_reverse_result_iterator(const_result_iterator const &&rhs)
Move a regular iterator into a reverse iterator.
Definition: result_iterator.hxx:220
bool operator<=(const_result_iterator const &i) const
Definition: result_iterator.hxx:160
void swap(row &) noexcept
Definition: row.cxx:122
void swap(const_reverse_result_iterator &other) noexcept
Definition: result_iterator.hxx:288
bool operator!=(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:324
Reference to one row in a result.
Definition: row.hxx:45
const_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:129
bool operator>(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:337
bool operator!=(const_result_iterator const &i) const
Definition: result_iterator.hxx:152
bool operator<(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:329
const_result_iterator & operator=(const_result_iterator &&rhs)
Definition: result_iterator.hxx:103
bool operator==(const_result_iterator const &i) const
Definition: result_iterator.hxx:148
const_reverse_result_iterator & operator--()
Definition: result_iterator.hxx:271
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:156
result::size_type num() const
Definition: row.hxx:155