MySQL 9.6.0
Source Code Documentation
iterator_with_range.h
Go to the documentation of this file.
1// Copyright (c) 2025, Oracle and/or its affiliates.
2//
3// This program is free software; you can redistribute it and/or modify
4// it under the terms of the GNU General Public License, version 2.0,
5// as published by the Free Software Foundation.
6//
7// This program is designed to work with certain software (including
8// but not limited to OpenSSL) that is licensed under separate terms,
9// as designated in a particular file or component or in included license
10// documentation. The authors of MySQL hereby grant you an additional
11// permission to link the program and your derivative works with the
12// separately licensed software that they have either included with
13// the program or referenced in the documentation.
14//
15// This program is distributed in the hope that it will be useful,
16// but WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License, version 2.0, for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this program; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
24#ifndef MYSQL_RANGES_ITERATOR_WITH_RANGE_H
25#define MYSQL_RANGES_ITERATOR_WITH_RANGE_H
26
27/// @file
28/// Experimental API header
29
30#include <cstddef> // ptrdiff_t
31#include <iterator> // bidirectional_iterator
32#include <ranges> // range
33#include "mysql/iterators/iterator_interface.h" // Iterator_interface
34#include "mysql/ranges/meta.h" // Range_const_iterator_type
35#include "mysql/ranges/view_sources.h" // View_source
36
37/// @addtogroup GroupLibsMysqlRanges
38/// @{
39
40namespace mysql::ranges {
41
42/// Iterator that holds a reference to its source range.
43///
44/// Since the iterator knows its range, it always knows whether it is positioned
45/// at the end, without having to compare with other objects.
46template <std::ranges::range Range_tp>
48 Iterator_with_range<Range_tp>> {
49 public:
50 using Range_t = Range_tp;
52
53 /// Construct a singular object.
55
56 /// Construct a new object from the given range, and set the iterator to
57 /// the beginning.
60 /// Construct a new object from the given range, and set the iterator to
61 /// the beginning.
64
65 [[nodiscard]] decltype(auto) get() const { return *m_iterator; }
66
67 [[nodiscard]] decltype(auto) get_pointer() const
68 requires std::contiguous_iterator<Range_tp>
69 {
70 return std::to_address(m_iterator);
71 }
72
73 void next() { ++m_iterator; }
74
75 void prev()
76 requires std::bidirectional_iterator<Iterator_t>
77 {
78 --m_iterator;
79 }
80
81 void advance(std::ptrdiff_t delta)
82 requires std::random_access_iterator<Iterator_t>
83 {
84 m_iterator += delta;
85 }
86
87 [[nodiscard]] bool is_equal(const Iterator_with_range &other) const {
88 return m_iterator == other.m_iterator;
89 }
90
91 [[nodiscard]] std::ptrdiff_t distance_from(
92 const Iterator_with_range &other) const
93 requires std::random_access_iterator<Iterator_t>
94 {
95 return m_iterator - other.m_iterator;
96 }
97
98 [[nodiscard]] auto &range() { return m_range; }
99 [[nodiscard]] const auto &range() const { return m_range; }
100 [[nodiscard]] auto &iterator() { return m_iterator; }
101 [[nodiscard]] const auto &iterator() const { return m_iterator; }
102
103 [[nodiscard]] bool is_end() const { return m_iterator == m_range.end(); }
104
105 private:
106 /// Range object.
108
109 /// Iterator into the range.
111}; // class Iterator_with_range
112
113} // namespace mysql::ranges
114
115// addtogroup GroupLibsMysqlRanges
116/// @}
117
118#endif // ifndef MYSQL_RANGES_ITERATOR_WITH_RANGE_H
CRTP base class (mixin) that makes your class a standard-compliant iterator, given only a minimal set...
Definition: iterator_interface.h:370
Iterator that holds a reference to its source range.
Definition: iterator_with_range.h:48
auto & iterator()
Definition: iterator_with_range.h:100
Iterator_with_range(const Range_t &range)
Construct a new object from the given range, and set the iterator to the beginning.
Definition: iterator_with_range.h:62
void advance(std::ptrdiff_t delta)
Definition: iterator_with_range.h:81
void next()
Definition: iterator_with_range.h:73
View_source< Range_t > m_range
Range object.
Definition: iterator_with_range.h:107
std::ptrdiff_t distance_from(const Iterator_with_range &other) const
Definition: iterator_with_range.h:91
Range_tp Range_t
Definition: iterator_with_range.h:50
decltype(auto) get() const
Definition: iterator_with_range.h:65
const auto & range() const
Definition: iterator_with_range.h:99
bool is_end() const
Definition: iterator_with_range.h:103
auto & range()
Definition: iterator_with_range.h:98
Iterator_t m_iterator
Iterator into the range.
Definition: iterator_with_range.h:110
void prev()
Definition: iterator_with_range.h:75
const auto & iterator() const
Definition: iterator_with_range.h:101
Iterator_with_range(const Range_t &range, const Iterator_t &iterator)
Construct a new object from the given range, and set the iterator to the beginning.
Definition: iterator_with_range.h:58
Iterator_with_range()=default
Construct a singular object.
bool is_equal(const Iterator_with_range &other) const
Definition: iterator_with_range.h:87
Range_const_iterator_type< Range_t > Iterator_t
Definition: iterator_with_range.h:51
decltype(auto) get_pointer() const
Definition: iterator_with_range.h:67
auto end() const
Return end iterator to the source.
Definition: view_sources.h:158
Experimental API header.
Experimental API header.
Definition: buffer_interface.h:40
std::remove_cvref_t< decltype(std::declval< const Range_t >().begin())> Range_const_iterator_type
Gives the const_iterator type, deduced from the begin() const member.
Definition: meta.h:47
const char * begin(const char *const c)
Definition: base64.h:44
Define std::hash<Gtid>.
Definition: gtid.h:355
Definition: gen_lex_token.cc:149
Experimental API header.