MySQL 9.6.0
Source Code Documentation
projection_views.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_PROJECTION_VIEWS_H
25#define MYSQL_RANGES_PROJECTION_VIEWS_H
26
27/// @file
28/// Experimental API header
29
30#include <cstddef> // size_t
31#include <iterator> // input_iterator
32#include <utility> // get(pair)
33#include "mysql/ranges/transform_view.h" // Transform_view
34
35/// @addtogroup GroupLibsMysqlRanges
36/// @{
37
38namespace mysql::ranges::detail {
39
40/// Function-like class that projects a tuple-like object to the Nth element.
41///
42/// @tparam index_tp The index of the element to project on.
43template <std::size_t index_tp>
45 /// Select component number `index_tp` from `tuple`.
46 ///
47 /// The element type must not be an rvalue reference.
48 ///
49 /// The returned type is:
50 ///
51 /// - a reference, if either the tuple is an lvalue reference, or the element
52 /// type is an lvalue reference.
53 ///
54 /// - a non-reference, otherwise, i.e., when the element type is a
55 /// non-reference and the tuple type is an rvalue reference. In this case,
56 /// the returned value is move-constructed from the tuple element.
57 template <class Tuple_t>
58 static decltype(auto) transform(Tuple_t &&tuple) {
59 using Noref_tuple_t = std::remove_reference_t<Tuple_t>;
60 using Element_t = std::tuple_element_t<index_tp, Noref_tuple_t>;
61 static_assert(
62 !std::is_rvalue_reference_v<Element_t>,
63 "Cannot project to tuple component of rvalue reference type.");
64 if constexpr (std::is_rvalue_reference_v<decltype(tuple)> &&
65 !std::is_reference_v<Element_t>) {
66 // The parameter is like `std::tuple<T> &&`, where T is not a reference.
67 // Thus, the tuple will expire, so we should return a copy of the value
68 // to avoid dangling reference after expiration.
69 return Element_t(std::move(std::get<index_tp>(tuple)));
70 } else {
71 return std::get<index_tp>(std::forward<Tuple_t>(tuple));
72 }
73 }
74};
75
76} // namespace mysql::ranges::detail
77namespace mysql::ranges {
78
79/// Projection iterator adaptor: given an iterator over tuple-like objects, this
80/// is an iterator over the Nth component.
81///
82/// @tparam index_tp The index of the element to project on.
83///
84/// @tparam Source_iterator_tp The source iterator, which should yield
85/// tuple-like objects.
86template <std::size_t index_tp, std::input_iterator Source_iterator_tp>
89 Source_iterator_tp>;
90
91/// Factory function to create a Projection_iterator.
92///
93/// @tparam index_t The component index.
94///
95/// @tparam Tuple_iterator_t The source iterator type.
96template <std::size_t index_t, std::input_iterator Tuple_iterator_t>
97[[nodiscard]] auto make_projection_iterator(const Tuple_iterator_t &iterator) {
99}
100
101/// Projection view: given a range over tuple-like objects, this is a range over
102/// the N'th component of those tuples.
103///
104/// @tparam index_tp The component index.
105///
106/// @tparam Source_tp The source type, which should be a range over tuple-like
107/// objects.
108template <std::size_t index_tp, std::ranges::range Source_tp>
111
112/// Factory function to create a Projection_view.
113///
114/// @tparam index_t The component index.
115///
116/// @tparam Source_t The source type.
117template <std::size_t index_t, std::ranges::range Source_t>
118[[nodiscard]] auto make_projection_view(const Source_t &source) {
120}
121
122/// Iterator adaptor that extracts the first component from tuple-like value
123/// types. For example, Key_iterator<std::map::iterator> is an iterator over the
124/// keys in the map.
125///
126/// @tparam Value_iterator_t Source iterator that yields value pairs.
127template <class Value_iterator_t>
129
130/// Factory function to create a new iterator from a given iterator over pairs.
131///
132/// @tparam Value_iterator_t Source iterator that yields value pairs.
133[[nodiscard]] auto make_key_iterator(const auto &iterator) {
134 return make_projection_iterator<0>(iterator);
135}
136
137/// View over the keys of a range of pairs.
138///
139/// @tparam Source_t Source range.
140template <class Source_t>
142
143/// Factory function to create a new view over the keys in a range of pairs.
144///
145/// @tparam Source_t Source range.
146[[nodiscard]] auto make_key_view(const auto &source) {
147 return make_projection_view<0>(source);
148}
149
150/// Iterator adaptor that extracts the second component from tuple-like value
151/// types. For example, Mapped_iterator<std::map::iterator> is an iterator over
152/// the mapped values in the map.
153///
154/// @tparam Value_iterator_t Source iterator that yields value pairs.
155template <class Value_iterator_t>
157
158/// Factory function to create a new iterator from a given iterator over pairs.
159///
160/// @tparam Value_iterator_t Source iterator that yields value pairs.
161[[nodiscard]] auto make_mapped_iterator(const auto &iterator) {
162 return make_projection_iterator<1>(iterator);
163}
164
165/// View over the mapped values in a range of pairs.
166///
167/// @tparam Source_t Source range.
168template <class Source_t>
170
171/// Factory function to create a new view over the mapped values in a range of
172/// pairs.
173///
174/// @tparam Source_t Source range.
175[[nodiscard]] auto make_mapped_view(const auto &source) {
176 return make_projection_view<1>(source);
177}
178
179} // namespace mysql::ranges
180
181// addtogroup GroupLibsMysqlRanges
182/// @}
183
184#endif // ifndef MYSQL_RANGES_PROJECTION_VIEWS_H
Iterator adaptor that applies a transformation on each value before returning it.
Definition: transform_view.h:52
View whose iterators provide transformed values.
Definition: transform_view.h:175
Definition: buffer_interface.h:40
Definition: buffer_interface.h:40
auto make_key_iterator(const auto &iterator)
Factory function to create a new iterator from a given iterator over pairs.
Definition: projection_views.h:133
auto make_mapped_iterator(const auto &iterator)
Factory function to create a new iterator from a given iterator over pairs.
Definition: projection_views.h:161
auto make_projection_view(const Source_t &source)
Factory function to create a Projection_view.
Definition: projection_views.h:118
auto make_projection_iterator(const Tuple_iterator_t &iterator)
Factory function to create a Projection_iterator.
Definition: projection_views.h:97
auto make_key_view(const auto &source)
Factory function to create a new view over the keys in a range of pairs.
Definition: projection_views.h:146
auto make_mapped_view(const auto &source)
Factory function to create a new view over the mapped values in a range of pairs.
Definition: projection_views.h:175
repeated Source source
Definition: replication_asynchronous_connection_failover.proto:42
Function-like class that projects a tuple-like object to the Nth element.
Definition: projection_views.h:44
static decltype(auto) transform(Tuple_t &&tuple)
Select component number index_tp from tuple.
Definition: projection_views.h:58
Experimental API header.