StormByte 3.0.0
C++26 foundation of the StormByte suite
Loading...
Searching...
No Matches
pair.txx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This is the base for StormByte Suite libraries.
5 *
6 * StormByte original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte source in this repository.
20 * They do not cover other StormByte modules or any third-party material
21 * shipped with this repository (including everything under thirdparty/),
22 * which remains under its own license.
23 *
24 * Neither license grants any patent rights. Any patent licenses required
25 * to use this software or third-party components must be obtained separately
26 * from the patent holders.
27 *
28 * StormByte is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU Lesser General Public License for more details.
32 *
33 * You should have received a copy of the GNU Lesser General Public License
34 * version 3 along with StormByte. If not, see
35 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
36 *
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
38 */
39
40#pragma once
41
42#include <utility>
43
44namespace StormByte {
45 namespace Safe {
46 template<Type::SafeValue First, Type::SafeValue Second>
47 Pair<First, Second>::Pair(const First& firstValue, const Second& secondValue):
48 first(firstValue), second(secondValue) {}
49
50 template<Type::SafeValue First, Type::SafeValue Second>
51 Pair<First, Second>::Pair(First&& firstValue, Second&& secondValue) noexcept(
52 std::is_nothrow_move_constructible_v<First> &&
53 std::is_nothrow_move_constructible_v<Second>):
54 first(std::move(firstValue)), second(std::move(secondValue)) {}
55
56 template<Type::SafeValue First, Type::SafeValue Second>
57 template<class U, class V>
58 requires Type::ConstructibleFrom<First, U> && Type::ConstructibleFrom<Second, V>
59 Pair<First, Second>::Pair(U&& firstValue, V&& secondValue):
60 first(std::forward<U>(firstValue)), second(std::forward<V>(secondValue)) {}
61
62 template<Type::SafeValue First, Type::SafeValue Second>
63 template<class... FirstArgs, class... SecondArgs>
64 Pair<First, Second>::Pair(std::piecewise_construct_t tag, std::tuple<FirstArgs...> firstArgs, std::tuple<SecondArgs...> secondArgs):
65 Pair(tag, firstArgs, secondArgs, std::index_sequence_for<FirstArgs...>{}, std::index_sequence_for<SecondArgs...>{}) {}
66
67 template<Type::SafeValue First, Type::SafeValue Second>
68 template<class OtherFirst, class OtherSecond>
69 requires Type::ConstructibleFrom<First, const OtherFirst&> &&
70 Type::ConstructibleFrom<Second, const OtherSecond&>
71 Pair<First, Second>::Pair(const std::pair<OtherFirst, OtherSecond>& other):
72 first(other.first), second(other.second) {}
73
74 template<Type::SafeValue First, Type::SafeValue Second>
75 template<class OtherFirst, class OtherSecond>
76 requires Type::ConstructibleFrom<First, OtherFirst> &&
77 Type::ConstructibleFrom<Second, OtherSecond>
78 Pair<First, Second>::Pair(std::pair<OtherFirst, OtherSecond>&& other):
79 first(std::move(other.first)), second(std::move(other.second)) {}
80
81 template<Type::SafeValue First, Type::SafeValue Second>
82 template<class OtherFirst, class OtherSecond>
83 requires Type::ConstructibleFrom<First, const OtherFirst&> &&
84 Type::ConstructibleFrom<Second, const OtherSecond&>
85 Pair<First, Second>::Pair(const Pair<OtherFirst, OtherSecond>& other):
86 first(other.first), second(other.second) {}
87
88 template<Type::SafeValue First, Type::SafeValue Second>
89 template<class OtherFirst, class OtherSecond>
90 requires Type::ConstructibleFrom<First, OtherFirst> &&
91 Type::ConstructibleFrom<Second, OtherSecond>
92 Pair<First, Second>::Pair(Pair<OtherFirst, OtherSecond>&& other):
93 first(std::move(other.first)), second(std::move(other.second)) {}
94
95 template<Type::SafeValue First, Type::SafeValue Second>
96 template<class OtherFirst, class OtherSecond>
97 requires Type::AssignableFrom<First&, const OtherFirst&> &&
98 Type::AssignableFrom<Second&, const OtherSecond&>
99 Pair<First, Second>& Pair<First, Second>::operator=(const Pair<OtherFirst, OtherSecond>& other) {
100 first = other.first;
101 second = other.second;
102 return *this;
103 }
104
105 template<Type::SafeValue First, Type::SafeValue Second>
106 template<class OtherFirst, class OtherSecond>
107 requires Type::AssignableFrom<First&, OtherFirst> &&
108 Type::AssignableFrom<Second&, OtherSecond>
109 Pair<First, Second>& Pair<First, Second>::operator=(Pair<OtherFirst, OtherSecond>&& other) {
110 first = std::move(other.first);
111 second = std::move(other.second);
112 return *this;
113 }
114
115 template<Type::SafeValue First, Type::SafeValue Second>
116 template<class OtherFirst, class OtherSecond>
117 requires Type::AssignableFrom<First&, const OtherFirst&> &&
118 Type::AssignableFrom<Second&, const OtherSecond&>
119 Pair<First, Second>& Pair<First, Second>::operator=(const std::pair<OtherFirst, OtherSecond>& other) {
120 first = other.first;
121 second = other.second;
122 return *this;
123 }
124
125 template<Type::SafeValue First, Type::SafeValue Second>
126 template<class OtherFirst, class OtherSecond>
127 requires Type::AssignableFrom<First&, OtherFirst> &&
128 Type::AssignableFrom<Second&, OtherSecond>
129 Pair<First, Second>& Pair<First, Second>::operator=(std::pair<OtherFirst, OtherSecond>&& other) {
130 first = std::move(other.first);
131 second = std::move(other.second);
132 return *this;
133 }
134
135 template<Type::SafeValue First, Type::SafeValue Second>
136 void Pair<First, Second>::swap(Pair& other) noexcept(Type::Swappable<First> && Type::Swappable<Second>) {
137 using std::swap;
138 swap(first, other.first);
139 swap(second, other.second);
140 }
141
142 template<Type::SafeValue First, Type::SafeValue Second>
143 bool Pair<First, Second>::operator==(const Pair& other) const requires Type::EqualityComparable<First> && Type::EqualityComparable<Second> {
144 return first == other.first && second == other.second;
145 }
146
147 template<Type::SafeValue First, Type::SafeValue Second>
148 STORMBYTE_FORCE_INLINE auto Pair<First, Second>::operator<=>(const Pair& other) const requires Type::ThreeWayComparable<First> && Type::ThreeWayComparable<Second> {
149 if (auto order = first <=> other.first; order != 0)
150 return order;
151 return second <=> other.second;
152 }
153
154 template<Type::SafeValue First, Type::SafeValue Second>
155 template<class... FirstArgs, class... SecondArgs, std::size_t... FirstIndexes, std::size_t... SecondIndexes>
156 Pair<First, Second>::Pair(std::piecewise_construct_t, std::tuple<FirstArgs...>& firstArgs, std::tuple<SecondArgs...>& secondArgs,
157 std::index_sequence<FirstIndexes...>, std::index_sequence<SecondIndexes...>):
158 first(std::get<FirstIndexes>(std::move(firstArgs))...),
159 second(std::get<SecondIndexes>(std::move(secondArgs))...) {}
160
161 template<Type::SafeValue First, Type::SafeValue Second, class SecondReference>
162 PairReference<First, Second, SecondReference>::PairReference(const First& firstValue, const Second& secondValue, SecondReference secondReference):
163 first(firstValue), second(std::move(secondReference)), m_snapshot(firstValue, secondValue) {}
164
165 template<Type::SafeValue First, Type::SafeValue Second, class SecondReference>
166 PairReference<First, Second, SecondReference>::operator const typename PairReference<First, Second, SecondReference>::value_type&() const {
167 m_snapshot.second = static_cast<Second>(second);
168 return m_snapshot;
169 }
170
171 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
172 requires (Index == 0)
173 constexpr First& get(Pair<First, Second>& pair) noexcept {
174 return pair.first;
175 }
176
177 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
178 requires (Index == 1)
179 constexpr Second& get(Pair<First, Second>& pair) noexcept {
180 return pair.second;
181 }
182
183 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
184 requires (Index == 0)
185 constexpr const First& get(const Pair<First, Second>& pair) noexcept {
186 return pair.first;
187 }
188
189 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
190 requires (Index == 1)
191 constexpr const Second& get(const Pair<First, Second>& pair) noexcept {
192 return pair.second;
193 }
194
195 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
196 requires (Index == 0)
197 constexpr First&& get(Pair<First, Second>&& pair) noexcept {
198 return std::move(pair.first);
199 }
200
201 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
202 requires (Index == 1)
203 constexpr Second&& get(Pair<First, Second>&& pair) noexcept {
204 return std::move(pair.second);
205 }
206
207 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
208 requires (Index == 0)
209 constexpr const First&& get(const Pair<First, Second>&& pair) noexcept {
210 return std::move(pair.first);
211 }
212
213 template<std::size_t Index, Type::SafeValue First, Type::SafeValue Second>
214 requires (Index == 1)
215 constexpr const Second&& get(const Pair<First, Second>&& pair) noexcept {
216 return std::move(pair.second);
217 }
218 }
219}