MySQL 9.6.0
Source Code Documentation
nested_set_interface.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_SETS_NESTED_SET_INTERFACE_H
25#define MYSQL_SETS_NESTED_SET_INTERFACE_H
26
27/// @file
28/// Experimental API header
29
30#include <iterator> // forward_iterator
31#include <utility> // pair
32#include "mysql/containers/basic_container_wrapper.h" // Basic_container_wrapper
33#include "mysql/meta/not_decayed.h" // Not_decayed
34#include "mysql/ranges/collection_interface.h" // Collection_interface
35#include "mysql/ranges/meta.h" // Range_iterator_type
36#include "mysql/sets/basic_set_container_wrapper.h" // Basic_set_container_wrapper
37#include "mysql/sets/nested_set_category.h" // Nested_set_category_tag
38#include "mysql/sets/nested_set_meta.h" // Is_nested_set_traits
39
40/// @addtogroup GroupLibsMysqlSets
41/// @{
42
43namespace mysql::sets::detail {
44
45/// CRTP base class/mixin used to define a Nested set based on an implementation
46/// that provides the `find` and `upper_bound` functions.
47///
48/// This provides the member types and `operator[]`.
49///
50/// @tparam Self_tp Implementation subclass.
51///
52/// @tparam Iterator_tp Iterator type for the subclass.
53///
54/// @tparam Const_iterator_tp Const iterator type for the subclass.
55///
56/// @tparam Set_traits_tp Set traits.
57template <class Self_tp, std::forward_iterator Iterator_tp,
58 std::forward_iterator Const_iterator_tp,
59 Is_nested_set_traits Set_traits_tp>
61 using Self_t = Self_tp;
62
63 public:
64 using Iterator_t = Iterator_tp;
65 using Const_iterator_t = Const_iterator_tp;
68
69 using Set_traits_t = Set_traits_tp;
70 using Key_traits_t = typename Set_traits_t::Key_traits_t;
71 using Key_t = typename Key_traits_t::Element_t;
72 using Mapped_category_t = typename Set_traits_t::Mapped_category_t;
73 using Mapped_traits_t = typename Set_traits_t::Mapped_traits_t;
74 using Mapped_t = typename Iterator_value_t::second_type;
75
76 static_assert(
77 std::same_as<Iterator_value_t, std::pair<const Key_t, Mapped_t>>);
78
79 /// Return non-const reference to the mapped Set for the given key.
80 ///
81 /// If the key is not in the set, the behavior is undefined.
82 ///
83 /// @param key Key to look for.
84 [[nodiscard]] auto &operator[](const Key_t &key) noexcept {
85 auto it = self().find(key);
86 assert(it != self().end());
87 return it->second;
88 }
89
90 /// Return const reference to the mapped Set for the given key.
91 ///
92 /// If the key is not in the set, the behavior is undefined.
93 ///
94 /// @param key Key to look for.
95 [[nodiscard]] const auto &operator[](const Key_t &key) const noexcept {
96 auto it = self().find(key);
97 assert(it != self().end());
98 return it->second;
99 }
100
101 private:
102 [[nodiscard]] auto &self() { return static_cast<Self_t &>(*this); }
103 [[nodiscard]] const auto &self() const {
104 return static_cast<const Self_t &>(*this);
105 }
106}; // class Nested_set_interface
107
108} // namespace mysql::sets::detail
109
110namespace mysql::sets {
111
112/// CRTP base class/mixin used to implement Nested sets that are *views*. This
113/// defines the `operator[]` members based on `find` members in the subclass,
114/// and also implements all the view members defined by
115/// `mysql::ranges::Collection_interface`.
116///
117/// @tparam Self_tp Class deriving from this class.
118///
119/// @tparam Iterator_tp Iterator type.
120///
121/// @tparam Const_iterator_tp Const iterator type.
122///
123/// @tparam Set_traits_tp Nested set traits.
124template <class Self_tp, std::input_iterator Iterator_tp,
125 std::input_iterator Const_iterator_tp,
126 Is_nested_set_traits Set_traits_tp>
128 : public mysql::ranges::Collection_interface<Self_tp>,
129 public detail::Nested_set_interface<Self_tp, Iterator_tp,
130 Const_iterator_tp, Set_traits_tp>,
131 public std::ranges::view_base {
133 detail::Nested_set_interface<Self_tp, Iterator_tp, Const_iterator_tp,
134 Set_traits_tp>;
135
136 public:
137 // Prefer the subscript operator for nested sets over the one from
138 // Collection_interface.
139 using Nested_set_base_t::operator[];
140};
141
142/// CRTP base class/mixin, used to implement Nested Sets that are *container
143/// wrappers*. This defines the `operator[]` members based on `find` in the
144/// subclass, and also implements all the container members defined by
145/// `mysql::containers::Basic_container_wrapper`.
146///
147/// @tparam Self_tp Class deriving from this class.
148///
149/// @tparam Wrapped_tp Type of the wrapped boundary set class.
150template <class Self_tp, class Wrapped_tp>
152 : public Basic_set_container_wrapper<Self_tp, Wrapped_tp>,
154 Self_tp, mysql::ranges::Range_iterator_type<Wrapped_tp>,
155 mysql::ranges::Range_const_iterator_type<Wrapped_tp>,
156 typename Wrapped_tp::Set_traits_t> {
161 typename Wrapped_tp::Set_traits_t>;
163
164 public:
165 // Prefer the subscript operator for nested sets over the one from
166 // Collection_interface.
167 using Nested_set_base_t::operator[];
168
169 template <class... Args_t>
170 requires mysql::meta::Not_decayed<This_t, Args_t...>
171 explicit Basic_nested_container_wrapper(Args_t &&...args) noexcept(
172 noexcept(Wrapper_base_t(std::forward<Args_t>(args)...)))
173 : Wrapper_base_t(std::forward<Args_t>(args)...) {}
174};
175
176} // namespace mysql::sets
177
178// addtogroup GroupLibsMysqlSets
179/// @}
180
181#endif // ifndef MYSQL_SETS_NESTED_SET_INTERFACE_H
Experimental API header.
Experimental API header.
CRTP base class to provide members of a collection based on an implementation that provides begin/end...
Definition: collection_interface.h:90
CRTP base class/mixin, used to implement Nested Sets that are container wrappers.
Definition: nested_set_interface.h:156
Basic_nested_container_wrapper(Args_t &&...args) noexcept(noexcept(Wrapper_base_t(std::forward< Args_t >(args)...)))
Definition: nested_set_interface.h:171
Basic_set_container_wrapper< Self_tp, Wrapped_tp > Wrapper_base_t
Definition: nested_set_interface.h:157
Basic_nested_container_wrapper< Self_tp, Wrapped_tp > This_t
Definition: nested_set_interface.h:162
Definition: basic_set_container_wrapper.h:47
CRTP base class/mixin used to implement Nested sets that are views.
Definition: nested_set_interface.h:131
CRTP base class/mixin used to define a Nested set based on an implementation that provides the find a...
Definition: nested_set_interface.h:60
Set_traits_tp Set_traits_t
Definition: nested_set_interface.h:69
typename Key_traits_t::Element_t Key_t
Definition: nested_set_interface.h:71
typename Set_traits_t::Mapped_category_t Mapped_category_t
Definition: nested_set_interface.h:72
typename Set_traits_t::Key_traits_t Key_traits_t
Definition: nested_set_interface.h:70
const auto & operator[](const Key_t &key) const noexcept
Return const reference to the mapped Set for the given key.
Definition: nested_set_interface.h:95
Iterator_tp Iterator_t
Definition: nested_set_interface.h:64
typename Set_traits_t::Mapped_traits_t Mapped_traits_t
Definition: nested_set_interface.h:73
typename Iterator_value_t::second_type Mapped_t
Definition: nested_set_interface.h:74
mysql::ranges::Iterator_value_type< Iterator_t > Iterator_value_t
Definition: nested_set_interface.h:67
Self_tp Self_t
Definition: nested_set_interface.h:61
auto & operator[](const Key_t &key) noexcept
Return non-const reference to the mapped Set for the given key.
Definition: nested_set_interface.h:84
Const_iterator_tp Const_iterator_t
Definition: nested_set_interface.h:65
Experimental API header.
false if Args is exactly one type, say A, and std::decay_t<A> equals Type.
Definition: not_decayed.h:84
Experimental API header.
Container::const_iterator find(const Container &c, Value &&value)
Definition: generic.h:39
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
std::remove_cvref_t< decltype(*std::declval< Iterator_t >())> Iterator_value_type
Gives the value type for any iterator type, deduced from operator *.
Definition: meta.h:63
std::remove_cvref_t< decltype(std::declval< Range_t >().begin())> Range_iterator_type
Gives the iterator type, deduced from the begin() member.
Definition: meta.h:42
Definition: aliases.h:97
Definition: gtid_set.h:183
Experimental API header.
Experimental API header.
Experimental API header.
required string key
Definition: replication_asynchronous_connection_failover.proto:60
Tag to identify a class as a Nested set.
Definition: nested_set_category.h:41