libpqxx  7.3.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-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 
46  const_result_iterator() noexcept = default;
48  const_result_iterator(const_result_iterator const &) noexcept = default;
50  const_result_iterator(const_result_iterator &&) noexcept = default;
52  const_result_iterator(row const &t) noexcept : row{t} {}
53 
68  [[nodiscard]] pointer operator->() const { return this; }
71  [[nodiscard]] reference operator*() const { return row{*this}; }
73 
78  using row::back;
79  using row::front;
80  using row::operator[];
81  using row::at;
82  using row::rownumber;
84 
90  {
91  row::operator=(rhs);
92  return *this;
93  }
94 
96  {
97  row::operator=(std::move(rhs));
98  return *this;
99  }
100 
101  const_result_iterator operator++(int);
103  {
104  ++m_index;
105  return *this;
106  }
107  const_result_iterator operator--(int);
109  {
110  --m_index;
111  return *this;
112  }
113 
115  {
116  m_index += i;
117  return *this;
118  }
120  {
121  m_index -= i;
122  return *this;
123  }
124 
126  void swap(const_result_iterator &other) noexcept { row::swap(other); }
128 
133  [[nodiscard]] bool operator==(const_result_iterator const &i) const
134  {
135  return m_index == i.m_index;
136  }
137  [[nodiscard]] bool operator!=(const_result_iterator const &i) const
138  {
139  return m_index != i.m_index;
140  }
141  [[nodiscard]] bool operator<(const_result_iterator const &i) const
142  {
143  return m_index < i.m_index;
144  }
145  [[nodiscard]] bool operator<=(const_result_iterator const &i) const
146  {
147  return m_index <= i.m_index;
148  }
149  [[nodiscard]] bool operator>(const_result_iterator const &i) const
150  {
151  return m_index > i.m_index;
152  }
153  [[nodiscard]] bool operator>=(const_result_iterator const &i) const
154  {
155  return m_index >= i.m_index;
156  }
158 
163  [[nodiscard]] inline const_result_iterator operator+(difference_type) const;
164  friend const_result_iterator
166  [[nodiscard]] inline const_result_iterator operator-(difference_type) const;
167  [[nodiscard]] inline difference_type
168  operator-(const_result_iterator const &) const;
170 
171 private:
172  friend class pqxx::result;
173  const_result_iterator(pqxx::result const *r, result_size_type i) noexcept :
174  row{*r, i}
175  {}
176 };
177 
178 
180 class PQXX_LIBEXPORT const_reverse_result_iterator
181  : private const_result_iterator
182 {
183 public:
191 
193  const_reverse_result_iterator() = default;
196  default;
200  {
201  super::operator--();
202  }
203 
206  const_result_iterator{std::move(rhs)}
207  {
208  super::operator--();
209  }
210 
212  [[nodiscard]] PQXX_PURE const_result_iterator base() const noexcept;
213 
218  using const_result_iterator::operator->;
221  using const_result_iterator::operator*;
223 
230  using const_result_iterator::operator[];
234 
241  {
242  iterator_type::operator=(r);
243  return *this;
244  }
246  {
247  iterator_type::operator=(std::move(r));
248  return *this;
249  }
251  {
252  iterator_type::operator--();
253  return *this;
254  }
255  const_reverse_result_iterator operator++(int);
257  {
258  iterator_type::operator++();
259  return *this;
260  }
261  const_reverse_result_iterator operator--(int);
263  {
264  iterator_type::operator-=(i);
265  return *this;
266  }
268  {
269  iterator_type::operator+=(i);
270  return *this;
271  }
272 
273  void swap(const_reverse_result_iterator &other) noexcept
274  {
276  }
278 
283  [[nodiscard]] const_reverse_result_iterator
285  {
286  return const_reverse_result_iterator(base() - i);
287  }
289  {
290  return const_reverse_result_iterator(base() + i);
291  }
292  [[nodiscard]] difference_type
294  {
295  return rhs.const_result_iterator::operator-(*this);
296  }
298 
303  [[nodiscard]] bool
304  operator==(const_reverse_result_iterator const &rhs) const noexcept
305  {
306  return iterator_type::operator==(rhs);
307  }
308  [[nodiscard]] bool
309  operator!=(const_reverse_result_iterator const &rhs) const noexcept
310  {
311  return not operator==(rhs);
312  }
313 
314  [[nodiscard]] bool operator<(const_reverse_result_iterator const &rhs) const
315  {
316  return iterator_type::operator>(rhs);
317  }
318  [[nodiscard]] bool operator<=(const_reverse_result_iterator const &rhs) const
319  {
320  return iterator_type::operator>=(rhs);
321  }
322  [[nodiscard]] bool operator>(const_reverse_result_iterator const &rhs) const
323  {
324  return iterator_type::operator<(rhs);
325  }
326  [[nodiscard]] bool operator>=(const_reverse_result_iterator const &rhs) const
327  {
328  return iterator_type::operator<=(rhs);
329  }
331 };
332 
333 
336 {
337  return const_result_iterator{
338  &m_result, size_type(result::difference_type(m_index) + o)};
339 }
340 
343 {
344  return i + o;
345 }
346 
349 {
350  return const_result_iterator{
351  &m_result, result_size_type(result::difference_type(m_index) - o)};
352 }
353 
356 {
357  return result::difference_type(num() - i.num());
358 }
359 
360 inline const_result_iterator result::end() const noexcept
361 {
362  return const_result_iterator{this, size()};
363 }
364 
365 
366 inline const_result_iterator result::cend() const noexcept
367 {
368  return end();
369 }
370 
371 
374 {
375  return const_reverse_result_iterator{i.base() - n};
376 }
377 
378 } // namespace pqxx
379 
380 #include "pqxx/internal/compiler-internal-post.hxx"
381 #endif
bool operator>=(const_result_iterator const &i) const
Definition: result_iterator.hxx:153
int result_difference_type
Difference between result sizes.
Definition: types.hxx:21
reference front() const noexcept
Definition: row.cxx:54
const_result_iterator & operator++()
Definition: result_iterator.hxx:102
bool operator<(const_result_iterator const &i) const
Definition: result_iterator.hxx:141
const_result_iterator & operator=(const_result_iterator const &rhs)
Definition: result_iterator.hxx:89
const_result_iterator operator-(difference_type) const
Definition: result_iterator.hxx:348
void swap(const_reverse_result_iterator &other) noexcept
Definition: result_iterator.hxx:273
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
const_reverse_result_iterator operator-(difference_type i)
Definition: result_iterator.hxx:288
result_size_type size_type
Definition: result_iterator.hxx:42
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
const_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:114
const_result_iterator & operator=(const_result_iterator &&rhs)
Definition: result_iterator.hxx:95
const_iterator cend() const noexcept
Definition: result_iterator.hxx:366
bool operator==(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:304
bool operator>(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:322
void swap(row &) noexcept
Definition: row.cxx:116
result::size_type num() const
Definition: row.hxx:157
const_iterator end() const noexcept
Definition: result_iterator.hxx:360
void swap(const_result_iterator &other) noexcept
Interchange two iterators in an exception-safe manner.
Definition: result_iterator.hxx:126
bool operator!=(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:309
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
const_result_iterator operator+(difference_type) const
Definition: result_iterator.hxx:335
const_reverse_result_iterator & operator++()
Definition: result_iterator.hxx:250
bool operator!=(const_result_iterator const &i) const
Definition: result_iterator.hxx:137
bool operator<=(const_result_iterator const &i) const
Definition: result_iterator.hxx:145
const_reverse_result_iterator & operator=(const_reverse_result_iterator &&r)
Definition: result_iterator.hxx:245
PQXX_PURE const_result_iterator base() const noexcept
Return the underlying "regular" iterator (as per standard library).
Definition: result.cxx:478
bool operator>(const_result_iterator const &i) const
Definition: result_iterator.hxx:149
const_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:119
bool operator<=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:318
Reference to one row in a result.
Definition: row.hxx:45
const_reverse_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:267
Result set containing data returned by a query or command.
Definition: result.hxx:70
const_reverse_result_iterator & operator--()
Definition: result_iterator.hxx:256
std::random_access_iterator_tag iterator_category
Definition: result_iterator.hxx:38
reference operator*() const
Dereference the iterator.
Definition: result_iterator.hxx:71
row & operator=(row const &)=default
result_difference_type difference_type
Definition: result.hxx:74
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:180
const_reverse_result_iterator operator+(difference_type i) const
Definition: result_iterator.hxx:284
const_result_iterator operator+(result::difference_type o, const_result_iterator const &i)
Definition: result_iterator.hxx:342
const_reverse_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:262
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:110
result::size_type m_index
Row number.
Definition: row.hxx:222
row const value_type
Definition: result_iterator.hxx:39
const_reverse_result_iterator(const_result_iterator const &&rhs)
Move a regular iterator into a reverse iterator.
Definition: result_iterator.hxx:205
reference at(size_type) const
Definition: row.cxx:137
const_reverse_result_iterator(const_result_iterator const &rhs)
Copy a reverse iterator from a regular iterator.
Definition: result_iterator.hxx:198
const_result_iterator & operator--()
Definition: result_iterator.hxx:108
row const * pointer
Definition: result_iterator.hxx:40
bool operator<(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:314
result_difference_type difference_type
Definition: result_iterator.hxx:43
bool operator>=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:326
reference back() const noexcept
Definition: row.cxx:60
difference_type operator-(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:293
bool operator==(const_result_iterator const &i) const
Definition: result_iterator.hxx:133
const_reverse_result_iterator & operator=(const_reverse_result_iterator const &r)
Definition: result_iterator.hxx:240