2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
4 * This is the base for StormByte Suite libraries.
6 * StormByte original source is dual-licensed:
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)
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>).
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.
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.
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.
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>.
37 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
46 template<Type::SafeValue First, Type::SafeValue Second>
47 Pair<First, Second>::Pair(const First& firstValue, const Second& secondValue):
48 first(firstValue), second(secondValue) {}
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)) {}
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)) {}
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...>{}) {}
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) {}
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)) {}
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) {}
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)) {}
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) {
101 second = other.second;
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);
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) {
121 second = other.second;
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);
135 template<Type::SafeValue First, Type::SafeValue Second>
136 void Pair<First, Second>::swap(Pair& other) noexcept(Type::Swappable<First> && Type::Swappable<Second>) {
138 swap(first, other.first);
139 swap(second, other.second);
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;
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)
151 return second <=> other.second;
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))...) {}
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) {}
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);
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 {
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 {
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 {
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 {
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);
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);
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);
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);