libpqxx  7.0.6
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 = default;
46  const_result_iterator(const_result_iterator const &) noexcept = default;
47  const_result_iterator(const_result_iterator &&) noexcept = default;
48  const_result_iterator(row const &t) noexcept : row{t} {}
49 
65  [[nodiscard]] pointer operator->() const { return this; }
66  [[nodiscard]] reference operator*() const { return row{*this}; }
68 
73  using row::back;
74  using row::front;
75  using row::operator[];
76  using row::at;
77  using row::rownumber;
79 
85  {
86  row::operator=(rhs);
87  return *this;
88  }
89 
91  {
92  row::operator=(std::move(rhs));
93  return *this;
94  }
95 
96  const_result_iterator operator++(int);
98  {
99  ++m_index;
100  return *this;
101  }
102  const_result_iterator operator--(int);
104  {
105  --m_index;
106  return *this;
107  }
108 
110  {
111  m_index += i;
112  return *this;
113  }
115  {
116  m_index -= i;
117  return *this;
118  }
119 
120  void swap(const_result_iterator &other) noexcept { row::swap(other); }
122 
127  [[nodiscard]] bool operator==(const_result_iterator const &i) const
128  {
129  return m_index == i.m_index;
130  }
131  [[nodiscard]] bool operator!=(const_result_iterator const &i) const
132  {
133  return m_index != i.m_index;
134  }
135  [[nodiscard]] bool operator<(const_result_iterator const &i) const
136  {
137  return m_index < i.m_index;
138  }
139  [[nodiscard]] bool operator<=(const_result_iterator const &i) const
140  {
141  return m_index <= i.m_index;
142  }
143  [[nodiscard]] bool operator>(const_result_iterator const &i) const
144  {
145  return m_index > i.m_index;
146  }
147  [[nodiscard]] bool operator>=(const_result_iterator const &i) const
148  {
149  return m_index >= i.m_index;
150  }
152 
157  [[nodiscard]] inline const_result_iterator operator+(difference_type) const;
158  friend const_result_iterator
160  [[nodiscard]] inline const_result_iterator operator-(difference_type) const;
161  [[nodiscard]] inline difference_type
162  operator-(const_result_iterator const &) const;
164 
165 private:
166  friend class pqxx::result;
167  const_result_iterator(pqxx::result const *r, result_size_type i) noexcept :
168  row{*r, i}
169  {}
170 };
171 
172 
174 class PQXX_LIBEXPORT const_reverse_result_iterator
175  : private const_result_iterator
176 {
177 public:
185 
186  const_reverse_result_iterator() = default;
188  default;
191  {
192  super::operator--();
193  }
194 
196  const_result_iterator{std::move(rhs)}
197  {
198  super::operator--();
199  }
200 
201  [[nodiscard]] PQXX_PURE const_result_iterator base() const noexcept;
202 
207  using const_result_iterator::operator->;
208  using const_result_iterator::operator*;
210 
217  using const_result_iterator::operator[];
221 
228  {
229  iterator_type::operator=(r);
230  return *this;
231  }
233  {
234  iterator_type::operator=(std::move(r));
235  return *this;
236  }
238  {
239  iterator_type::operator--();
240  return *this;
241  }
242  const_reverse_result_iterator operator++(int);
244  {
245  iterator_type::operator++();
246  return *this;
247  }
248  const_reverse_result_iterator operator--(int);
250  {
251  iterator_type::operator-=(i);
252  return *this;
253  }
255  {
256  iterator_type::operator+=(i);
257  return *this;
258  }
259 
260  void swap(const_reverse_result_iterator &other) noexcept
261  {
263  }
265 
270  [[nodiscard]] const_reverse_result_iterator
272  {
273  return const_reverse_result_iterator(base() - i);
274  }
276  {
277  return const_reverse_result_iterator(base() + i);
278  }
279  [[nodiscard]] difference_type
281  {
282  return rhs.const_result_iterator::operator-(*this);
283  }
285 
290  [[nodiscard]] bool operator==(const_reverse_result_iterator const &rhs) const
291  noexcept
292  {
293  return iterator_type::operator==(rhs);
294  }
295  [[nodiscard]] bool operator!=(const_reverse_result_iterator const &rhs) const
296  noexcept
297  {
298  return not operator==(rhs);
299  }
300 
301  [[nodiscard]] bool operator<(const_reverse_result_iterator const &rhs) const
302  {
303  return iterator_type::operator>(rhs);
304  }
305  [[nodiscard]] bool operator<=(const_reverse_result_iterator const &rhs) const
306  {
307  return iterator_type::operator>=(rhs);
308  }
309  [[nodiscard]] bool operator>(const_reverse_result_iterator const &rhs) const
310  {
311  return iterator_type::operator<(rhs);
312  }
313  [[nodiscard]] bool operator>=(const_reverse_result_iterator const &rhs) const
314  {
315  return iterator_type::operator<=(rhs);
316  }
318 };
319 
320 
323 {
324  return const_result_iterator{
325  &m_result, size_type(result::difference_type(m_index) + o)};
326 }
327 
330 {
331  return i + o;
332 }
333 
336 {
337  return const_result_iterator{
338  &m_result, result_size_type(result::difference_type(m_index) - o)};
339 }
340 
343 {
344  return result::difference_type(num() - i.num());
345 }
346 
347 inline const_result_iterator result::end() const noexcept
348 {
349  return const_result_iterator{this, size()};
350 }
351 
352 
353 inline const_result_iterator result::cend() const noexcept
354 {
355  return end();
356 }
357 
358 
361 {
362  return const_reverse_result_iterator{i.base() - n};
363 }
364 
365 } // namespace pqxx
366 
367 #include "pqxx/internal/compiler-internal-post.hxx"
368 #endif
bool operator==(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:290
bool operator>(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:309
const_reverse_result_iterator operator-(difference_type i)
Definition: result_iterator.hxx:275
bool operator!=(const_result_iterator const &i) const
Definition: result_iterator.hxx:131
bool operator<(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:301
bool operator==(const_result_iterator const &i) const
Definition: result_iterator.hxx:127
bool operator>=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:313
const_result_iterator & operator++()
Definition: result_iterator.hxx:97
row & operator=(row const &)=default
int result_difference_type
Difference between result sizes.
Definition: types.hxx:21
void swap(const_result_iterator &other) noexcept
Definition: result_iterator.hxx:120
const_reverse_result_iterator & operator=(const_reverse_result_iterator &&r)
Definition: result_iterator.hxx:232
bool operator<=(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:305
bool operator>=(const_result_iterator const &i) const
Definition: result_iterator.hxx:147
const_reverse_result_iterator & operator++()
Definition: result_iterator.hxx:237
const_result_iterator operator+(difference_type) const
Definition: result_iterator.hxx:322
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
reference back() const noexcept
Definition: row.cxx:62
row const value_type
Definition: result_iterator.hxx:39
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:329
reference at(size_type) const
Definition: row.cxx:139
const_result_iterator & operator--()
Definition: result_iterator.hxx:103
const_reverse_result_iterator operator+(difference_type i) const
Definition: result_iterator.hxx:271
reference front() const noexcept
Definition: row.cxx:56
void swap(row &) noexcept
Definition: row.cxx:118
const_reverse_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:249
bool operator!=(const_reverse_result_iterator const &rhs) const noexcept
Definition: result_iterator.hxx:295
result::size_type rownumber() const noexcept
Row number, assuming this is a real row and not end()/rend().
Definition: row.hxx:114
const_reverse_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:254
result_size_type size_type
Definition: result_iterator.hxx:42
const_result_iterator & operator=(const_result_iterator const &rhs)
Definition: result_iterator.hxx:84
const_result_iterator & operator-=(difference_type i)
Definition: result_iterator.hxx:114
const_iterator end() const noexcept
Definition: result_iterator.hxx:347
const_result_iterator & operator=(const_result_iterator &&rhs)
Definition: result_iterator.hxx:90
result::size_type num() const
Definition: row.hxx:167
const_reverse_result_iterator & operator--()
Definition: result_iterator.hxx:243
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:135
difference_type operator-(const_reverse_result_iterator const &rhs) const
Definition: result_iterator.hxx:280
const_result_iterator & operator+=(difference_type i)
Definition: result_iterator.hxx:109
bool operator>(const_result_iterator const &i) const
Definition: result_iterator.hxx:143
const_reverse_result_iterator & operator=(const_reverse_result_iterator const &r)
Definition: result_iterator.hxx:227
Reference to one row in a result.
Definition: row.hxx:38
const_result_iterator operator-(difference_type) const
Definition: result_iterator.hxx:335
reference operator*() const
Definition: result_iterator.hxx:66
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:174
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
const_reverse_result_iterator(const_result_iterator const &&rhs)
Definition: result_iterator.hxx:195
const_iterator cend() const noexcept
Definition: result_iterator.hxx:353
const_reverse_result_iterator(const_result_iterator const &rhs)
Definition: result_iterator.hxx:189
bool operator<=(const_result_iterator const &i) const
Definition: result_iterator.hxx:139
result::size_type m_index
Row number.
Definition: row.hxx:199
void swap(const_reverse_result_iterator &other) noexcept
Definition: result_iterator.hxx:260
pointer operator->() const
Definition: result_iterator.hxx:65
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35